tschoffelen / react-native-email-link

📭 Open an email client from React Native (for 'magic link' type functionality).
MIT License
409 stars 74 forks source link

[Android] Attempt to invoke virtual method 'int android.content.Intent.getFlags()' on a null object reference #20

Closed mk-nickyang closed 5 years ago

mk-nickyang commented 5 years ago

Hi, this library works perfectly on most Android devices, but when I tested it on a Galaxy S6 with Android 5, it crashed. Here's the stack trace I got

java.lang.NullPointerException Attempt to invoke virtual method 'int android.content.Intent.getFlags()' on a null object reference 
    Intent.java:891 android.content.Intent.createChooser
    EmailModule.java:42 com.facebook.react.modules.email.EmailModule.open
    Method.java:-2 java.lang.reflect.Method.invoke
    Method.java:372 java.lang.reflect.Method.invoke
    JavaMethodWrapper.java:372 com.facebook.react.bridge.JavaMethodWrapper.invoke
    JavaModuleWrapper.java:160 com.facebook.react.bridge.JavaModuleWrapper.invoke
    NativeRunnable.java:-2 com.facebook.react.bridge.queue.NativeRunnable.run
    Handler.java:739 android.os.Handler.handleCallback
    Handler.java:95 android.os.Handler.dispatchMessage
    MessageQueueThreadHandler.java:29 com.facebook.react.bridge.queue.MessageQueueThreadHandler.dispatchMessage
    Looper.java:145 android.os.Looper.loop
    MessageQueueThreadImpl.java:192 com.facebook.react.bridge.queue.MessageQueueThreadImpl$3.run
    Thread.java:818 java.lang.Thread.run

I also experienced the similar crashes when I test it on some Android Emulators with Android 7 (API version 25) or earlier

Image from Gyazo


My code:

import { openInbox } from "react-native-email-link";
...
<Button
    onPress={() => openInbox()}
    title="Open Email App"
/>
...

My environment:

"react-native": "0.57.0",
"react": "16.5.0",
"react-native-email-link": "^1.2.1",
tschoffelen commented 5 years ago

@RBogie any chance you could have a look at this some time next week? I noticed we are also getting similar crash reports for our own app (Hoy).

I've tried a few things, but I can't really figure out how to properly solve this problem. Need to brush up on my Java...

Thanks!

jorisw commented 5 years ago

Same error here, that is, the one in red, on Android only.

Attempt to read from field 'java.lang.String android.content.Intent.mAction' on a null object reference

The error seems to occur in node_modules/react-native-email-link/android/src/main/java/com/facebook/react/modules/email/EmailModule.java at line 34:

Intent emailIntent = new Intent(Intent.ACTION_VIEW, Uri.parse("mailto:"));

My environment:

        buildToolsVersion = "27.0.3"
        minSdkVersion = 16
        compileSdkVersion = 27
        targetSdkVersion = 26
        supportLibVersion = "27.0.3"
    "react": "16.6.0-alpha.8af6728",
    "react-native": "0.57.3",
    "react-native-email-link": "^1.2.3",
johnnyborg commented 5 years ago

Yeah, we've got this issue too. Its however not an issue on the line state @jorisw but a bit lower. Android on lower SDKs has an com.android.fallback package which has no default intent, causing the null pointer exception.

I've written an fix for this. @tschoffelen can you integrate it? https://github.com/leanmotherfuckers/react-native-email-link/pull/24

tschoffelen commented 5 years ago

Thanks @johnnyborg, that's great! I've incorporated your fix into the latest NPM release (react-native-email-link@1.4.0).

johnnyborg commented 5 years ago

Awesome, @tschoffelen . Bedankt ;-)!