我有Fragment
可以拍照。一些片段具有常规图片和额外图片。这意味着我使用两个不同的RESULT CODES
来拍摄我拍摄的照片。我得到的错误只是在我使用RESULT_PICTURE_EXTRA
代码时,它似乎是随机的。
为什么在使用REQUEST_PICTURE_EXTRA
时出现此错误?此外,如果它有帮助,我会在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
答案 0 :(得分:1)
阅读您的错误消息:NullPointerException
内发生showExtraPicture()
。这就是为什么它只发生在REQUEST_PICTURE_EXTRA
代码中。
您需要找到null
的内容以及原因。这是在第388行,所以只有你知道,但我打赌view
是null
。
无论是否,你都不应该抓住你创建的根View
(假设这是view
引用的内容)。每次需要时,只需从附件Activity
中找到您的观点:
getActivity().findViewById(R.id.extra_image);
或者片段的根视图也适用,如果其中包含R.id.extra_image
:
getView().findViewById(R.id.extra_image);
而且,作为旁注,您应该最终关闭资源:请参阅FileInputStream documentation中的示例。