Starting activity results java.lang.RuntimeExcepti

2019-08-15 03:05发布

问题:

I have an activity in manifest:

<activity android:name="RingdroidSelectActivity"
          android:label="@string/main_activity_label">

    <intent-filter>
        <action android:name="android.intent.action.GET_CONTENT" />
        <category android:name="android.intent.category.DEFAULT" />
        <category android:name="android.intent.category.OPENABLE" />
        <data android:mimeType="audio/*" />
    </intent-filter>

</activity>

When using the following code to open it:

startActivity (new Intent(getApplicationContext(), com.ringdroid.RingdroidSelectActivity.class));

I get:

06-03 09:49:32.744: ERROR/AndroidRuntime(11360): FATAL EXCEPTION: main
06-03 09:49:32.744: ERROR/AndroidRuntime(11360): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.ringdroid/com.ringdroid.RingdroidSelectActivity}: java.lang.NullPointerException
06-03 09:49:32.744: ERROR/AndroidRuntime(11360):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1647)
06-03 09:49:32.744: ERROR/AndroidRuntime(11360):     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1663)
06-03 09:49:32.744: ERROR/AndroidRuntime(11360):     at android.app.ActivityThread.access$1500(ActivityThread.java:117)
06-03 09:49:32.744: ERROR/AndroidRuntime(11360):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:931)
06-03 09:49:32.744: ERROR/AndroidRuntime(11360):     at android.os.Handler.dispatchMessage(Handler.java:99)
06-03 09:49:32.744: ERROR/AndroidRuntime(11360):     at android.os.Looper.loop(Looper.java:130)
06-03 09:49:32.744: ERROR/AndroidRuntime(11360):     at android.app.ActivityThread.main(ActivityThread.java:3683)
06-03 09:49:32.744: ERROR/AndroidRuntime(11360):     at java.lang.reflect.Method.invokeNative(Native Method)
06-03 09:49:32.744: ERROR/AndroidRuntime(11360):     at java.lang.reflect.Method.invoke(Method.java:507)
06-03 09:49:32.744: ERROR/AndroidRuntime(11360):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839)
06-03 09:49:32.744: ERROR/AndroidRuntime(11360):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597)
06-03 09:49:32.744: ERROR/AndroidRuntime(11360):     at dalvik.system.NativeStart.main(Native Method)
06-03 09:49:32.744: ERROR/AndroidRuntime(11360): Caused by: java.lang.NullPointerException
06-03 09:49:32.744: ERROR/AndroidRuntime(11360):     at com.ringdroid.RingdroidSelectActivity.onCreate(RingdroidSelectActivity.java:108)
06-03 09:49:32.744: ERROR/AndroidRuntime(11360):     at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
06-03 09:49:32.744: ERROR/AndroidRuntime(11360):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1611)
06-03 09:49:32.744: ERROR/AndroidRuntime(11360):     ... 11 more

Any idea?

回答1:

You have intent.getAction()== null obviously.

Fix your line of code to:

mWasGetContentIntent = Intent.ACTION_GET_CONTENT.equals(intent.getAction());


回答2:

Regarding this code:

Intent intent = getIntent(); 
mWasGetContentIntent = intent.getAction().equals( Intent.ACTION_GET_CONTENT);

It's best practice (and common sense) when using the .equals() to put what you know not to be null on the left side, so you avoid these NullPointerExceptions. So rewrite it to be:

mWasGetContentIntent =  Intent.ACTION_GET_CONTENT.equals(intent.getAction());