49

Started getting this error in the production version of my app.

java.lang.NullPointerException: Attempt to invoke virtual method 'boolean android.content.Intent.migrateExtraStreamToClipData()' on a null object reference

There's no clear line at which this actually occurs but I recently changed my support library version to 24.0.0. Here's the full stacktrace:

Fatal Exception: java.lang.NullPointerException: Attempt to invoke virtual method 'boolean android.content.Intent.migrateExtraStreamToClipData()' on a null object reference
   at android.app.Instrumentation.execStartActivity(Instrumentation.java:1494)
   at android.app.Activity.startActivityForResult(Activity.java:3745)
   at android.support.v4.app.BaseFragmentActivityJB.startActivityForResult(BaseFragmentActivityJB.java:48)
   at android.support.v4.app.FragmentActivity.startActivityForResult(FragmentActivity.java:75)
   at android.app.Activity.startActivityForResult(Activity.java:3706)
   at android.support.v4.app.FragmentActivity.startActivityForResult(FragmentActivity.java:871)
   at com.google.android.gms.common.internal.zzi$1.zztD(Unknown Source)
   at com.google.android.gms.common.internal.zzi.onClick(Unknown Source)
   at com.android.internal.app.AlertController$ButtonHandler.handleMessage(AlertController.java:162)
   at android.os.Handler.dispatchMessage(Handler.java:102)
   at android.os.Looper.loop(Looper.java:135)
   at android.app.ActivityThread.main(ActivityThread.java:5254)
   at java.lang.reflect.Method.invoke(Method.java)
   at java.lang.reflect.Method.invoke(Method.java:372)
   at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:903)
   at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:698)

EDIT: I also want to note that 100% of the users getting this error are also rooted. This also occurs on 23.4.0... I also have a potential related error which popped up at the same time which has to do with the Base64.decode function in relation to Firebase.

EDIT 2: I received some help from an Android Dev the other day. They suggested that I update my project's Google Play Services version and it seems to have helped so far. I'll wait a few more days to get the results from my users but the initial logs are promising.

I was previously using 9.0.2 but I'm now on 9.2.0.

EDIT 3: Updating to 9.2.0 didn't help the crashes. I'm still getting the same error from rooted users. I've noted that at the users getting crashes are below Android 6.0 so I'll be testing on a live device and update ASAP.

c0deblooded
  • 1,737
  • 1
  • 16
  • 20
  • 2
    This happens on my emulator as well. First i get a popup saying: ... Relies on Google Play Services, which is not supported by your device. Contact the manufacturer for assistance. I will try to debug. – Wirling Jun 30 '16 at 07:11
  • @Wirling Now that you mention it, I have a bit of a suspicion that the error reports are coming from Google's Cloud Test lab devices instead of my user's devices. I've just recently rooted my device specifically to test for this error and I haven't seen it pop up yet. – c0deblooded Jul 08 '16 at 01:17
  • I'm able to reproduce this using the Google Play Services Vision library (9.2). I do a simple check by using the `isGooglePlayServicesAvailable` method before starting up the camera for QR code detection. If it is not available I'll then show the dialog from the `getErrorDialog` method. It will then crash when trying to update. Only happening on rooted devices. – Brian Jul 23 '16 at 10:54
  • I am also facing the same problem when trying to open Playstore - Here's the code - Intent intent = new Intent(Intent.ACTION_VIEW); intent.setData(Uri.parse("market://details?id=com.whatsapp")); startActivity(intent); – Vamsi Aug 01 '16 at 09:20
  • 3
    Hi @c0d3blooded . I've looked at this issue: https://github.com/google/gcm/issues/209, and they are saying, that the bug is fixed. Can you (or anybody) check that? I cannot reproduce the bug. Thanks! – NonGrate Aug 31 '16 at 11:51

8 Answers8

19

Seems like the error occurs on devices where Google Play Services are not installed, passed intent will then be null.

