单击后退按钮会导致强制关闭/ NullPointerException

时间:2014-04-25 01:14:20

标签: java android android-intent nullpointerexception fatal-error

我在使用我构建的Android应用程序启动图库或默认音频播放器后单击后退按钮时遇到NullPointerException和ForceClose问题。

我已经看到一些例子说明我需要完成活动或检查后退按钮 - 但我不确定这究竟是多少适合这种情况。

第84行崩溃了:Uri selectedImage = data.getData();

来源:

public void onActivityResult(int requestCode, int resultCode, Intent data) {
    super.onActivityResult(requestCode, resultCode, data);
    // if (resultCode == Activity.RESULT_OK) {
    if (requestCode == SELECT_VIDEO) {
        {
            Uri selectedVideo = data.getData();
            Intent intent = new Intent(Intent.ACTION_VIEW);
            intent.setDataAndType(selectedVideo, "video/*");
            startActivity(intent);

        }
    } else if (requestCode == SELECT_PHOTO) {

        Uri selectedImage = data.getData();
        Intent intent = new Intent();
        intent.setAction(Intent.ACTION_VIEW);
        intent.setDataAndType(selectedImage, "image/*");
        startActivity(intent);
    }

logcat的:

04-24 21:04:26.441: D/AndroidRuntime(4491): Shutting down VM
04-24 21:04:26.441: W/dalvikvm(4491): threadid=1: thread exiting with uncaught exception (group=0x41df5930)
04-24 21:04:26.451: E/AndroidRuntime(4491): FATAL EXCEPTION: main
04-24 21:04:26.451: E/AndroidRuntime(4491): java.lang.RuntimeException: Failure delivering result ResultInfo{who=null, request=1, result=0, data=null} to activity {com.tablet.hairmax/com.tablet.hairmax.UI}: java.lang.NullPointerException
04-24 21:04:26.451: E/AndroidRuntime(4491):     at android.app.ActivityThread.deliverResults(ActivityThread.java:3319)
04-24 21:04:26.451: E/AndroidRuntime(4491):     at android.app.ActivityThread.handleSendResult(ActivityThread.java:3362)
04-24 21:04:26.451: E/AndroidRuntime(4491):     at android.app.ActivityThread.access$1100(ActivityThread.java:141)
04-24 21:04:26.451: E/AndroidRuntime(4491):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1282)
04-24 21:04:26.451: E/AndroidRuntime(4491):     at android.os.Handler.dispatchMessage(Handler.java:99)
04-24 21:04:26.451: E/AndroidRuntime(4491):     at android.os.Looper.loop(Looper.java:137)
04-24 21:04:26.451: E/AndroidRuntime(4491):     at android.app.ActivityThread.main(ActivityThread.java:5039)
04-24 21:04:26.451: E/AndroidRuntime(4491):     at java.lang.reflect.Method.invokeNative(Native Method)
04-24 21:04:26.451: E/AndroidRuntime(4491):     at java.lang.reflect.Method.invoke(Method.java:511)
04-24 21:04:26.451: E/AndroidRuntime(4491):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
04-24 21:04:26.451: E/AndroidRuntime(4491):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)
04-24 21:04:26.451: E/AndroidRuntime(4491):     at dalvik.system.NativeStart.main(Native Method)
04-24 21:04:26.451: E/AndroidRuntime(4491): Caused by: java.lang.NullPointerException
04-24 21:04:26.451: E/AndroidRuntime(4491):     at com.tablet.hairmax.UI.onActivityResult(UI.java:84)
04-24 21:04:26.451: E/AndroidRuntime(4491):     at android.app.Activity.dispatchActivityResult(Activity.java:5293)
04-24 21:04:26.451: E/AndroidRuntime(4491):     at android.app.ActivityThread.deliverResults(ActivityThread.java:3315)
04-24 21:04:26.451: E/AndroidRuntime(4491):     ... 11 more
04-24 21:04:28.721: I/Process(4491): Sending signal. PID: 4491 SIG: 9
04-24 21:04:39.681: D/AndroidRuntime(4543): Shutting down VM
04-24 21:04:39.681: W/dalvikvm(4543): threadid=1: thread exiting with uncaught exception (group=0x41df5930)
04-24 21:04:39.691: E/AndroidRuntime(4543): FATAL EXCEPTION: main
04-24 21:04:39.691: E/AndroidRuntime(4543): java.lang.RuntimeException: Failure delivering result ResultInfo{who=null, request=2, result=0, data=null} to activity {com.tablet.hairmax/com.tablet.hairmax.UI}: java.lang.NullPointerException
04-24 21:04:39.691: E/AndroidRuntime(4543):     at android.app.ActivityThread.deliverResults(ActivityThread.java:3319)
04-24 21:04:39.691: E/AndroidRuntime(4543):     at android.app.ActivityThread.handleSendResult(ActivityThread.java:3362)
04-24 21:04:39.691: E/AndroidRuntime(4543):     at android.app.ActivityThread.access$1100(ActivityThread.java:141)
04-24 21:04:39.691: E/AndroidRuntime(4543):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1282)
04-24 21:04:39.691: E/AndroidRuntime(4543):     at android.os.Handler.dispatchMessage(Handler.java:99)
04-24 21:04:39.691: E/AndroidRuntime(4543):     at android.os.Looper.loop(Looper.java:137)
04-24 21:04:39.691: E/AndroidRuntime(4543):     at android.app.ActivityThread.main(ActivityThread.java:5039)
04-24 21:04:39.691: E/AndroidRuntime(4543):     at java.lang.reflect.Method.invokeNative(Native Method)
04-24 21:04:39.691: E/AndroidRuntime(4543):     at java.lang.reflect.Method.invoke(Method.java:511)
04-24 21:04:39.691: E/AndroidRuntime(4543):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
04-24 21:04:39.691: E/AndroidRuntime(4543):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)
04-24 21:04:39.691: E/AndroidRuntime(4543):     at dalvik.system.NativeStart.main(Native Method)
04-24 21:04:39.691: E/AndroidRuntime(4543): Caused by: java.lang.NullPointerException
04-24 21:04:39.691: E/AndroidRuntime(4543):     at com.tablet.hairmax.UI.onActivityResult(UI.java:76)
04-24 21:04:39.691: E/AndroidRuntime(4543):     at android.app.Activity.dispatchActivityResult(Activity.java:5293)
04-24 21:04:39.691: E/AndroidRuntime(4543):     at android.app.ActivityThread.deliverResults(ActivityThread.java:3315)
04-24 21:04:39.691: E/AndroidRuntime(4543):     ... 11 more
04-24 21:05:14.191: D/AndroidRuntime(4586): Shutting down VM
04-24 21:05:14.191: W/dalvikvm(4586): threadid=1: thread exiting with uncaught exception (group=0x41df5930)
04-24 21:05:14.191: E/AndroidRuntime(4586): FATAL EXCEPTION: main
04-24 21:05:14.191: E/AndroidRuntime(4586): java.lang.RuntimeException: Failure delivering result ResultInfo{who=null, request=1, result=0, data=null} to activity {com.tablet.hairmax/com.tablet.hairmax.UI}: java.lang.NullPointerException
04-24 21:05:14.191: E/AndroidRuntime(4586):     at android.app.ActivityThread.deliverResults(ActivityThread.java:3319)
04-24 21:05:14.191: E/AndroidRuntime(4586):     at android.app.ActivityThread.handleSendResult(ActivityThread.java:3362)
04-24 21:05:14.191: E/AndroidRuntime(4586):     at android.app.ActivityThread.access$1100(ActivityThread.java:141)
04-24 21:05:14.191: E/AndroidRuntime(4586):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1282)
04-24 21:05:14.191: E/AndroidRuntime(4586):     at android.os.Handler.dispatchMessage(Handler.java:99)
04-24 21:05:14.191: E/AndroidRuntime(4586):     at android.os.Looper.loop(Looper.java:137)
04-24 21:05:14.191: E/AndroidRuntime(4586):     at android.app.ActivityThread.main(ActivityThread.java:5039)
04-24 21:05:14.191: E/AndroidRuntime(4586):     at java.lang.reflect.Method.invokeNative(Native Method)
04-24 21:05:14.191: E/AndroidRuntime(4586):     at java.lang.reflect.Method.invoke(Method.java:511)
04-24 21:05:14.191: E/AndroidRuntime(4586):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
04-24 21:05:14.191: E/AndroidRuntime(4586):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)
04-24 21:05:14.191: E/AndroidRuntime(4586):     at dalvik.system.NativeStart.main(Native Method)
04-24 21:05:14.191: E/AndroidRuntime(4586): Caused by: java.lang.NullPointerException
04-24 21:05:14.191: E/AndroidRuntime(4586):     at com.tablet.hairmax.UI.onActivityResult(UI.java:84)
04-24 21:05:14.191: E/AndroidRuntime(4586):     at android.app.Activity.dispatchActivityResult(Activity.java:5293)
04-24 21:05:14.191: E/AndroidRuntime(4586):     at android.app.ActivityThread.deliverResults(ActivityThread.java:3315)
04-24 21:05:14.191: E/AndroidRuntime(4586):     ... 11 more

1 个答案:

答案 0 :(得分:1)

datanull。如果datanull,您需要在该行前检查。由于您按下“返回”按钮,因此您不会像往常一样Intent发送setResult()

public void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
// if (resultCode == Activity.RESULT_OK) {
if (data != null)
{
    if (requestCode == SELECT_VIDEO) {
        Uri selectedVideo = data.getData();
        Intent intent = new Intent(Intent.ACTION_VIEW);
        intent.setDataAndType(selectedVideo, "video/*");
        startActivity(intent);
    } else if (requestCode == SELECT_PHOTO) {

        Uri selectedImage = data.getData();
        Intent intent = new Intent();
        intent.setAction(Intent.ACTION_VIEW);
        intent.setDataAndType(selectedImage, "image/*");
        startActivity(intent);
    }
}

See this answer about getting an image from gallery