getContext() returns null in Fragment. View seems to be unattached
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.
java android android-fragments
add a comment |
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.
java android android-fragments
add a comment |
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.
java android android-fragments
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.
java android android-fragments
java android android-fragments
asked Jan 20 at 10:07
Khushboo GandhiKhushboo Gandhi
111
111
add a comment |
add a comment |
5 Answers
5
active
oldest
votes
in fragment use getActivity() for context like this :
below code:
Glide.with(getActivity())
.load(imageUri)
.into(imageview);
add a comment |
If(isAdded()) {
Glide.with((getActivity().getContext())).load(user.getImgURl()).into(image_profile)
}
Hope it will help.
add a comment |
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);
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?
– Khushboo Gandhi
Jan 20 at 10:52
add a comment |
I hope it works.
Glide.with(view.getContext()).load(user.getImgURl()).into(image_profile);
Make sure the URL is correct when doing this.
– AOK
Jan 20 at 10:31
add a comment |
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.
add a comment |
Your Answer
StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f54275374%2fgetcontext-returns-null-in-fragment-view-seems-to-be-unattached%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
5 Answers
5
active
oldest
votes
5 Answers
5
active
oldest
votes
active
oldest
votes
active
oldest
votes
in fragment use getActivity() for context like this :
below code:
Glide.with(getActivity())
.load(imageUri)
.into(imageview);
add a comment |
in fragment use getActivity() for context like this :
below code:
Glide.with(getActivity())
.load(imageUri)
.into(imageview);
add a comment |
in fragment use getActivity() for context like this :
below code:
Glide.with(getActivity())
.load(imageUri)
.into(imageview);
in fragment use getActivity() for context like this :
below code:
Glide.with(getActivity())
.load(imageUri)
.into(imageview);
answered Jan 20 at 10:29
white hillswhite hills
866
866
add a comment |
add a comment |
If(isAdded()) {
Glide.with((getActivity().getContext())).load(user.getImgURl()).into(image_profile)
}
Hope it will help.
add a comment |
If(isAdded()) {
Glide.with((getActivity().getContext())).load(user.getImgURl()).into(image_profile)
}
Hope it will help.
add a comment |
If(isAdded()) {
Glide.with((getActivity().getContext())).load(user.getImgURl()).into(image_profile)
}
Hope it will help.
If(isAdded()) {
Glide.with((getActivity().getContext())).load(user.getImgURl()).into(image_profile)
}
Hope it will help.
answered Jan 20 at 10:21
Android KillerAndroid Killer
14k105384
14k105384
add a comment |
add a comment |
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);
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?
– Khushboo Gandhi
Jan 20 at 10:52
add a comment |
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);
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?
– Khushboo Gandhi
Jan 20 at 10:52
add a comment |
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);
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);
edited Jan 20 at 10:22
answered Jan 20 at 10:14
tim4devtim4dev
392312
392312
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?
– Khushboo Gandhi
Jan 20 at 10:52
add a comment |
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?
– Khushboo Gandhi
Jan 20 at 10:52
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?
– Khushboo Gandhi
Jan 20 at 10:52
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?
– Khushboo Gandhi
Jan 20 at 10:52
add a comment |
I hope it works.
Glide.with(view.getContext()).load(user.getImgURl()).into(image_profile);
Make sure the URL is correct when doing this.
– AOK
Jan 20 at 10:31
add a comment |
I hope it works.
Glide.with(view.getContext()).load(user.getImgURl()).into(image_profile);
Make sure the URL is correct when doing this.
– AOK
Jan 20 at 10:31
add a comment |
I hope it works.
Glide.with(view.getContext()).load(user.getImgURl()).into(image_profile);
I hope it works.
Glide.with(view.getContext()).load(user.getImgURl()).into(image_profile);
answered Jan 20 at 10:26
AOKAOK
3111
3111
Make sure the URL is correct when doing this.
– AOK
Jan 20 at 10:31
add a comment |
Make sure the URL is correct when doing this.
– AOK
Jan 20 at 10:31
Make sure the URL is correct when doing this.
– AOK
Jan 20 at 10:31
Make sure the URL is correct when doing this.
– AOK
Jan 20 at 10:31
add a comment |
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.
add a comment |
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.
add a comment |
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.
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.
answered Jan 20 at 10:38
TaslimTaslim
1,22341625
1,22341625
add a comment |
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f54275374%2fgetcontext-returns-null-in-fragment-view-seems-to-be-unattached%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown