我在blah.startAnimation(anim)
上收到NullPointerException,它位于LongClickListener
内。我想要做的是在DragListener上获取GridLayout中的子项数,并在开始拖动imageview时为所有子项设置动画。由于某种原因,这会返回NullPointerException,
它的来源是:
@Override
public boolean onLongClick(View v) {
View.DragShadowBuilder shadowBuilder = new View.DragShadowBuilder(v);
v.startDrag(null, shadowBuilder, v, 0);
deleteAreaForAdapter.setVisibility(View.VISIBLE);
deleteAreaForAdapter.startAnimation(slide_in);
for(int i=0; i<middleViewForAdapter.getChildCount(); i++) {
int middleChildCount = middleViewForAdapter.getChildCount();
int middleChildCount1 = middleViewForAdapter.getChildCount();
int topChildCount = middleViewForAdapter.getChildCount();
int topChildCount1 = middleViewForAdapter.getChildCount();
int bottomChildCount = middleViewForAdapter.getChildCount();
LinearLayout topChild = (LinearLayout)topViewForAdapter.getChildAt(i);
LinearLayout topChild1 = (LinearLayout)topViewForAdapter1.getChildAt(i);
LinearLayout bottomChild = (LinearLayout)bottomViewForAdapter.getChildAt(i);
Context context = mContext;
Animation shakeAnim = AnimationUtils.loadAnimation(context, R.anim.shake);
// do stuff with child view
//ll.clearAnimation();
/*middleChild1.startAnimation(shakeAnim);
topChild.startAnimation(shakeAnim);
topChild1.startAnimation(shakeAnim);
bottomChild.startAnimation(shakeAnim);*/
if(middleChildCount > 0)
{
GridLayout hjk = middleViewForAdapter;
LinearLayout middleChild = (LinearLayout)hjk.getChildAt(i);
middleChild.startAnimation(shakeAnim);
ll.clearAnimation();
}
if(middleChildCount1 > 0)
{
GridLayout hjs = middleLayoutForAdapter1;
LinearLayout middleChild1 = (LinearLayout)hjs.getChildAt(i);
middleChild1.startAnimation(shakeAnim); //Line it's coming from!
ll.clearAnimation();
}
if(topChildCount > 0)
{
topChild.startAnimation(shakeAnim);
ll.clearAnimation();
}
if(topChildCount1 > 0)
{
topChild1.startAnimation(shakeAnim);
ll.clearAnimation();
}
}
v.setVisibility(View.INVISIBLE);
return true;
}
});
logcat的:
08-14 07:47:18.281 2078-2078/com.matt.cards.app E/AndroidRuntime﹕ FATAL EXCEPTION: main
Process: com.matt.cards.app, PID: 2078
java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.LinearLayout.startAnimation(android.view.animation.Animation)' on a null object reference
at com.matt.cards.app.DrawerLongClickListener$1.onLongClick(DrawerLongClickListener.java:169)
at android.view.View.performLongClick(View.java:4474)
at android.view.View$CheckForLongPress.run(View.java:18401)
at android.os.Handler.handleCallback(Handler.java:733)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:136)
at android.app.ActivityThread.main(ActivityThread.java:5017)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:779)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:595)
谢谢!
编辑:任何人都可以帮忙!?!?!?
答案 0 :(得分:3)
你只需要使用
Animation shakeAnim = AnimationUtils.loadAnimation(v.getContext(), R.anim.shake);
取代
Animation shakeAnim = AnimationUtils.loadAnimation(context, R.anim.shake);
答案 1 :(得分:1)
答案很简单,我只需要创建一个
的实例 每个RelativeLayout的 for(int i=0; i<middleViewForAdapter.getChildCount(); i++)
我想让它继续工作(我使用RelativeLayout.getChildAt(i);对于我所拥有的每个relativeLayout)。