Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android NullPointerException in Instrumentation.execStartActivity

Tags:

I keep getting the bellow exception from some users:

java.lang.NullPointerException
    at android.app.Instrumentation.execStartActivity(Instrumentation.java:1414)
    at android.app.Activity.startActivityForResult(Activity.java:2880)
    at android.support.v4.app.FragmentActivity.startActivityForResult(FragmentActivity.java:817)
    at android.app.Activity.startActivity(Activity.java:2986)
    at com.google.android.gms.internal.bb$5.onClick(Unknown Source)
    at android.view.View.performClick(View.java:2535)
    at android.view.View$PerformClick.run(View.java:9130)
    at android.os.Handler.handleCallback(Handler.java:618)
    at android.os.Handler.dispatchMessage(Handler.java:123)
    at android.os.Looper.loop(SourceFile:351)
    at android.app.ActivityThread.main(ActivityThread.java:3826)
    at java.lang.reflect.Method.invokeNative(Native Method)
    at java.lang.reflect.Method.invoke(Method.java:538)
    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:969)
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:727)
    at dalvik.system.NativeStart.main(Native Method)

I've found a similar problem here but it's not taken care of since April. All I know is that it was reproduced on a Samsung Galaxy Y (GT-S5360) and I'm using Google Maps Android API v2.

Do you have any idea how can I make a workaround for this?

like image 439
Adrian Avatar asked Jul 09 '13 13:07

Adrian


1 Answers

I have the same error, I also use Google Maps API. It seems to happen on all Android versions and phones. Just to mention few:

AN10DG3, GT-I8190, F815, GT-I9300, GOOPHONE, HTC One, KFTT, MID8127, KFAPWI, AN10DG3

java.lang.NullPointerException
at android.app.Instrumentation.execStartActivity(Instrumentation.java:1409)
at android.app.Activity.startActivityForResult(Activity.java:3351)
at android.app.Activity.startActivityForResult(Activity.java:3312)
at android.support.v4.app.FragmentActivity.startActivityForResult(FragmentActivity.java:839)
at android.app.Activity.startActivity(Activity.java:3522)
at android.app.Activity.startActivity(Activity.java:3490)
at com.google.android.gms.dynamic.a$5.onClick(Unknown Source)
at android.view.View.performClick(View.java:4084)
at android.view.View$PerformClick.run(View.java:16966)
at android.os.Handler.handleCallback(Handler.java:615)
at android.os.Handler.dispatchMessage(Handler.java:92)
at android.os.Looper.loop(Looper.java:137)
at android.app.ActivityThread.main(ActivityThread.java:4745)
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:786)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
at dalvik.system.NativeStart.main(Native Method) 

as I read somewhere, solution is:

Above mentioned problem can occur if you have emulator or device in which google play services are not installed. I don't have perfect solution but I figured out some work around to save your app from crashing. In this case you have to follow simple steps

  1. Override startActivityForResult(intent, requestcode)
  2. In startActivityForResult add super call super.startActivityForResult in try catch and catch the NullPointerException

Easy its done Now you can trap the nullpointerexception here and add your error handling in catch

@Override
public void startActivityForResult(Intent intent, int requestCode) {
    try {
        super.startActivityForResult(intent, requestCode);
    } catch (Exception e) {
        // fixes Google Maps bug: http://stackoverflow.com/a/20905954/2075875
    }
}
like image 188
Malachiasz Avatar answered Oct 13 '22 07:10

Malachiasz