Android应用程序崩溃 - 空指针异常

时间:2013-12-29 18:13:21

标签: android crash nullpointerexception android-file android-photos

我开发了一个简单的应用程序,可以从相机中捕获图像并将其保存到存储中。我无法弄清楚这里有什么问题...当用户拍照并接受它时,应用程序崩溃了。

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    this.imageView = (ImageView)this.findViewById(R.id.imageView1);
    Button photoButton = (Button) this.findViewById(R.id.button1);
    photoButton.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View v) {
            Intent cameraIntent = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE); 

            try {
                File f = createImageFile();
                photoFile = Uri.fromFile(f);
            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
            cameraIntent.putExtra(android.provider.MediaStore.EXTRA_OUTPUT,
                    photoFile);
            startActivityForResult(cameraIntent, CAMERA_REQUEST); 
        }
    });

    Button saveButton = (Button) this.findViewById(R.id.button3);
    saveButton.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View v) {
            Thread thread = new Thread(new Runnable(){
                @Override
                public void run() {
                    try {
                        save2Samba("hello", "kfir.txt");
                        //Your code goes here
                    } 
                    catch (Exception e) {
                        e.printStackTrace();
                    }
                }
            });

            thread.start();
        }
    });
}

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {  
    super.onActivityResult(requestCode, resultCode, data);
            if (requestCode == CAMERA_REQUEST && resultCode == RESULT_OK) {  
                Bitmap photo = (Bitmap) data.getExtras().get("data"); 
                imageView.setImageBitmap(photo);
            }  
    }

    private File createImageFile() throws IOException {
    // Create an image file name
    String timeStamp = new SimpleDateFormat("yyyyMMdd_HHmmss").format(new Date());
    String imageFileName = "JPEG_" + timeStamp + "_";
    File storageDir = Environment.getExternalStoragePublicDirectory(
            Environment.DIRECTORY_PICTURES);
    File image = File.createTempFile(
        imageFileName,  /* prefix */
        ".jpg",         /* suffix */
        storageDir      /* directory */
    );

    // Save a file: path for use with ACTION_VIEW intents
    mCurrentPhotoPath = "file:" + image.getAbsolutePath();
    return image;
}

logcat说

12-29 18:01:20.893: E/AndroidRuntime(8026): java.lang.RuntimeException: Failure    delivering result ResultInfo{who=null, request=1888, result=-1, data=null} to activity {com.example.hofyam/com.example.hofyam.MainActivity}: java.lang.NullPointerException

请帮忙..我被困住了。感谢

2 个答案:

答案 0 :(得分:2)

who=null, request=1888, result=-1, data=null

datanull,表示您从完成活动中收到的意图为null。所以调用data.getExtras()会导致NPE。

如果数据不为空,则必须先测试,如下所示:

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {  
    super.onActivityResult(requestCode, resultCode, data);
    if (requestCode == CAMERA_REQUEST && resultCode == RESULT_OK) {  
        if(data != null) {
            Bitmap photo = (Bitmap) data.getExtras().get("data"); 
            imageView.setImageBitmap(photo);
        }
    }  
}

修改 如果未创建图像,可能是由于您的if测试,这意味着数据仍为空。您必须在完成活动中设置data值:

Intent intent = getIntent().putExtra("data", "your data value");

为了让他们回到onActivityResult方法:

data.getExtras().getString("data");

答案 1 :(得分:1)

问题出在这里,如果活动显式返回,没有返回任何结果,或者在操作过程中崩溃,resultCode将为RESULT_CANCELED。

您应该处理可以返回null的字段,即data和resultCode。

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

  if(resultCode != RESULT_CANCELED)
     { 
      if (requestCode == CAMERA_REQUEST) {  
        if (data != null){
            Bitmap photo = (Bitmap) data.getExtras().get("data"); 
            imageView.setImageBitmap(photo);
     } 
      }
       }
        }