0

我有一个Fragment可以拍照的。一些片段有一张普通的图片,还有一张额外的图片。这意味着我使用两个不同RESULT CODES的照片来拍摄我正在拍摄的照片。我得到的错误只是当我使用RESULT_PICTURE_EXTRA代码时,它似乎是随机的。

为什么我在使用时会收到此错误REQUEST_PICTURE_EXTRA?另外,如果有帮助,我会在 a 中显示这张图片,PopupWindow但它没有显示出来。

片段代码

private static final int REQUEST_PICTURE = 1;
private static final int REQUEST_PICTURE_EXTRA = 2;

private void showExtraPicture() {
    ImageView extraImage = (ImageView) view.findViewById(R.id.extra_image);

    if (step.getIsReqPictureFinished()) {
        try {
            FileInputStream fis = getActivity().openFileInput(step.getExtraImageFilename());
            Bitmap imgFromFile = BitmapFactory.decodeStream(fis);
            fis.close();
            extraImage.setImageBitmap(imgFromFile);
            extraImage.invalidate();
        } 
        catch (FileNotFoundException e) { e.printStackTrace(); } 
        catch (IOException e) { e.printStackTrace(); }
    }
}

@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
    super.onActivityResult(requestCode, resultCode, data);

    if (requestCode == REQUEST_PICTURE && resultCode == Activity.RESULT_OK) {
        Bundle extras = data.getExtras();
        Bitmap image = (Bitmap) extras.get("data");

        ImageHandler imageHandler = new ImageHandler(getActivity());
        String filename = imageHandler.writeToFile(image, step.getChecklistId(), step.getOrder(), false);
        step.setImageFilename(filename);

        finishStep();
        showResult();
        checkIfAllFinished();
    }

    if (requestCode == REQUEST_PICTURE_EXTRA && resultCode == Activity.RESULT_OK) {
        Bundle extras = data.getExtras();
        Bitmap image = (Bitmap) extras.get("data");

        ImageHandler imageHandler = new ImageHandler(getActivity());
        String filename = imageHandler.writeToFile(image, step.getChecklistId(), step.getOrder(), true);
        step.setExtraImageFilename(filename);

        if (step.getReqPicture()) { step.setIsReqPictureFinished(true); }
        showExtraPicture();
        checkIfAllFinished();
    }
}

堆栈跟踪

01-31 14:42:54.192: E/AndroidRuntime(21536): FATAL EXCEPTION: main
01-31 14:42:54.192: E/AndroidRuntime(21536): java.lang.RuntimeException: Failure delivering result ResultInfo{who=android:fragment:0, request=2, result=-1, data=Intent { act=inline-data dat=content://media/external/images/media/3504 (has extras) }} to activity {com.medusa.checkit.android/com.medusa.checkit.android.StepActivity}: java.lang.NullPointerException
01-31 14:42:54.192: E/AndroidRuntime(21536):    at android.app.ActivityThread.deliverResults(ActivityThread.java:3205)
01-31 14:42:54.192: E/AndroidRuntime(21536):    at android.app.ActivityThread.handleSendResult(ActivityThread.java:3248)
01-31 14:42:54.192: E/AndroidRuntime(21536):    at android.app.ActivityThread.access$1200(ActivityThread.java:143)
01-31 14:42:54.192: E/AndroidRuntime(21536):    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1289)
01-31 14:42:54.192: E/AndroidRuntime(21536):    at android.os.Handler.dispatchMessage(Handler.java:99)
01-31 14:42:54.192: E/AndroidRuntime(21536):    at android.os.Looper.loop(Looper.java:137)
01-31 14:42:54.192: E/AndroidRuntime(21536):    at android.app.ActivityThread.main(ActivityThread.java:4950)
01-31 14:42:54.192: E/AndroidRuntime(21536):    at java.lang.reflect.Method.invokeNative(Native Method)
01-31 14:42:54.192: E/AndroidRuntime(21536):    at java.lang.reflect.Method.invoke(Method.java:511)
01-31 14:42:54.192: E/AndroidRuntime(21536):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1004)
01-31 14:42:54.192: E/AndroidRuntime(21536):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:771)
01-31 14:42:54.192: E/AndroidRuntime(21536):    at dalvik.system.NativeStart.main(Native Method)
01-31 14:42:54.192: E/AndroidRuntime(21536): Caused by: java.lang.NullPointerException
01-31 14:42:54.192: E/AndroidRuntime(21536):    at com.medusa.checkit.android.StepFragment.showExtraPicture(StepFragment.java:388)
01-31 14:42:54.192: E/AndroidRuntime(21536):    at com.medusa.checkit.android.StepFragment.onActivityResult(StepFragment.java:547)
01-31 14:42:54.192: E/AndroidRuntime(21536):    at android.app.Activity.dispatchActivityResult(Activity.java:5367)
01-31 14:42:54.192: E/AndroidRuntime(21536):    at android.app.ActivityThread.deliverResults(ActivityThread.java:3201)
01-31 14:42:54.192: E/AndroidRuntime(21536):    ... 11 more
4

1 回答 1

1

阅读您的错误消息: aNullPointerException发生在showExtraPicture(). 这就是为什么它只发生在REQUEST_PICTURE_EXTRA代码上。

你需要找出是什么null以及为什么。这是第 388 行,所以只有你知道,但我敢打赌viewnull

无论如何,您都不应该保留您创建的根目录View(假设这是view这里的参考)。Activity每次需要时从附件中找到您的意见:

getActivity().findViewById(R.id.extra_image);

或者从片段的根视图中,如果R.id.extra_image它被保存在它里面:

getView().findViewById(R.id.extra_image);

而且,作为旁注,您应该在 finally 中关闭资源:请参阅FileInputStream 文档中的示例。

于 2014-01-31T23:30:52.397 回答