Intent在Fragment中返回null onActivityResult

时间:2015-09-22 07:44:53

标签: android-fragments android-intent android-activity android-camera-intent

在Eclipse中测试了Android 4.4。

我按照developers所述的方式进行了跟踪。所以在我的片段中我说:

public void tomarfoto() {
    // Check Camera
        if (MainActivity.if_cam) {

            // create Intent to take a picture and return control to the calling application
            Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);

            Uri fileUri = getOutputMediaFileUri(MEDIA_TYPE_IMAGE); // create a file to save the image
            intent.putExtra(MediaStore.EXTRA_OUTPUT, fileUri); // set the image file name

            // start the image capture Intent
            this.startActivityForResult(intent, CAPTURE_IMAGE_ACTIVITY_REQUEST_CODE);               
        } else {
            //Toast.makeText(getActivity(), "Camera not supported", Toast.LENGTH_LONG).show();
        }
    }

之后,我设置了这个:

@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
    if (requestCode == CAPTURE_IMAGE_ACTIVITY_REQUEST_CODE) {

        if (resultCode == Activity.RESULT_OK) {
            // Image captured and saved to fileUri specified in the Intent
            Toast.makeText(getActivity(), "Image saved to:\n" +data.getData(), Toast.LENGTH_LONG).show();
        } else if (resultCode == Activity.RESULT_CANCELED) {
            // User cancelled the image capture
        } else {
            // Image capture failed, advise user
        }
        }
    }

问题是数据意图为空并引发了这一点:

  

java.lang.RuntimeException:传递结果失败   ResultInfo {who = android:fragment:0,request = 1888,result = -1,data = null}   to activity {com.myapp.MainActivity}:java.lang.NullPointerException

我不太了解,但显然onActivityResult与调用它的片段断开连接,它无法接收数据结果。我怎么解决这个问题? launchMode="singleTask"或singleInstance不是我的问题,我没有解决这个问题。

图片正确保存在fileUri所保存的目录中。帮助

0 个答案:

没有答案