Android NullPointerException in Instrumentation.execStartActivity

前端 未结 2 1993
半阙折子戏
半阙折子戏 2021-02-05 04:44

I keep getting the bellow exception from some users:

java.lang.NullPointerException
    at android.app.Instrumentation.execStartActivity(Instrumentation.java:141         


        
2条回答
  •  后悔当初
    2021-02-05 05:21

    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
        }
    }
    

提交回复
热议问题