gpt4 book ai didi

java - startAnimation(anim) 上的 NullPointerException

转载 作者:塔克拉玛干 更新时间:2023-11-02 22:44:52 24 4
gpt4 key购买 nike

我在 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;
}
});

日志:

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)

谢谢!

编辑:谁能帮忙!?!?!?

最佳答案

你只需要使用

Animation shakeAnim = AnimationUtils.loadAnimation(v.getContext(), R.anim.shake);

代替

 Animation shakeAnim = AnimationUtils.loadAnimation(context, R.anim.shake);

关于java - startAnimation(anim) 上的 NullPointerException,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25302916/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com