Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

NullPointerException with Hardwareacceleration turned on?

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!

like image 668
5w4rley Avatar asked Oct 11 '12 11:10

5w4rley


1 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);
    }
}
like image 124
Aaron Mathew Crayford Avatar answered Nov 05 '22 10:11

Aaron Mathew Crayford