问题
My app has null pointer exceptions when it tries to take a picture with the camera.
On clicked button method
public void onClick(View v) {
if (v.getId() == R.id.capture_btn) {
try {
//use standard intent to capture an image
Intent captureIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
//we will handle the returned data in onActivityResult
startActivityForResult(captureIntent, CAMERA_CAPTURE);
}
catch(ActivityNotFoundException anfe){
}
}
}
On onActivityResult method
on the activity result the image is sent to the crop operation to re-size the image
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if (resultCode == RESULT_OK) {
//user is returning from capturing an image using the camera
if(requestCode == CAMERA_CAPTURE){
//get the Uri for the captured image
picUri = data.getData();
//carry out the crop operation
performCrop();
}
//user is returning from cropping the image
else if(requestCode == PIC_CROP){
//get the returned data
Bundle extras = data.getExtras();
//get the cropped bitmap
Bitmap thePic = extras.getParcelable("data");
//retrieve a reference to the ImageView
ImageView picView = (ImageView)findViewById(R.id.imageViewMemberRegistration);
//display the returned cropped image
picView.setImageBitmap(thePic); // Line 99
}
}
}
performCrop This part helps to performs a crop operation , resize the image size and return the new size
private void performCrop(){
try {
//call the standard crop action intent (the user device may not support it)
Intent cropIntent = new Intent("com.android.camera.action.CROP");
//indicate image type and Uri
cropIntent.setDataAndType(picUri, "image/*");
//set crop properties
cropIntent.putExtra("crop", "true");
//indicate aspect of desired crop
cropIntent.putExtra("aspectX", 1);
cropIntent.putExtra("aspectY", 1);
//indicate output X and Y
cropIntent.putExtra("outputX", 256);
cropIntent.putExtra("outputY", 256);
//retrieve data on return
cropIntent.putExtra("return-data", true);
//start the activity - we handle returning in onActivityResult
startActivityForResult(cropIntent, PIC_CROP);
}
catch(ActivityNotFoundException anfe){
}
}
Below is the logcat:
09-11 17:56:46.194: E/AndroidRuntime(1293): Caused by: java.lang.RuntimeException: Failure delivering result ResultInfo{who=null, request=2, result=-1, data=Intent { act=inline-data (has extras) }} to activity {com.mobile.ppp/com.mobile.ppp.RegisterMember}: java.lang.NullPointerException
09-11 17:56:46.194: E/AndroidRuntime(1293): at android.app.ActivityThread.deliverResults(ActivityThread.java:3020)
09-11 17:56:46.194: E/AndroidRuntime(1293): at android.app.ActivityThread.performResumeActivity(ActivityThread.java:2546)
09-11 17:56:46.194: E/AndroidRuntime(1293): ... 12 more
09-11 17:56:46.194: E/AndroidRuntime(1293): Caused by: java.lang.NullPointerException
09-11 17:56:46.194: E/AndroidRuntime(1293): at com.mobile.ppp.RegisterMember.onActivityResult(RegisterMember.java:99)
09-11 17:56:46.194: E/AndroidRuntime(1293): at android.app.Activity.dispatchActivityResult(Activity.java:4108)
09-11 17:56:46.194: E/AndroidRuntime(1293): at android.app.ActivityThread.deliverResults(ActivityThread.java:3016)
09-11 17:56:46.194: E/AndroidRuntime(1293): ... 13 more
The line 99 point to this
99: picView.setImageBitmap(thePic);
回答1:
The most likely reason is because there is no ImageView in the current Activity with id R.id.imageViewMemberRegistration. If you need help troubleshooting your layout, please post the XML file for your activity. credit goes to Code-Apprentice
来源:https://stackoverflow.com/questions/25794773/null-pointer-exceptions-when-capture-image