Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

You cannot start a load on a not yet attached View or a Fragment where getActivity()

The application crashes when I switch from the profile fragmnett to another fragment. I couldn't figure out how to solve it.I don't know what to do because I'm new at android. I'm waiting for your help

debug console:

E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.bg.askandtalk, PID: 21676
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.bg.askandtalk.Fragments.ProfileFragment$1.onDataChange(ProfileFragment.java:83)
    at com.google.android.gms.internal.firebase_database.zzfc.zza(Unknown Source:13)
    at com.google.android.gms.internal.firebase_database.zzgx.zzdr(Unknown Source:2)
    at com.google.android.gms.internal.firebase_database.zzhd.run(Unknown Source:71)
    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)

my profile fragment

public class ProfileFragment extends Fragment {

CircleImageView image_profile;
TextView username;

DatabaseReference reference;
FirebaseUser fuser;

StorageReference storageReference;
private static final int IMAGE_REQUEST = 1;
private Uri imageUri;
private StorageTask uploadTask;
private ImageButton postButton;


@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         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);
    postButton = (ImageButton) view.findViewById(R.id.post_button);

    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.getImageURL().equals("default")){
                image_profile.setImageResource(R.mipmap.ic_launcher);
            } else {
                Glide.with(getContext()).load(user.getImageURL()).into(image_profile);
            }
        }
like image 292
chapter Avatar asked Dec 09 '18 15:12

chapter


1 Answers

You are trying to load image after fragment detaches from the activity. You can fix issue by just adding isAdded check before loading fragment.

 reference.addValueEventListener(new ValueEventListener() {
        @Override
        public void onDataChange(@NonNull DataSnapshot dataSnapshot) {
            if(isAdded()){
                //Load image if the fragment is currently added to its activity.
                User user = dataSnapshot.getValue(User.class);
                username.setText(user.getUsername());
                if (user.getImageURL().equals("default")){
                    image_profile.setImageResource(R.mipmap.ic_launcher);
                } else {
                    Glide.with(getContext()).load(user.getImageURL()).into(image_profile);
                }
            }
        }
}
like image 128
Dhaval Patel Avatar answered Sep 19 '22 18:09

Dhaval Patel