我的应用程序有一个表单,需要从图库中选择图片或拍照。
为此,有两个按钮。选择图库的一个正常工作。
问题出在让你拍照的按钮上。当点击它时,它成功地打开了相机。然后,你可以拍照,然后它会问你是否要保存它。
如果你选择保存它,它将尝试返回应用程序,但它会崩溃。
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;
}
}
我不知道错误是从哪里来的。我按照教程做,但代码看起来和我的一样。所以如果可以帮忙就非常感激了。
谢谢。