gpt4 book ai didi

android - 在启动 Activity 时在 OnDestroy 中回收位图 - 'trying to use a recycled bitmap'

转载 作者:塔克拉玛干 更新时间:2023-11-02 23:07:39 25 4
gpt4 key购买 nike

我有两个简单的 Activity AB。用户通过按下按钮从 A 启动 B,然后用户通过按下后退按钮返回到 A

在 Activity BonDestroy() 方法中,我回收了 Activity B 中使用的一些背景图像。我想了解的是,为什么当 Activity B 再次启动时,我会“尝试使用回收的位图”。肯定会在 onCreate() 方法中再次加载位图吗?就像他们一定是第一次启动 Activity 一样。

这是我的示例代码:

public class ActivityB extends Activity {

@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);
setContentView(R.layout.selectionpage);
}

@Override
public void onDestroy() {
ImageView iv = (ImageView) findViewById(R.id.imageView1);
((BitmapDrawable)iv.getDrawable()).getBitmap().recycle();
LinearLayout ll = (LinearLayout) findViewById(R.id.linearLayout1);
((BitmapDrawable)ll.getBackground()).getBitmap().recycle();
super.onDestroy();
}
}

我用来从 A 启动 Activity B 的代码

     Intent intent = new Intent(ActivityA.this, ActivityB.class);
startActivity(intent);

选择页面.XML :

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/linearLayout1"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="@drawable/backgroundimage">

<ImageView
android:id="@+id/imageView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@drawable/selectionimage"/>
</LinearLayout>

这部分可能是相关的。我不确定。我注意到在启动 Activity B 后,即使它已被销毁,在使用 MAT 分析内存堆时我仍然可以看到我的 Activity 实例。通往 GC 根的路径似乎要经过 Java.lang.Thread 和 ContextImpl。

最佳答案

您会收到该错误,因为 ImageView ivLinearLayout ll 仍指向回收的位图。您不需要在 onDestroy() 中自行回收。位图在系统不需要时将被释放。

关于android - 在启动 Activity 时在 OnDestroy 中回收位图 - 'trying to use a recycled bitmap',我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8263120/

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