安卓:无法破坏活动 [英] Android: Unable to destroy activity

查看:140
本文介绍了安卓:无法破坏活动的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用下面的code,以消除对每一个孩子的的ViewGroup:

I am using the following code to removes childs on every viewgroup:

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

where the view: R.id.RelativeLayout1 is a ListView.

不过,这样做的我都设有异常:

But doing this I have en exception:

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

我该如何解决这个问题?

How can I solve this?

推荐答案

那么,错误日志pretty的太多解释的那样:不要叫 removeAllViews()适配器视图。而你的code在某一点达到的ViewGroup 也就是适配器视图

Well, the error log pretty much explains it: do not call removeAllViews() on AdapterView. And your code at some point meets ViewGroup that also is AdapterView.

只需使用排除这种情况下出的instanceof 检查或处理异常尝试 / 包装。

Just rule this case out using instanceof check or handle exception with try/catch wrapper.

这篇关于安卓:无法破坏活动的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