gpt4 book ai didi

android - 您不能在尚未附加的 View 或 getActivity() 的 fragment 上开始加载

转载 作者:太空狗 更新时间:2023-10-29 13:02:36 57 4
gpt4 key购买 nike

当我从配置文件 fragmnett 切换到另一个 fragment 时,应用程序崩溃。我不知道如何解决它。我不知道该怎么做,因为我是 android 的新手。我在等你的帮助

调试控制台:

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)

我的个人资料 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);
}
}

最佳答案

您正在尝试在 fragment 从 Activity 中分离后加载图像。您可以通过添加 isAdded 来解决问题在加载 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);
}
}
}
}

关于android - 您不能在尚未附加的 View 或 getActivity() 的 fragment 上开始加载,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53693696/

57 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com