Exception in unbindDrawables

本文探讨了在Android Activity的onDestroy方法中调用自定义的unbindDrawables方法时出现的NullPointerException问题,并提供了一段修改后的代码示例来解决这个问题。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

问题描述:

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) {

        }
    }
}
error log is following:

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);//当Activity销毁时,设定存储的Drawable的callback为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();
            }
    }


转自: http://stackoverflow.com/questions/9461364/exception-in-unbinddrawables

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值