java - startAnimation(anim) 上的 NullPointerException

标签 java android nullpointerexception

我在 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/

相关文章:

android - Service.startForeground() 在使用 ServiceTestCase 运行时抛出 NullPointerException

java - Java中IP地址过滤器内存数据结构的最佳选择

java - 使用Java程序作为Java Web APP的一部分

Java TCP/IP Socket 互联网连接问题

java - HtmlUnit,如何获取没有名称属性的按钮

java - 在 ffmpeg for android studio 中的视频上叠加图像

java - 如果 equals(null) 抛出 NullPointerException 是个坏主意吗?

java - 从 kotlin Activity 调用 java 类

android - Android如何从sdcard/文件管理器加载音频文件并播放

java - 拆分为字符串数组时出现空指针异常