GridView 行删除和动画 - Android
我有一个 GridView,其中每一行都有一个自定义视图。网格视图适配器有一个保存自定义视图的数组。
单击按钮时,我想从网格中删除特定行,并且在执行此操作时我想要在其上添加动画。我有一个动画监听器。
当我从数组中删除最上面的行并在 onAnimationEnd() 中设置适配器时。它工作得很好。但是,当我删除任何其他行时,它会在主线程中给出 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)
请帮忙!!!
I have a GridView in which each row has a custom view. The grid view adapter has an array that keeps the custom view.
At click of a button, I want to remove a specific row from the Grid and while doing so I want animation on it. I have an AnimationListener.
When I remove the upper most row from the array and setAdapter in onAnimationEnd(). It works perfectly fine. But, when I remove any other row, it gives a NullPointerException in the main thread.
Here is the code snippet:
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);
}
}
The exception thread is as follows:
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)
Please help!!!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
这是抛出异常的
android.view.ViewGroup
代码。我没有看到您的代码,但看起来当您拉出该行时,您返回了空视图。Here's
android.view.ViewGroup
code that throws exception. I don't see your code but it looks like when you yank out the row you have null View returned.