启动活动结果java.lang.RuntimeException

时间:2011-06-03 13:56:35

标签: android exception null

我在清单中有一项活动:

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

使用以下代码打开它时:

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

我明白了:

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

有什么想法吗?

2 个答案:

答案 0 :(得分:0)

你有intent.getAction()== null。

修改您的代码行:

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

答案 1 :(得分:0)

关于此代码:

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

使用.equals()将您知道的内容放在左侧不为空时,这是最佳做法(常识),因此您可以避免这些NullPointerException s 。所以重写为:

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