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();
}
}
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?
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
.
Just rule this case out using instanceof
check or handle exception with try
/catch
wrapper.
Verify if your ViewGroup isn't a instance of AdapterView.
Do something like that:
if (!(view instanceof AdapterView<?>))
((ViewGroup) view).removeAllViews();
So, on your code:
if (view instanceof ViewGroup) {
for (int i = 0; i < ((ViewGroup) view).getChildCount(); i++) {
unbindDrawables(((ViewGroup) view).getChildAt(i));
}
if (!(view instanceof AdapterView<?>))
((ViewGroup) view).removeAllViews();
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With