In my app, the user has the option of selecting an image from the photo gallery or by taking a photo with the camera. However, when the user selects a photo to use (when they tap on it), the app crashes and this error comes up in the log -
java.lang.RuntimeException: Failure delivering result ResultInfo{who=null, request=1, result=-1, data=Intent { dat=content://com.google.android.apps.photos.contentprovider/-1/1/content://media/external/images/media/46/ORIGINAL/NONE/859067274 flg=0x1 clip={text/uri-list U:content://com.google.android.apps.photos.contentprovider/-1/1/content%3A%2F%2Fmedia%2Fexternal%2Fimages%2Fmedia%2F46/ORIGINAL/NONE/859067274} }} to activity {com.veterinarysedations.georgeberdovskiy.veterinarysedations/com.veterinarysedations.georgeberdovskiy.veterinarysedations.DogDetailActivity}: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.ImageView.setImageURI(android.net.Uri)' on a null object reference
Here is my code -
public void PickImage(View view) {
AlertDialog.Builder builder = new AlertDialog.Builder(this);
final String[] weightUnitOptions = {"Camera", "Gallery"};
builder.setTitle("Pick Image from")
.setItems(weightUnitOptions, new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
if (which == 0) {
Intent takePicture = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
startActivityForResult(takePicture, 0);
} else {
Intent pickPhoto = new Intent(Intent.ACTION_PICK,
android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
startActivityForResult(pickPhoto , 1);
}
}
});
builder.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
}
});
AlertDialog dialog = builder.create();
dialog.show();
}
protected void onActivityResult(int requestCode, int resultCode, Intent imageReturnedIntent) {
super.onActivityResult(requestCode, resultCode, imageReturnedIntent);
if (resultCode != RESULT_CANCELED) {
if (requestCode == 0) {
Uri selectedImage = imageReturnedIntent.getData();
imageView.setImageURI(selectedImage);
} else if (requestCode == 1) {
Uri selectedImage = imageReturnedIntent.getData();
imageView.setImageURI(selectedImage);
} else {
super.onActivityResult(requestCode, resultCode, imageReturnedIntent);
}
}
}
Here is one of the answers on StackOverflow that I tried using -
Does anybody know what I need to do to fix this error? Thanks in advance!