本文介绍了您无法在尚未连接的View或Fragment所在的getActivity()上开始加载的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
当我从配置文件fragmnett切换到另一个片段时,应用程序崩溃.我不知道该怎么解决.我不知道该怎么办,因为我是android的新手.我在等你的帮助
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
调试控制台:
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)
我的个人资料片段
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);
}
}
推荐答案
您正在尝试在片段与活动分离后加载图像.您可以通过在加载前添加 iwego 检查来解决问题片段.
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);
}
}
}
}
这篇关于您无法在尚未连接的View或Fragment所在的getActivity()上开始加载的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!