Android 非绑定可提取项中的例外
我在unbindDrawables中得到空指针异常,我正在移除所有后台drawables上的回调Android 非绑定可提取项中的例外,android,layout,bitmap,nullpointerexception,Android,Layout,Bitmap,Nullpointerexception,我在unbindDrawables中得到空指针异常,我正在移除所有后台drawables上的回调 protected void onDestroy() { super.onDestroy(); unbindDrawables(findViewById(R.id.top_layout)); Runtime.getRuntime().gc(); } private void unbindDrawables(View view) { if (view.getBack
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) {
}
}
}
我不知道为什么会有空指针,但这是我用来做完全相同事情的代码,我没有问题:)。调用垃圾收集器和检查视图中AdapterView/ViewGroup实例的方式略有不同。这是我用于此代码的原始代码
@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();
}
}
@覆盖
受保护的空onDestroy()
{
super.ondestory();
不可绑定的绘图(findViewById(R.id.top_布局));
gc();
}
私有void未绑定可提取项(视图)
{
if(view.getBackground()!=null)
{
view.getBackground().setCallback(null);
}
if(查看视图组的实例&!(查看AdapterView的实例))
{
对于(int i=0;i<((视图组)视图)。getChildCount();i++)
{
未绑定的Drawables(((视图组)视图).getChildAt(i));
}
((视图组)视图);
}
}
System.gc();inturns调用Runtime.getRuntime().gc();我正在添加尝试捕捉处理!(查看AdapterView的实例)。。我无法准确理解wt的问题所在。AdapterView是ViewGroup的一个子类,因此它永远不会在unbindDrawables方法中引发异常:)if(view!=null){//
@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();
}
}