Search code examples
androidandroid-imageviewandroid-gallery

Blank Screen showing up after Image Selected from Gallery


I am trying to get an image into the ImageView but whenever I click a picture on the Gallery the app shows a blank screen and closes down. This ImageView is in a fragment

public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { View v = inflater.inflate(R.layout.fragment_auction_add, container, false);

    auction_add_imageViewButton = v.findViewById(R.id.auction_add_imageView_button);

    auction_add_imageViewButton.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            imageSelectAction(v);
        }
    });


    dateTimeAction(v);


    return v;
}

private void imageSelectAction(View v) {
    auction_add_imageViewButton = v.findViewById(R.id.auction_add_imageView_button);
    auction_add_image = v.findViewById(R.id.auction_add_imageView);

    choosePicture();

}

private void choosePicture() {
    Intent intent = new Intent();
    intent.setType("image/*");
    intent.setAction(Intent.ACTION_GET_CONTENT);
    startActivityForResult(Intent.createChooser(intent, "Select Picture"), SELECT_PICTURE);
}

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

    if (resultCode == Activity.RESULT_OK) {
        if (requestCode == SELECT_PICTURE) {
            if (data != null && data.getData() != null) {
                Uri selectedImageUri = data.getData();

                auction_add_image.setImageURI(selectedImageUri);
            }
        }
    }
}

The Inital Declaration

public class auction_add extends Fragment {


    private static final int SELECT_PICTURE = 10;
    private static final int PERMISSION_CODE = 11;
    ImageButton auction_add_imageViewButton;


    //References to all Auction Add Page EditText
    EditText auction_add_itemNameTxt;
    EditText auction_add_descriptionTxt;
    EditText auction_add_initialPriceTxt;
    EditText auction_add_startTimeTxt;
    EditText auction_add_endTimeTxt;

    //Reference to ImageView
    private ImageView auction_add_image;

    final Calendar myCalendar = Calendar.getInstance();

Would Appreciate the help thanks!

Edit:

Followed is the error on the Run tab of Android Studio

W/System: A resource failed to call close. 
D/AndroidRuntime: Shutting down VM
E/AndroidRuntime: FATAL EXCEPTION: main
    Process: com.example.projectcrest, PID: 5618
    java.lang.RuntimeException: Failure delivering result ResultInfo{who=null, request=242868079, result=-1, data=Intent { dat=content://com.android.providers.media.documents/document/image:31 flg=0x1 }} to activity {com.example.projectcrest/com.example.projectcrest.pages.LandingPage}: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.ImageView.setImageURI(android.net.Uri)' on a null object reference
        at android.app.ActivityThread.deliverResults(ActivityThread.java:5015)
        at android.app.ActivityThread.handleSendResult(ActivityThread.java:5056)
        at android.app.servertransaction.ActivityResultItem.execute(ActivityResultItem.java:51)
        at android.app.servertransaction.TransactionExecutor.executeCallbacks(TransactionExecutor.java:135)
        at android.app.servertransaction.TransactionExecutor.execute(TransactionExecutor.java:95)
        at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2066)
        at android.os.Handler.dispatchMessage(Handler.java:106)
        at android.os.Looper.loop(Looper.java:223)
        at android.app.ActivityThread.main(ActivityThread.java:7656)
        at java.lang.reflect.Method.invoke(Native Method)
        at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:592)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:947)
     Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.ImageView.setImageURI(android.net.Uri)' on a null object reference
        at com.example.projectcrest.fragments.auction_add.onActivityResult(auction_add.java:155)
        at androidx.fragment.app.FragmentManager$9.onActivityResult(FragmentManager.java:2905)
        at androidx.fragment.app.FragmentManager$9.onActivityResult(FragmentManager.java:2885)
        at androidx.activity.result.ActivityResultRegistry.doDispatch(ActivityResultRegistry.java:377)
        at androidx.activity.result.ActivityResultRegistry.dispatchResult(ActivityResultRegistry.java:336)
        at androidx.activity.ComponentActivity.onActivityResult(ComponentActivity.java:624)
        at androidx.fragment.app.FragmentActivity.onActivityResult(FragmentActivity.java:164)
        at android.app.Activity.dispatchActivityResult(Activity.java:8310)
        at android.app.ActivityThread.deliverResults(ActivityThread.java:5008)
        at android.app.ActivityThread.handleSendResult(ActivityThread.java:5056) 
        at android.app.servertransaction.ActivityResultItem.execute(ActivityResultItem.java:51) 
        at android.app.servertransaction.TransactionExecutor.executeCallbacks(TransactionExecutor.java:135) 
        at android.app.servertransaction.TransactionExecutor.execute(TransactionExecutor.java:95) 
        at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2066) 
        at android.os.Handler.dispatchMessage(Handler.java:106) 
        at android.os.Looper.loop(Looper.java:223) 
        at android.app.ActivityThread.main(ActivityThread.java:7656) 
        at java.lang.reflect.Method.invoke(Native Method) 
        at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:592) 
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:947) 
I/Process: Sending signal. PID: 5618 SIG: 9

Solution

  • By looking into the exception it seems that the App is crashing in #onActivityResult function at line auction_add_image.setImageURI(selectedImageUri); due to NPE (Null Pointer Exception). This expection/crash is caused since the auction_add_image object has null value instead of an ImageView.

    After taking an close look into your code, I think the issue has occured due to following code:

        auction_add_imageViewButton = v.findViewById(R.id.auction_add_imageView_button);
    
        auction_add_imageViewButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                imageSelectAction(v);
            }
        });
    

    Note that in #onClick function you are passing view v on which you are going to perform the #findViewById operation. Here the passed view v refers to the auction_add_imageViewButton instead of the v you have captured above.

    I would suggest to modify the code as below (just rename the v variable in #onClick function):

        auction_add_imageViewButton = v.findViewById(R.id.auction_add_imageView_button);
    
        auction_add_imageViewButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View buttonView) {
                imageSelectAction(v);
            }
        });
    

    This way the variable name collision could be avoided and the error would be resolved.

    Also, in case this does not solve your problem, then please check whether the #findViewById is returning an ImageView instead of null. In case the Id provided to #findViewById is not present on the screen or in the child views of view (on which the #findViewById) is being performed then the #findViewById will return null value.

    Also, check whether you have assigned null value to auction_add_image in anywhere your code.