Glide.with((getContext())).load(user.getImgURl()).into(image_profile);

The above line is returning getContext as null hence allowing the app to crash. I have added this in a fragment under the onDataChange method as follows.

public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {

    View view=inflater.inflate(R.layout.fragment_profile, container, false);

    image_profile=view.findViewById(R.id.profile_image);
    username=view.findViewById(R.id.username);


    storageReference=FirebaseStorage.getInstance().getReference("uploads");

    fuser=FirebaseAuth.getInstance().getCurrentUser();
    reference=FirebaseDatabase.getInstance().getReference("Users").child(fuser.getUid());




    reference.addValueEventListener(new ValueEventListener() {


        @Override
        public void onDataChange(@NonNull DataSnapshot dataSnapshot) {


            User user= dataSnapshot.getValue(User.class);
            username.setText(user.getUsername());

            if(user.getImgURl()!= null && user.getImgURl().equals("default")){
                image_profile.setImageResource(R.mipmap.ic_launcher);
            }else{

            Glide.with((getContext())).load(user.getImgURl()).into(image_profile);

            }

        }

        @Override
        public void onCancelled(@NonNull DatabaseError databaseError) {

        }
    });



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


    return view;
}

The code is supposed to retrieve an image from firebase storage and display it onto the image_profile.

The following is the logcat for the error:

java.lang.NullPointerException: 

You cannot start a load on a not yet attached View or a Fragment where getActivity() returns null (which usually occurs when getActivity() is called before the Fragment is attached or after the Fragment is destroyed).

        at com.bumptech.glide.util.Preconditions.checkNotNull(Preconditions.java:31)
        at com.bumptech.glide.Glide.getRetriever(Glide.java:675)
        at com.bumptech.glide.Glide.with(Glide.java:707)
        at com.hello.khushboo.messaging.Fragments.ProfileFragment$1.onDataChange(ProfileFragment.java:110)
        at com.google.firebase.database.core.ValueEventRegistration.fireEvent(com.google.firebase:firebase-database@@16.0.5:75)
        at com.google.firebase.database.core.view.DataEvent.fire(com.google.firebase:firebase-database@@16.0.5:63)
        at com.google.firebase.database.core.view.EventRaiser$1.run(com.google.firebase:firebase-database@@16.0.5:55)
        at android.os.Handler.handleCallback(Handler.java:873)
        at android.os.Handler.dispatchMessage(Handler.java:99)
        at android.os.Looper.loop(Looper.java:193)
        at android.app.ActivityThread.main(ActivityThread.java:6669)
        at java.lang.reflect.Method.invoke(Native Method)
        at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:493)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:858)

Any help is appreciated. Thank you in advance.

Answer

Just add a check to null

if (getContext() != null) {
    Glide.with((getContext())).load(user.getImgURl()).into(image_profile);
}

And remove Firebase listener in onDestroyView() or onDestroy():

reference.removeEventListener(listener);
  • 0
Reply Report
      • 2
    • Thank you! The destroyer method allows the context to not be null. However I am still unable to view the image! Any take on that?

Instead of using getContext. Try using getActivity. Works all the time for me. Putting this in code, we have:

Glide.with((getActivity())).load(user.getImgURl()).into(image_profile);


More details

This is the relationship between an Activity and a Context:

  ? android.content.Context
      ? android.content.ContextWrapper
          ? android.view.ContextThemeWrapper
              ? android.app.Activity

From the illustration above, we can see that Activity is a type of Context and that's why it's usable here.

I hope this helps. Merry coding.

  • 0
Reply Report

Warm tip !!!

This article is reproduced from Stack Exchange / Stack Overflow, please click

Trending Tags

Related Questions