Android:无法销毁活动

2023-11-24

我使用以下代码来删除每个视图组上的子项:

protected void onDestroy() {
    super.onDestroy();
    this.liberarMemoria();
}

public void liberarMemoria(){
     imagenes.recycleBitmaps(); 
     this.unbindDrawables(findViewById(R.id.RelativeLayout1));
     System.gc();
}
private void unbindDrawables(View view) {
    if (view.getBackground() != null) {
    view.getBackground().setCallback(null);
}
if (view instanceof ViewGroup) {
    for (int i = 0; i < ((ViewGroup) view).getChildCount(); i++) {
        unbindDrawables(((ViewGroup) view).getChildAt(i));
    }
    ((ViewGroup) view).removeAllViews();
    }
}

其中视图:R.id.RelativeLayout1 是一个 ListView。

但这样做我有一个例外:

E/AndroidRuntime(582): java.lang.RuntimeException: Unable to destroy activity {...}: java.lang.UnsupportedOperationException: removeAllViews() is not supported in AdapterView

我该如何解决这个问题?


好吧,错误日志几乎解释了这一点:不要调用removeAllViews() on AdapterView。并且您的代码在某些时候满足ViewGroup那也是AdapterView.

只需使用以下方法排除这种情况instanceof检查或处理异常try/catch包装纸。

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

Android:无法销毁活动 的相关文章

随机推荐