Fisica and Integration with Navigation Drawer

edited March 2016 in Android Mode

Hi, first of all, I want to share my excitement as I only realized today that PApplet was now extending Fragment rather than Activity :D

Second, I am now playing with this new PApplet and trying to place it as part of a NavigationDrawer kind of application.

However, it results in this error when switching away from the PApplet:

java.lang.NullPointerException: Attempt to invoke virtual method 'java.lang.Object android.app.Activity.getSystemService(java.lang.String)' on a null object reference
at processing.core.PGraphicsAndroid2D.imageImpl(Unknown Source)
at processing.core.PGraphics.image(Unknown Source)
at fisica.FDrawable.drawImage(FDrawable.java:97)
at fisica.FCircle.draw(FCircle.java:101)
at fisica.FDrawable.draw(FDrawable.java:119)
at fisica.FBody.draw(FBody.java:41)
at net.diyoyo.MainSketch.draw(MainSketch.java:76)
...

in my PApplet, I just attach an image to a body:

in setup():

    inner=loadImage("inner.png");
    circle1=new FCircle(inner.width);
    circle1.setPosition(width/2, height/2);
    circle1.setName("inner");
    circle1.attachImage(inner);
    //circle1.setStatic(true);
    circle1.setSensor(true);

in draw():

    background(255);
    world.step();
   // image(inner, displayWidth/2, displayHeight/2);
    circle1.draw(this);

This last line causes the error.

I am using Fisica directly from the source, but I don't understand what's wrong in the drawImage method:

protected void drawImage(PGraphics applet) {
applet.tint(255, 255, 255, m_imageAlpha);
applet.image(m_image, 0-m_image.width/2, 0-m_image.height/2);
applet.tint(255, 255, 255, 255);
}

I tried changing draw(this) by draw(this.g), but it does do anything...

I guess it's a problem of how PApplet now manages the images when it is sent to the back stack or something, isn't it?

Thanks.

Tagged:

Answers

  • OK, I guess it comes from this part of PGraphicsAndroid2D:

    // If the OS things the memory is low, then recycles bitmaps automatically...
    // but I don't think it is particularly efficient, as the bitmaps are stored
    // in native heap for Android 10 and older.
    MemoryInfo mi = new MemoryInfo();
    ActivityManager activityManager = (ActivityManager) parent.getActivity().getSystemService(android.content.Context.ACTIVITY_SERVICE);
    activityManager.getMemoryInfo(mi);
    if (mi.lowMemory) {
      src.bitmap.recycle();
      src.bitmap = null;
    

    But then the question is: can I safely edit it?

  • Commenting these lines out solved the problem, but is it safe?

Sign In or Register to comment.