我正在使用以下代码来删除每个视图组上的子代:
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
我该如何解决?
解决方法
@H_502_15@ 那么错误日志几乎解释一下:不要在AdapterView上调用removeAllViews().而且您的代码在某种程度上符合ViewGroup,也是AdapterView.只需使用instanceof检查或使用try / catch包装器处理异常即可.