Android: Unable to destroy activity
Asked Answered
L

4

10

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?

Lovins answered 6/12, 2011 at 19:17 Comment(0)
S
11

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.

Symmetry answered 6/12, 2011 at 19:25 Comment(2)
Any idea why the AddapterView doesn't support this operation? I couldn't find anything in official reference of AdapterView about it.Chambermaid
@r1k0, Yes, that's because AdapterView manages its children internally. You cannot add/remove them as this might break its internal state.Symmetry
G
7

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();
}
Gibun answered 24/1, 2013 at 12:48 Comment(0)
T
0

Remove that line? Or at least check if the operation is supported with try and catch.

Also, it is a bit confusing to want to do this at all in a method called unbindDrawables, unless it is just a badly named method (doesn't describe what it does fully).

Are you calling all of this in onDestroy? If so, is there benefit from doing this? I was under the impression that the system takes care of this sort of thing for you.

Thorton answered 6/12, 2011 at 19:24 Comment(2)
onDestroy call "liberarMemoria()" and this call "unbindDrawables(..)". I must do it manually to ensure the destroy of Bitmap.Lovins
Right... but removeAllViews isn't necessary and is safe to remove hereThorton
C
0

Don't call it. UnsupportedOperationException is telling you that this method is not supported or functional so you'll have to accomplish the task another way. I don't see the need in calling this anyway as the garbage collector will handle this task. Bitmap recycling should be done manually if you need to ensure it being done.

Crippen answered 6/12, 2011 at 19:29 Comment(2)
I want to recycle Bitmap and i must do manually, but what is the best way?Lovins
I can't tell you the best way without seeing all your source but you should do it in onDestroy() and keep track of all Bitmap objects used so you can recycle() them.Crippen

© 2022 - 2024 — McMap. All rights reserved.