Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

NullPointerException on startAnimation(anim)

I'm getting a NullPointerException on blah.startAnimation(anim), which is inside a LongClickListener. What I'm trying to do is get the number of children in a GridLayout on a DragListener, and set an animation to all of the children when you start to drag an imageview. For some reason this returns a NullPointerException,

Where it's coming from is:

@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)

Thanks!

EDIT: Can anyone Help!?!?!?

like image 273
Groot Avatar asked Sep 30 '22 02:09

Groot


2 Answers

you just have to use

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

in place of

 Animation shakeAnim = AnimationUtils.loadAnimation(context, R.anim.shake);
like image 94
raj Avatar answered Nov 03 '22 02:11

raj


Answer was simple, I just had to create an instance of

for(int i=0; i<middleViewForAdapter.getChildCount(); i++) for every RelativeLayout I wanted it to work on (I was using RelativeLayout.getChildAt(i); for every relativeLayout I had).

like image 37
Groot Avatar answered Nov 03 '22 02:11

Groot