相机中止时应用程序崩溃

时间:2012-11-02 09:28:50

标签: java android camera nullpointerexception

我使用此代码启动标准相机应用程序:

CAMERA_REQUEST += 1;
Intent cameraIntent = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
startActivityForResult(cameraIntent, CAMERA_REQUEST);

这段代码来获取图片:

protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        if (requestCode == CAMERA_REQUEST) {

            if (data.getAction() != null) {

                Bitmap photo = (Bitmap) data.getExtras().get("data");

                if (imageView1.getDrawable() == null) {
                    imageView1.setImageBitmap(photo);
                } else if (imageView2.getDrawable() == null) {
                    imageView2.setImageBitmap(photo);
                } else if (imageView3.getDrawable() == null) {
                    imageView3.setImageBitmap(photo);
                }

            }

        }
    }

但是当我在制作照片时在我的Android设备上按“后退”应用程序崩溃时,这是我的logcat:

当有人点击imageView时,是否可以查看完整图像?

11-02 10:21:41.883: E/AndroidRuntime(9801): FATAL EXCEPTION: main
11-02 10:21:41.883: E/AndroidRuntime(9801): java.lang.RuntimeException: Failure delivering result ResultInfo{who=null, request=3, result=0, data=null} to activity {com.obat.tabtasks/com.obat.tabtasks.photo.TakePicture}: java.lang.NullPointerException
11-02 10:21:41.883: E/AndroidRuntime(9801):     at android.app.ActivityThread.deliverResults(ActivityThread.java:2994)
11-02 10:21:41.883: E/AndroidRuntime(9801):     at android.app.ActivityThread.handleSendResult(ActivityThread.java:3037)
11-02 10:21:41.883: E/AndroidRuntime(9801):     at android.app.ActivityThread.access$1100(ActivityThread.java:128)
11-02 10:21:41.883: E/AndroidRuntime(9801):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1191)
11-02 10:21:41.883: E/AndroidRuntime(9801):     at android.os.Handler.dispatchMessage(Handler.java:99)
11-02 10:21:41.883: E/AndroidRuntime(9801):     at android.os.Looper.loop(Looper.java:137)
11-02 10:21:41.883: E/AndroidRuntime(9801):     at android.app.ActivityThread.main(ActivityThread.java:4514)
11-02 10:21:41.883: E/AndroidRuntime(9801):     at java.lang.reflect.Method.invokeNative(Native Method)
11-02 10:21:41.883: E/AndroidRuntime(9801):     at java.lang.reflect.Method.invoke(Method.java:511)
11-02 10:21:41.883: E/AndroidRuntime(9801):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:790)
11-02 10:21:41.883: E/AndroidRuntime(9801):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:557)
11-02 10:21:41.883: E/AndroidRuntime(9801):     at dalvik.system.NativeStart.main(Native Method)
11-02 10:21:41.883: E/AndroidRuntime(9801): Caused by: java.lang.NullPointerException
11-02 10:21:41.883: E/AndroidRuntime(9801):     at com.obat.tabtasks.photo.TakePicture.onActivityResult(TakePicture.java:50)
11-02 10:21:41.883: E/AndroidRuntime(9801):     at android.app.Activity.dispatchActivityResult(Activity.java:4649)
11-02 10:21:41.883: E/AndroidRuntime(9801):     at android.app.ActivityThread.deliverResults(ActivityThread.java:2990)

2 个答案:

答案 0 :(得分:2)

试试这个:

if(data != null && data.getData() != null){

}
else
{

}

答案 1 :(得分:1)

如LogCat中所指定,它是NullPointerException。数据可能为空。使用以下代码段:

        if (data != null && data.getAction() != null) {
            //....
        }