gpt4 book ai didi

java - Glide - 应用程序因另一个 Activity 中的上下文错误而崩溃

转载 作者:行者123 更新时间:2023-12-01 21:18:06 25 4
gpt4 key购买 nike

我在使用 Glide 时遇到问题。这个问题是这样发生的:

我转到我的个人资料页面,然后单击后退按钮,然后在 Firebase 数据库中插入一个对象(现在应用崩溃了)。问题出在我的 UserProfile Activity 中,但我不知道如何解决此问题。我读过两篇类似的文章,但不太明白。 ( Glide error after activity was destroyed and restartedGlide image loading with application context )

我的logcat是这样的:

java.lang.IllegalArgumentException: You cannot start a load for a destroyed activity
at com.bumptech.glide.manager.RequestManagerRetriever.assertNotDestroyed(RequestManagerRetriever.java:134)
at com.bumptech.glide.manager.RequestManagerRetriever.get(RequestManagerRetriever.java:102)
at com.bumptech.glide.manager.RequestManagerRetriever.get(RequestManagerRetriever.java:87)
at com.bumptech.glide.Glide.with(Glide.java:629)
at gamingproject.sellmybooks.UserProfile.getUserData(UserProfile.java:114)
at gamingproject.sellmybooks.UserProfile.access$000(UserProfile.java:39)
at gamingproject.sellmybooks.UserProfile$1.onDataChange(UserProfile.java:75)
at com.google.android.gms.internal.zzaie.zza(Unknown Source)
at com.google.android.gms.internal.zzaje.zzcta(Unknown Source)
at com.google.android.gms.internal.zzajh$1.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:135)
at android.app.ActivityThread.main(ActivityThread.java:5608)
at java.lang.reflect.Method.invoke(Native Method)
at java.lang.reflect.Method.invoke(Method.java:372)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1397)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1192)

配置文件类别:

public class UserProfile extends AppCompatActivity {

Context mContext;
ImageView userProfileImage;
TextView name, mail, user_location_textView;
String tag_login;
String user_name, user_mail, user_token, user_urlPicture, user_location, user_key;
String TAG_LOGIN = "TAG_LOGIN";
String KEY = "KEY";
int REQUEST_IMAGE_CAPTURE;
DatabaseReference users;

@TargetApi(Build.VERSION_CODES.KITKAT)
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(layout.activity_user_profile);
getSupportActionBar().hide();

mContext = this;

userProfileImage = (ImageView) findViewById(R.id.imageProfile);
name = (TextView) findViewById(R.id.name);
mail = (TextView) findViewById(R.id.mail);
user_location_textView = (TextView) findViewById(R.id.user_location);

Intent intent = getIntent();
tag_login = intent.getStringExtra(TAG_LOGIN);
user_key = intent.getStringExtra(KEY);

users = FirebaseDatabase.getInstance().getReference("Users").child("User" + tag_login).child(user_key);

users.addValueEventListener(new ValueEventListener() {
@RequiresApi(api = Build.VERSION_CODES.KITKAT)
@Override
public void onDataChange(DataSnapshot dataSnapshot) {
getUserData(dataSnapshot);
}
@Override
public void onCancelled(DatabaseError databaseError) {
}
});

}


///Get user's data with the key///

@TargetApi(Build.VERSION_CODES.KITKAT)
@RequiresApi(api = Build.VERSION_CODES.KITKAT)
private void getUserData(DataSnapshot dataSnapshot){
if (Objects.equals(tag_login, "App")){
UserApp userApp = dataSnapshot.getValue(UserApp.class);
user_name = userApp.user_name;
user_mail = userApp.user_mail;
user_token = userApp.user_token;
user_location = userApp.user_location;
user_urlPicture = userApp.user_bitmapImage;
name.setText(user_name);
mail.setText(user_mail);
user_location_textView.setText(user_location);
if (Objects.equals(user_urlPicture, "NoImage")){
userProfileImage.setImageResource(drawable.noimage);
Toast.makeText(mContext, "Click on image to change it", Toast.LENGTH_LONG).show();
} else {
Glide.with(mContext).load(user_urlPicture).into(userProfileImage);
}
userProfileImage.setOnTouchListener(new View.OnTouchListener() {
@Override
public boolean onTouch(View v, MotionEvent event) {
Intent takePictureIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
if (takePictureIntent.resolveActivity(getPackageManager()) != null) {
REQUEST_IMAGE_CAPTURE = 1;
startActivityForResult(takePictureIntent, REQUEST_IMAGE_CAPTURE);
}
return false;
}
});


}

}

感谢您的帮助

最佳答案

您应该在 onDestroy Activity 期间删除值更改监听器。

类似这样的事情:

// during onCreate(Bundle)
users.addValueEventListener(listener);

// then during onDestroy()
users.removeValueEventListener(listener);

// outside any method you write the listener
private ValueEventListener listener = new ValueEventListener() {
@RequiresApi(api = Build.VERSION_CODES.KITKAT)
@Override
public void onDataChange(DataSnapshot dataSnapshot) {
getUserData(dataSnapshot);
}
@Override
public void onCancelled(DatabaseError databaseError) {
}
}

关于java - Glide - 应用程序因另一个 Activity 中的上下文错误而崩溃,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39600601/

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