25

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?

Adrian
  • 717
  • 11
  • 18
  • Similar issue found on a Samsung GT I9500 (Galaxy S4) but on line 1416. Interestingly I'm also using Google Maps API v2 and have been receiving various errors relating to maps. – TheIT Feb 04 '14 at 19:42

2 Answers2

17

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
    }
}
JJD
  • 50,076
  • 60
  • 203
  • 339
Malachiasz
  • 7,126
  • 2
  • 35
  • 49
  • Which class's startActivityForResult should be overrridden? – JY2k Aug 28 '14 at 13:51
  • The one which extends Activity or FragmentActivity and crashes. – Malachiasz Aug 28 '14 at 14:26
  • @steelbytes: some explenation? How do you know that this error is caused by com.android.vending ? – Malachiasz Feb 11 '15 at 12:14
  • @Malachiasz: I was also experiencing this problem so I logged the value of intent during the catch and saw it was always com.android.vending (missing). Since I only want to hide this exception when caused by MapView on phones without playservices/playstore, I added filter on intent.pkg as I don't want to hide other exceptions (eg, if my own code calls startActivityForResult) – SteelBytes Feb 12 '15 at 01:29
  • @SteelBytes that's interesting, for me the `intent` was `null`. Malachiasz, might I suggest something though: instead of swallowing the `Exception` (which should only be `NPE`, btw.), it would be nicer to `onActivityResult(requestCode, RESULT_CANCELED, intent);`, because that code is very likely already handling the non-`RESULT_OK` case. – TWiStErRob Jul 25 '16 at 22:12
0

in my case crash in Instrumentation.java

line 1581,

android.util.SeempLog.record_str(377, intent.toString());

intent is null then NPE

not sure how to fix this

Maayan Hope
  • 1,482
  • 17
  • 32