Home » Android » java – NullPointerException with Hardwareacceleration turned on?

java – NullPointerException with Hardwareacceleration turned on?

Posted by: admin May 14, 2020 Leave a comment

Questions:

I got some error messages from my application on a simple button click. After the click an animation should start, but the button becomes bigger. It runs perfectly on most devices, but on some (i think mostly 4.X and above) it crashes.

Exception message (Stack Trace):

java.lang.NullPointerException
at android.view.GLES20RecordingCanvas.drawBitmap(GLES20RecordingCanvas.java:118)
at android.graphics.drawable.BitmapDrawable.draw(BitmapDrawable.java:400)
at android.view.View.draw(View.java:10999)
at android.view.ViewGroup.drawChild(ViewGroup.java:3186)
at android.view.ViewGroup.dispatchDraw(ViewGroup.java:2788)
at android.view.ViewGroup.drawChild(ViewGroup.java:3184)
at android.view.ViewGroup.dispatchDraw(ViewGroup.java:2788)
at android.view.ViewGroup.drawChild(ViewGroup.java:3184)
at android.view.ViewGroup.dispatchDraw(ViewGroup.java:2788)
at android.view.ViewGroup.drawChild(ViewGroup.java:3184)
at android.view.ViewGroup.dispatchDraw(ViewGroup.java:2788)
at android.view.View.draw(View.java:11017)
at android.view.View.getDisplayList(View.java:10444)
at android.view.ViewGroup.dispatchGetDisplayList(ViewGroup.java:2896)
at android.view.View.getDisplayList(View.java:10407)
at android.view.ViewGroup.dispatchGetDisplayList(ViewGroup.java:2896)
at android.view.View.getDisplayList(View.java:10407)
at android.view.ViewGroup.dispatchGetDisplayList(ViewGroup.java:2896)
at android.view.View.getDisplayList(View.java:10407)
at android.view.HardwareRenderer$GlRenderer.draw(HardwareRenderer.java:883)
at android.view.ViewRootImpl.draw(ViewRootImpl.java:2089)
at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:1781)
at android.view.ViewRootImpl.handleMessage(ViewRootImpl.java:2666)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:156)
at android.app.ActivityThread.main(ActivityThread.java:4977)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:511)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:784)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:551)
at dalvik.system.NativeStart.main(Native Method)

Thanks in advance!

How to&Answers:

I’ve been searching for the answer to this for the last couple days. It’s documented in ICS but I have noticed it in one of my test phones a Samsung Galaxy S3 running 4.1.2. Most people suggest turning off hardware acceleration; I didn’t want to do that cause hardware acceleration makes my animations so silky smooth. So what I found was if I override draw() try and catch the null pointer exception the hardware accelerated features work (most of the time) and enough for you to never notice they aren’t working or throwing the exception.

@Override
public void draw(Canvas canvas) {
    try{
        super.draw(canvas);
    }catch(java.lang.NullPointerException e){
        //you can log if you want to you can leave your friends behind
        //Log.e("mytag","MyCustomView::draw():"+e);
    }
}

Answer:

I’ve seen this very same error and it is strange but only happens in a very specific scenario.

I’ll describe it here in case it helps anyone.

It appears when I run a scale animation that goes to 0% on a view (basically shrink to the middle of the screen) on a ICS device with HW acceleration turned on.

  • On ICS, HW acceleration off, it does not crash.
  • On ICS, HW acceleration on, other animation types (grow, translate) it does not crash.
  • On Jelly Bean (4.1, 4.3) (any combination of other factors) it does not crash.

I think I will disable HW acceleration just for that view and move on, so the rest of my app will run smoothly.

Hope this helps.