从意图选择器中挑选图像后Android应用程序崩溃

时间:2014-06-20 22:29:53

标签: android image android-intent

嗨,在我的Android应用程序中,我试图使用意图选择打开图像。

在另一个类中,我有这个功能打开一个对话框,通过单击其中的IageView,它打开了选择图片的意图。

public void OpenImageUploadDialog() {
    final NavigationScreen context = (NavigationScreen) img.getContext();

    final Dialog imageUploadDialog = new Dialog(context);
    imageUploadDialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
    imageUploadDialog.setContentView(R.layout.dialog_upload_image);

    final TextView avatarStatus = (TextView) imageUploadDialog.findViewById(R.id.avatar_status);
    final ImageView avatar = (ImageView) imageUploadDialog.findViewById(R.id.avatar);
    Button addButton = (Button) imageUploadDialog.findViewById(R.id.save);
    Button cancelButton = (Button) imageUploadDialog.findViewById(R.id.cancel);

    dialog_img = avatar;
    dialog_status = avatarStatus;

    avatar.setOnClickListener(new OnClickListener() {
        @Override
        public void onClick(View arg0) {
            Intent intent = new Intent();
            intent.setType("image/*");
            intent.setAction(Intent.ACTION_GET_CONTENT);

            Intent image_chooser = Intent.createChooser(intent, context.getString(R.string.select_picture));
            context.startActivityForResult(image_chooser, SELECT_PICTURE);
        }
    });

    addButton.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            if (isReady) {
                if (Network.isNetworkAvailable(context)) {
                    imageUploadDialog.dismiss();
                } else {    
                    Views.ShowErrorMessage(context, context.getString(R.string.no_internet));
                }
            } else {
                Views.ShowErrorMessage(context, context.getString(R.string.select_image_first));
            }
        }
    });

    cancelButton.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            imageUploadDialog.dismiss();
        }
    });

    imageUploadDialog.show();
}

然后在活动课上,我有这个:

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    super.onActivityResult(requestCode, resultCode, data);
    if (resultCode == RESULT_OK) {
        if (imageUploadHandler != null){
            imageUploadHandler.handleResult(this, data);
        }
    }
}

当我单击对话框上的ImageView时,意图显示,然后我可以选择一个图像。一旦我这样做,它会返回到应用程序,但活动是白屏,然后在2秒后崩溃。这是日志:

06-20 18:24:02.465: E/AndroidRuntime(9254): FATAL EXCEPTION: main
06-20 18:24:02.465: E/AndroidRuntime(9254): java.lang.RuntimeException: Unable to resume activity {android.arin/android.arin.NavigationScreen}: java.lang.RuntimeException: Failure delivering result ResultInfo{who=null, request=0, result=-1, data=Intent { dat=content://media/external/images/media/11738 (has extras) }} to activity {android.arin/android.arin.NavigationScreen}: java.lang.NullPointerException
06-20 18:24:02.465: E/AndroidRuntime(9254):     at android.app.ActivityThread.performResumeActivity(ActivityThread.java:2639)
06-20 18:24:02.465: E/AndroidRuntime(9254):     at android.app.ActivityThread.handleResumeActivity(ActivityThread.java:2667)
06-20 18:24:02.465: E/AndroidRuntime(9254):     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2140)
06-20 18:24:02.465: E/AndroidRuntime(9254):     at android.app.ActivityThread.access$700(ActivityThread.java:143)
06-20 18:24:02.465: E/AndroidRuntime(9254):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1241)
06-20 18:24:02.465: E/AndroidRuntime(9254):     at android.os.Handler.dispatchMessage(Handler.java:99)
06-20 18:24:02.465: E/AndroidRuntime(9254):     at android.os.Looper.loop(Looper.java:137)
06-20 18:24:02.465: E/AndroidRuntime(9254):     at android.app.ActivityThread.main(ActivityThread.java:4950)
06-20 18:24:02.465: E/AndroidRuntime(9254):     at java.lang.reflect.Method.invokeNative(Native Method)
06-20 18:24:02.465: E/AndroidRuntime(9254):     at java.lang.reflect.Method.invoke(Method.java:511)
06-20 18:24:02.465: E/AndroidRuntime(9254):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1004)
06-20 18:24:02.465: E/AndroidRuntime(9254):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:771)
06-20 18:24:02.465: E/AndroidRuntime(9254):     at dalvik.system.NativeStart.main(Native Method)
06-20 18:24:02.465: E/AndroidRuntime(9254): Caused by: java.lang.RuntimeException: Failure delivering result ResultInfo{who=null, request=0, result=-1, data=Intent { dat=content://media/external/images/media/11738 (has extras) }} to activity {android.arin/android.arin.NavigationScreen}: java.lang.NullPointerException
06-20 18:24:02.465: E/AndroidRuntime(9254):     at android.app.ActivityThread.deliverResults(ActivityThread.java:3205)
06-20 18:24:02.465: E/AndroidRuntime(9254):     at android.app.ActivityThread.performResumeActivity(ActivityThread.java:2626)
06-20 18:24:02.465: E/AndroidRuntime(9254):     ... 12 more
06-20 18:24:02.465: E/AndroidRuntime(9254): Caused by: java.lang.NullPointerException
06-20 18:24:02.465: E/AndroidRuntime(9254):     at android.arin.NavigationScreen.onActivityResult(NavigationScreen.java:181)
06-20 18:24:02.465: E/AndroidRuntime(9254):     at android.app.Activity.dispatchActivityResult(Activity.java:5361)
06-20 18:24:02.465: E/AndroidRuntime(9254):     at android.app.ActivityThread.deliverResults(ActivityThread.java:3201)
06-20 18:24:02.465: E/AndroidRuntime(9254):     ... 13 more

有没有人看到这个问题?

由于

1 个答案:

答案 0 :(得分:0)

你还没有在这里包含行号,但我猜测NavigationScreen.java:181是

imageUploadHandler.handleResult(this,data);

并且imageUploadHandler为null(由于某种原因;该代码未显示)。在任何情况下,看起来NPE指向NavigationScreen.java:181你应该看那里。