Android: Unable to destroy activity

后端 未结 4 1852
野的像风
野的像风 2021-01-04 13:31

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

protected void onDestroy() {
    super.onDestroy();
    this.liberarMemoria();
}

public          


        
相关标签:
4条回答
  • 2021-01-04 13:37

    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.

    0 讨论(0)
  • 2021-01-04 13:40

    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.

    0 讨论(0)
  • 2021-01-04 13:45

    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.

    0 讨论(0)
  • 2021-01-04 13:57

    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();
    }
    
    0 讨论(0)
提交回复
热议问题