本文介绍了unbindDrawables 中的异常的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我在 unbindDrawables 中遇到空指针异常,其中我正在删除所有后台可绘制对象的回调.
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) {
}
}
}
下面是日志:
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)
推荐答案
我不知道为什么你有一个空指针,但这是我用来做完全相同的事情的代码,我没有问题:).我调用垃圾收集器和检查视图中的 AdapterView/ViewGroup 实例的方式略有不同.这是我用于的原始线程这段代码
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();
}
}
这篇关于unbindDrawables 中的异常的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!