异常的unbindDrawables异常、unbindDrawables

2023-09-04 12:10:32 作者:随性如我

即时得到空指针异常unbindDrawables那里IM删除回调在所有的背景可绘制。

 保护无效的onDestroy(){
    super.onDestroy();
    unbindDrawables(findViewById(R.id.top_layout));
    调用Runtime.getRuntime()GC()。
}


私人无效unbindDrawables(查看视图){
    如果(view.getBackground()!= NULL){
        view.getBackground()setCallback(空)。
    }
    如果(查看的instanceof的ViewGroup){
        的for(int i = 0;我≤((ViewGroup中)查看).getChildCount();我++){

            unbindDrawables(((ViewGroup中)查看).getChildAt(一));
        }
        尝试 {
            ((ViewGroup中)查看).removeAllViews();
        }赶上(UnsupportedOperationException异常E){

        }
    }
}
 

下面是日志:

  02-27 15:11:05.286:E / AndroidRuntime(13549):致命异常:主要
02-27 15:11:05.286:E / AndroidRuntime(13549):java.lang.RuntimeException的:无法破坏活动{com.xxx.xxx.xxx/com.xxx.xxxx.xxxActivity}:显示java.lang.NullPointerException
02-27 15:11:05.286:E / AndroidRuntime(13549):在android.app.ActivityThread.performDestroyActivity(ActivityThread.java:3655)
02-27 15:11:05.286:E / AndroidRuntime(13549):在android.app.ActivityThread.handleDestroyActivity(ActivityThread.java:3673)
02-27 15:11:05.286:E / AndroidRuntime(13549):在android.app.ActivityThread.access $ 2900(ActivityThread.java:125)
02-27 15:11:05.286:E / AndroidRuntime(13549):在android.app.ActivityThread $ H.handleMessage(ActivityThread.java:2066)
02-27 15:11:05.286:E / AndroidRuntime(13549):在android.os.Handler.dispatchMessage(Handler.java:99)
02-27 15:11:05.286:E / AndroidRuntime(13549):在android.os.Looper.loop(Looper.java:123)
02-27 15:11:05.286:E / AndroidRuntime(13549):在android.app.ActivityThread.main(ActivityThread.java:4627)
02-27 15:11:05.286:E / AndroidRuntime(13549):在java.lang.reflect.Method.invokeNative(本机方法)
02-27 15:11:05.286:E / AndroidRuntime(13549):在java.lang.reflect.Method.invoke(Method.java:521)
02-27 15:11:05.286:E / AndroidRuntime(13549):在com.android.internal.os.ZygoteInit $ MethodAndArgsCaller.run(ZygoteInit.java:860)
02-27 15:11:05.286:E / AndroidRuntime(13549):在com.android.internal.os.ZygoteInit.main(ZygoteInit.java:618)
02-27 15:11:05.286:E / AndroidRuntime(13549):在dalvik.system.NativeStart.main(本机方法)
02-27 15:11:05.286:E / AndroidRuntime(13549):由:显示java.lang.NullPointerException
02-27 15:11:05.286:E / AndroidRuntime(13549):在com.xxx.xxx.xxx.unbindDrawables(xxxActivity.java:153)
02-27 15:11:05.286:E / AndroidRuntime(13549):在com.xxx.xxx.xxx.onDestroy(xxxActivity.java:141)
02-27 15:11:05.286:E / AndroidRuntime(13549):在android.app.ActivityThread.performDestroyActivity(ActivityThread.java:3642​​)
 

解决方案

我不知道为什么你有一个空指针,但是这是我用来做同样的事情code,我不有问题:)。在这样的细微差别我称之为垃圾收集和检查适配器视图/实例的ViewGroup在视图。这是原来的thread我用这个code

  @覆盖
    保护无效的onDestroy()
    {
            super.onDestroy();

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

    私人无效unbindDrawables(查看视图)
    {
            如果(view.getBackground()!= NULL)
            {
                    view.getBackground()setCallback(空)。
            }
            如果(查看的instanceof的ViewGroup和放大器;&安培;!(查看的instanceof适配器视图))
            {
                    的for(int i = 0;我≤((ViewGroup中)查看).getChildCount();我++)
                    {
                            unbindDrawables(((ViewGroup中)查看).getChildAt(一));
                    }
                    ((ViewGroup中)查看).removeAllViews();
            }
    }
 
Picasso

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)

解决方案

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();
            }
    }