gpt4 book ai didi

java - 在 Fragment 中使用 Glide 以及 FirebaseDatabase 和 Storage 会导致 Glide.with(getActivity) Null 对象

转载 作者:行者123 更新时间:2023-12-02 11:41:48 27 4
gpt4 key购买 nike

我正在创建一个应用程序,让用户通过按 ImageView 来更改他/她的图像,该图像将来自他/她的图库并将上传到我的 FirebaseStorage 中。用户的个人详细信息单独上传到 FirebaseDatabase 中,然后添加 ImageUrl。在我选择图像后,我收到错误 java.lang.NullPointerException: Attempt to invoke virtual method 'boolean android.app.Activity.isDestroyed()' on a null object reference从图库中继续上传图像,但即使出现错误,图像仍会上传到我的 FirebaseStorage 中,并且 ImageUrl 会在用户的详细信息中更新。重新启动应用程序实际上会显示我刚刚上传的图像。错误在 Glide.with(getActivity()) 部分指出。我的 FirebaseDatabase.addValueEventListener 中有我的 Glide.with。我该如何解决这个问题?

这是我的onCreateViewonRequestPermissionsResultonActivityResult的部分代码

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
FrameLayout rootView = (FrameLayout)inflater.inflate(R.layout.fragment_tch_profile, container, false);

mDataRef.addValueEventListener(new ValueEventListener() {
@Override
public void onDataChange(DataSnapshot dataSnapshot) {
{
tv_email.setText(dataSnapshot.child("email").getValue(String.class));
tv_name.setText(dataSnapshot.child("name").getValue(String.class));
tv_gs.setText(dataSnapshot.child("cassSection").getValue(String.class));
if(dataSnapshot.hasChild("tImage")){
Glide.with(getActivity())
.load(dataSnapshot.child("tImage").getValue(String.class))
.crossFade()
.placeholder(R.mipmap.ic_loader)
.thumbnail(0.1f)
.diskCacheStrategy(DiskCacheStrategy.ALL)
.into(iv_image);
}

}
else {
}
}

@Override
public void onCancelled(DatabaseError databaseError) {
Toast.makeText(getActivity(),"The read failed: " + databaseError.getCode(),Toast.LENGTH_SHORT).show();
}
});

return rootView;
}

@Override
public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
super.onRequestPermissionsResult(requestCode, permissions, grantResults);

switch (requestCode) {
case READ_EXTERNAL_STORAGE:
if (grantResults.length > 0 && grantResults[0] == PackageManager.PERMISSION_GRANTED)
callGallery();
return;
}
}

@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);

if (requestCode == GALLERY_INTENT && resultCode == RESULT_OK) {

mImageURI = data.getData();
iv_image.setImageURI(mImageURI);
StorageReference filePath = mStorageRef.child("userImage").child(mImageURI.getLastPathSegment());

filePath.putFile(mImageURI).addOnSuccessListener(new OnSuccessListener<UploadTask.TaskSnapshot>() {
@Override
public void onSuccess(UploadTask.TaskSnapshot taskSnapshot) {

Uri downloadUri = taskSnapshot.getDownloadUrl(); //Ignore This error

mDataRef.child("tImage").setValue(downloadUri.toString());

}
});
}
}

private void callGallery() {
Intent i = new Intent(Intent.ACTION_PICK);
i.setType("image/*");
startActivityForResult(i, GALLERY_INTENT);
}

这是 Logcat:

FATAL EXCEPTION: main
Process: kayaba.akihiro.educ_games, PID: 4502
java.lang.NullPointerException: Attempt to invoke virtual method 'boolean android.app.Activity.isDestroyed()' on a null object reference
at com.bumptech.glide.manager.RequestManagerRetriever.assertNotDestroyed(RequestManagerRetriever.java:133)
at com.bumptech.glide.manager.RequestManagerRetriever.get(RequestManagerRetriever.java:102)
at com.bumptech.glide.Glide.with(Glide.java:653)
at kayaba.akihiro.educ_games.TchProfile$3.onSuccess(TchProfile.java:169)
at kayaba.akihiro.educ_games.TchProfile$3.onSuccess(TchProfile.java:161)
at com.google.firebase.storage.zzi.zzi(Unknown Source)
at com.google.firebase.storage.zzz.run(Unknown Source)
at android.os.Handler.handleCallback(Handler.java:739)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:148)
at android.app.ActivityThread.main(ActivityThread.java:5417)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)

最佳答案

根据 @ADM 评论,我尝试在 fragment 分离时删除 ValueEventListener,它确实解决了问题。

以下是添加的代码以供将来引用:

ValueEventListener mListener;
DatabaseReference mDataRef;

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
FrameLayout rootView = (FrameLayout)inflater.inflate(R.layout.fragment_tch_profile, container, false);

mListener = mDataRef.addValueEventListener(new ValueEventListener() {
@Override
public void onDataChange(DataSnapshot dataSnapshot) {
{
//SOME CODES HERE
}

@Override
public void onCancelled(DatabaseError databaseError) {
Toast.makeText(getActivity(),"The read failed: " + databaseError.getCode(),Toast.LENGTH_SHORT).show();
}
});

return rootView;
}


@Override
public void onDetach() {
super.onDetach();
if(mDataRef!=null && mListener!=null){
mDataRef.removeEventListener(mListener);
}
}

关于java - 在 Fragment 中使用 Glide 以及 FirebaseDatabase 和 Storage 会导致 Glide.with(getActivity) Null 对象,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48487617/

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