Exception in unbindDrawables

六眼飞鱼酱① 提交于 2019-12-20 10:56:07

问题


Im getting null pointer exception in unbindDrawables where im Removing callbacks on all the background drawables.

protected void onDestroy() {
    super.onDestroy();
    unbindDrawables(findViewById(R.id.top_layout));
    Runtime.getRuntime().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));
        }
        try {
            ((ViewGroup) view).removeAllViews();
        } catch (UnsupportedOperationException  e) {

        }
    }
}

Below is the log:

  02-27 15:11:05.286: E/AndroidRuntime(13549): FATAL EXCEPTION: main
02-27 15:11:05.286: E/AndroidRuntime(13549): java.lang.RuntimeException: Unable to destroy activity {com.xxx.xxx.xxx/com.xxx.xxxx.xxxActivity}: java.lang.NullPointerException
02-27 15:11:05.286: E/AndroidRuntime(13549):    at android.app.ActivityThread.performDestroyActivity(ActivityThread.java:3655)
02-27 15:11:05.286: E/AndroidRuntime(13549):    at android.app.ActivityThread.handleDestroyActivity(ActivityThread.java:3673)
02-27 15:11:05.286: E/AndroidRuntime(13549):    at android.app.ActivityThread.access$2900(ActivityThread.java:125)
02-27 15:11:05.286: E/AndroidRuntime(13549):    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2066)
02-27 15:11:05.286: E/AndroidRuntime(13549):    at android.os.Handler.dispatchMessage(Handler.java:99)
02-27 15:11:05.286: E/AndroidRuntime(13549):    at android.os.Looper.loop(Looper.java:123)
02-27 15:11:05.286: E/AndroidRuntime(13549):    at android.app.ActivityThread.main(ActivityThread.java:4627)
02-27 15:11:05.286: E/AndroidRuntime(13549):    at java.lang.reflect.Method.invokeNative(Native Method)
02-27 15:11:05.286: E/AndroidRuntime(13549):    at java.lang.reflect.Method.invoke(Method.java:521)
02-27 15:11:05.286: E/AndroidRuntime(13549):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:860)
02-27 15:11:05.286: E/AndroidRuntime(13549):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:618)
02-27 15:11:05.286: E/AndroidRuntime(13549):    at dalvik.system.NativeStart.main(Native Method)
02-27 15:11:05.286: E/AndroidRuntime(13549): Caused by: java.lang.NullPointerException
02-27 15:11:05.286: E/AndroidRuntime(13549):    at com.xxx.xxx.xxx.unbindDrawables(xxxActivity.java:153)
02-27 15:11:05.286: E/AndroidRuntime(13549):    at com.xxx.xxx.xxx.onDestroy(xxxActivity.java:141)
02-27 15:11:05.286: E/AndroidRuntime(13549):    at android.app.ActivityThread.performDestroyActivity(ActivityThread.java:3642)

回答1:


I'm not sure why you have a null pointer but this is the code that I use to do exactly the same thing, and I do not have a problem :). Slight differences in the way I call garbage collector and check for AdapterView/ViewGroup instance in the View. This is the original thread I used for this code

    @Override
    protected void onDestroy()
    {
            super.onDestroy();

            unbindDrawables(findViewById(R.id.top_layout));
            System.gc();
    }

    private void unbindDrawables(View view)
    {
            if (view.getBackground() != null)
            {
                    view.getBackground().setCallback(null);
            }
            if (view instanceof ViewGroup && !(view instanceof AdapterView))
            {
                    for (int i = 0; i < ((ViewGroup) view).getChildCount(); i++)
                    {
                            unbindDrawables(((ViewGroup) view).getChildAt(i));
                    }
                    ((ViewGroup) view).removeAllViews();
            }
    }


来源:https://stackoverflow.com/questions/9461364/exception-in-unbinddrawables

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!