NullPointerException in startAnimation (animation)

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

Where does it come from:

 @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 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!?!?!?

+5
source share
2 answers

The answer was simple, I just need to instantiate

for(int i=0; i<middleViewForAdapter.getChildCount(); i++) for each RelativeLayout I wanted it to work (I used RelativeLayout.getChildAt (i), for each relLayout that I had).

0
source

you just need to use

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

instead

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

Source: https://habr.com/ru/post/1200304/


All Articles