我对Glide
有疑问。这个问题就是这样发生的:
我转到我的个人资料页面,然后点击后退按钮,然后我在Firebase数据库中插入一个对象(现在应用程序崩溃了)。问题出在我的UserProfile
活动中,但我不知道如何解决这个问题。我已经阅读了两篇类似的帖子,但我不太了解。 (Glide error after activity was destroyed and restarted和Glide 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;
}
});
}
}
感谢您的帮助
答案 0 :(得分:2)
您应该在活动onDestroy期间删除值更改侦听器。
这样的事情:
// 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) {
}
}
答案 1 :(得分:1)
当用户已经离开活动时,很可能会调用您的监听器。您需要在销毁活动时删除侦听器。或者至少在设置文本之前检查这些视图是否为空。