我有一个GridView,其中每一行都有一个自定义视图。网格视图适配器有一个保持自定义视图的数组。
点击一个按钮,我想从网格中删除一个特定的行,同时这样做我想要动画。我有一个AnimationListener。
当我从数组中移除最上面的行并在onAnimationEnd()中移除setAdapter时。它工作得很好。但是,当我删除任何其他行时,它会在主线程中产生NullPointerException。
以下是代码段:
Animation anim = AnimationUtils.loadAnimation(getApplicationContext(), R.anim.pushout);
anim.setAnimationListener(animListener);
public void removeRow() {
final CustomRowView customRowView = rowArray.get(clickedId);
customRowView.startAnimation(anim);
//Remove the row clicked
rowArray.remove(clickedId);
}
private AnimationListener animListener = new AnimationListener() {
@Override
public void onAnimationStart(Animation animation) {}
@Override
public void onAnimationRepeat(Animation animation) {}
@Override
public void onAnimationEnd(Animation animation) {
gameGrid.setAdapter(adapter);
}
}
异常线程如下:
ERROR/AndroidRuntime(11503): java.lang.NullPointerException
at android.view.ViewGroup.dispatchDraw(ViewGroup.java:1227)
at android.widget.AbsListView.dispatchDraw(AbsListView.java:1319)
at android.view.View.draw(View.java:5944)
at android.widget.AbsListView.draw(AbsListView.java:2121)
at android.view.ViewGroup.drawChild(ViewGroup.java:1486)
at android.view.ViewGroup.dispatchDraw(ViewGroup.java:1228)
at android.view.View.draw(View.java:5841)
at android.widget.FrameLayout.draw(FrameLayout.java:352)
at android.view.ViewGroup.drawChild(ViewGroup.java:1486)
at android.view.ViewGroup.dispatchDraw(ViewGroup.java:1228)
at android.view.View.draw(View.java:5841)
at android.widget.FrameLayout.draw(FrameLayout.java:352)
at android.view.ViewGroup.drawChild(ViewGroup.java:1486)
at android.view.ViewGroup.dispatchDraw(ViewGroup.java:1228)
at android.view.View.draw(View.java:5841)
at android.widget.FrameLayout.draw(FrameLayout.java:352)
at com.android.internal.policy.impl.PhoneWindow$DecorView.draw(PhoneWindow.java:1847)
at android.view.ViewRoot.draw(ViewRoot.java:1217)
at android.view.ViewRoot.performTraversals(ViewRoot.java:1030)
at android.view.ViewRoot.handleMessage(ViewRoot.java:1482)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:123)
at android.app.ActivityThread.main(ActivityThread.java:3948)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:521)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:782)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:540)
at dalvik.system.NativeStart.main(Native Method)
请帮助!!!
答案 0 :(得分:1)
这是抛出异常的android.view.ViewGroup
代码。我没有看到你的代码,但看起来当你把你的行拉出来时,你看到了返回的视图。
if ((flags & FLAG_USE_CHILD_DRAWING_ORDER) == 0) {
for (int i = 0; i < count; i++) {
final View child = children[i];
/* line 1227 throws NullPointerException */ if ((child.mViewFlags & VISIBILITY_MASK) == VISIBLE || child.getAnimation() != null) {
more |= drawChild(canvas, child, drawingTime);
}
}
} else {
for (int i = 0; i < count; i++) {
final View child = children[getChildDrawingOrder(count, i)];
if ((child.mViewFlags & VISIBILITY_MASK) == VISIBLE || child.getAnimation() != null) {
more |= drawChild(canvas, child, drawingTime);
}
}
}