You can make sure intent passed is not null by overriding startActivityForResult method in your Activity.

@Override    
public void startActivityForResult(Intent intent, int requestCode) {
    if (intent == null) {    
        intent = new Intent();        
    }       
    super.startActivityForResult(intent, requestCode);
}
Nikola Despotoski
  • 49,966
  • 15
  • 119
  • 148
MVojtkovszky
  • 519
  • 4
  • 11
  • @jlivelyare you can alternatively call super in a try catch block. That way null pointer exception will be caught without a doubt. The only thing to make sure is that the activity that overrides startActivityForResult is the right one. – MVojtkovszky Jul 27 '16 at 08:41
  • 1
    Excuse me, but what on Earth is the point of starting activity with empty intent? – Jan Hudec May 10 '17 at 13:10
  • 1
    @JanHudec the whole point of this answer is to avoid processing passed null intent, which can actually happen beyond your control, and will cause the crash (hence the question). – MVojtkovszky May 10 '17 at 14:03
8

This actually worked for me.

For Android 11 (API level 30) or higher, in AndroidManifest.xml,

<queries>
        <package android:name="com.google.android.youtube" />
        <package android:name="com.example.app" />
</queries>

"If your app targets Android 11 (API level 30) or higher and needs to interact with apps other than the ones that are visible automatically, add the element in your app's manifest file. Within the element, specify the other apps by package name."

Refer here for more details.

dmukherjee
  • 153
  • 1
  • 5
4

This question is a bit old, but I just wanted to share an update on it. According to this Github issue on the GCM project the issue should be solved in Google Play Services version 9.4.0. The accepted answer should work as well (as an intermediate patch), but if you update your Google Play Services library this issue should be solved.

Smalls
  • 352
  • 3
  • 13
2

You can open app directly using getLaunchIntentForPackage and set your app package

val intentApp = requireActivity().packageManager.getLaunchIntentForPackage("com.twitter.android")
        try {
            this.startActivity(intentApp)
        } catch (ex: ActivityNotFoundException) {
            Toast.makeText(this,getString(R.string.activity_no_app), Toast.LENGTH_LONG).show()
        }

But Android 11 (API level 30 ) for security reason it will no working , to over come this solution you need to specify app package in AndroidManifes.xml

<queries>
        <package android:name="com.google.android.youtube" />
        <package android:name="com.example.app" />
</queries>
Nimantha
  • 6,405
  • 6
  • 28
  • 69
Tarif Chakder
  • 1,708
  • 1
  • 11
  • 10
2

It got fixed after adding this code in Manifest

<uses-permission android:name="android.permission.QUERY_ALL_PACKAGES" tools:ignore="QueryAllPackagesPermission" />
Murali M
  • 21
  • 2
1

thats really works

@Override
public void startActivityForResult(Intent intent, int requestCode) {
    try {
        super.startActivityForResult(intent, requestCode);
    } catch (Exception ignored){}
}
user2212515
  • 1,220
  • 1
  • 12
  • 10
1

Most probably it's the same problem as in getLaunchIntentForPackage is null for some apps. Not really a duplicate (since OP couldn't trace the offending line, which is known in linked question), but if someone faces this problem, linked question's solution might help.

Piotr Śmietana
  • 393
  • 2
  • 19
  • I am having this exact issue. There has to be a way around it. Other 3rd party apps are successfully opening activities that I can't – A P Aug 06 '19 at 13:43
1

Following code worked for Android 11 (API level 30) or higher:

In AndroidManifest.xml,

<queries>
        <package android:name="com.google.android.youtube" />
        <package android:name="com.example.app" />
</queries>

"If your app targets Android 11 (API level 30) or higher and needs to interact with apps other than the ones that are visible automatically, add the element in your app's manifest file. Within the element, specify the other apps by package name."

Refer here for more details.

dmukherjee
  • 153
  • 1
  • 5