3

我的应用程序有一个表单,需要从图库中选择图片或拍照。

为此,有两个按钮。做画廊挑选的人工作正常。

问题在于可以让您拍照的那个。单击时,它成功打开相机。然后,您可以拍照,它会询问您是要保留还是丢弃它。

如果您选择保存它,它会尝试返回应用程序,但它会崩溃。

logcat 提示这个错误。

11-30 23:20:06.288: ERROR/AndroidRuntime(3045): FATAL EXCEPTION: main
11-30 23:20:06.288: ERROR/AndroidRuntime(3045): java.lang.RuntimeException: Unable to resume activity {com.android.upvar/com.android.upvar.NewPOI}: java.lang.RuntimeException: Failure delivering result ResultInfo{who=null, request=1, result=-1, data=Intent { act=inline-data (has extras) }} to activity {com.android.upvar/com.android.upvar.NewPOI}: java.lang.NullPointerException
11-30 23:20:06.288: ERROR/AndroidRuntime(3045):     at android.app.ActivityThread.performResumeActivity(ActivityThread.java:2124)
11-30 23:20:06.288: ERROR/AndroidRuntime(3045):     at android.app.ActivityThread.handleResumeActivity(ActivityThread.java:2139)
11-30 23:20:06.288: ERROR/AndroidRuntime(3045):     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1672)
11-30 23:20:06.288: ERROR/AndroidRuntime(3045):     at android.app.ActivityThread.access$1500(ActivityThread.java:117)
11-30 23:20:06.288: ERROR/AndroidRuntime(3045):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:935)
11-30 23:20:06.288: ERROR/AndroidRuntime(3045):     at android.os.Handler.dispatchMessage(Handler.java:99)
11-30 23:20:06.288: ERROR/AndroidRuntime(3045):     at android.os.Looper.loop(Looper.java:130)
11-30 23:20:06.288: ERROR/AndroidRuntime(3045):     at android.app.ActivityThread.main(ActivityThread.java:3687)
11-30 23:20:06.288: ERROR/AndroidRuntime(3045):     at java.lang.reflect.Method.invokeNative(Native Method)
11-30 23:20:06.288: ERROR/AndroidRuntime(3045):     at java.lang.reflect.Method.invoke(Method.java:507)
11-30 23:20:06.288: ERROR/AndroidRuntime(3045):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:842)
11-30 23:20:06.288: ERROR/AndroidRuntime(3045):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:600)
11-30 23:20:06.288: ERROR/AndroidRuntime(3045):     at dalvik.system.NativeStart.main(Native Method)
11-30 23:20:06.288: ERROR/AndroidRuntime(3045): Caused by: java.lang.RuntimeException: Failure delivering result ResultInfo{who=null, request=1, result=-1, data=Intent { act=inline-data (has extras) }} to activity {com.android.upvar/com.android.upvar.NewPOI}: java.lang.NullPointerException
11-30 23:20:06.288: ERROR/AndroidRuntime(3045):     at android.app.ActivityThread.deliverResults(ActivityThread.java:2536)
11-30 23:20:06.288: ERROR/AndroidRuntime(3045):     at android.app.ActivityThread.performResumeActivity(ActivityThread.java:2111)
11-30 23:20:06.288: ERROR/AndroidRuntime(3045):     ... 12 more
11-30 23:20:06.288: ERROR/AndroidRuntime(3045): Caused by: java.lang.NullPointerException
11-30 23:20:06.288: ERROR/AndroidRuntime(3045):     at com.android.upvar.NewPOI.onActivityResult(NewPOI.java:130)
11-30 23:20:06.288: ERROR/AndroidRuntime(3045):     at android.app.Activity.dispatchActivityResult(Activity.java:3908)
11-30 23:20:06.288: ERROR/AndroidRuntime(3045):     at android.app.ActivityThread.deliverResults(ActivityThread.java:2532)
11-30 23:20:06.288: ERROR/AndroidRuntime(3045):     ... 13 more

这是启动 Activity 的按钮侦听器:

    //mTakeImg is the button
    mTakeImg.setOnClickListener(new View.OnClickListener() {            
        @Override
        public void onClick(View v) {
            // TODO Auto-generated method stub
            Intent cameraIntent = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE);  
            startActivityForResult(cameraIntent, CAMERA_PIC_REQUEST);  
        }
    });

这是接受响应的 Activity 结果方法:

@Override
    protected void onActivityResult(int requestCode, int resultCode, Intent imageReturnedIntent) { 
             super.onActivityResult(requestCode, resultCode, imageReturnedIntent); 

        switch(requestCode) { 
            case ACTIVITY_SELECT_IMAGE:
                if(resultCode == RESULT_OK){  
                    Uri selectedImage = imageReturnedIntent.getData();
                    mImg.setText(selectedImage.getPath());
                    InputStream imageStream;
                    try {
                        imageStream = getContentResolver().openInputStream(selectedImage);
                        Bitmap yourSelectedImage = BitmapFactory.decodeStream(imageStream);
                        mImage.setImageBitmap(yourSelectedImage);
                    } catch (FileNotFoundException e) {
                        // TODO Auto-generated catch block
                        e.printStackTrace();
                    }                   
                }
                break;
            case CAMERA_PIC_REQUEST:
                if (resultCode == RESULT_OK){
                    Uri selectedImage = imageReturnedIntent.getData();
                    mImg.setText(selectedImage.getPath());
                    InputStream imageStream;
                    try {
                        imageStream = getContentResolver().openInputStream(selectedImage);
                        Bitmap yourSelectedImage = BitmapFactory.decodeStream(imageStream);
                        mImage.setImageBitmap(yourSelectedImage);
                    } catch (FileNotFoundException e) {
                        // TODO Auto-generated catch block
                        e.printStackTrace();
                    }                   
                }
                break;
        }
    }

我看不出错误来自哪里。我按照教程执行此操作,它们看起来与我的代码相同。所以任何帮助将不胜感激。

谢谢。

4

1 回答 1

1

假设您在其他地方初始化了 mImg(因为您尝试在其上调用 setText),请尝试检查 selectedImage 是否为空。getData() 将返回 null 的事实是某些设备上的一个已知错误,我不确定它是否已修复。

于 2011-11-30T23:24:32.840 回答