我正在使用以下代码来移除每个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。
但是这样做会导致异常:
E/AndroidRuntime(582): java.lang.RuntimeException: Unable to destroy activity {...}: java.lang.UnsupportedOperationException: removeAllViews() is not supported in AdapterView
我该如何解决这个问题?