简体   繁体   中英

ListViewAnimation insert() NullPointerException on notifyDataSetChanged()

I'm using the ListViewAnimation library and been trying to do the add-item animation for my ListView.

In order to implement such animation, their documentation says that I should do this:

To use this functionality, simply let your adapter implement Insertable, and call one of the insert methods on the DynamicListView:

MyInsertableAdapter myAdapter = new MyInsertableAdapter(); // MyInsertableAdapter implements Insertable
mDynamicListView.setAdapter(myAdapter);
mDynamicListView.insert(0, myItem); // myItem is of the type the adapter represents.

So here's what I did:

  1. I had my Listview be a DynamicListView(I'm using ButterKnife ) and set it to use my adapter

      @InjectView(R.id.flow_list) DynamicListView mFlowList; mFlowListAdapter = new FlowListAdapter(getActivity(), mItems); mFlowList.setAdapter(mFlowListAdapter); 
  2. Made my adapter implement the 'Insertable' interface and overridden its abstract method:

     @Override public void add(int i, @NonNull Object o) { Item newItem = (Item)o; mList.add(Item); notifyDataSetChanged(); } 
  3. Call insert() on my ListView

     @OnClick(R.id.done) void addItem() { ... mFlowList.insert(0,item); } 

But when I ran my app and clicked that done button that triggers the insert() of my ListView I got a NullPointerException saying:

09-15 11:51:41.046  14660-14660/com.jezer.MyApp E/AndroidRuntime﹕ FATAL EXCEPTION: main
java.lang.NullPointerException
        at com.nhaarman.listviewanimations.itemmanipulation.animateaddition.AnimateAdditionAdapter$HeightUpdater.onAnimationUpdate(AnimateAdditionAdapter.java:352)
        at com.nineoldandroids.animation.ValueAnimator.animateValue(ValueAnimator.java:1178)
        at com.nineoldandroids.animation.ValueAnimator.animationFrame(ValueAnimator.java:1139)
        at com.nineoldandroids.animation.ValueAnimator.setCurrentPlayTime(ValueAnimator.java:545)
        at com.nineoldandroids.animation.ValueAnimator.start(ValueAnimator.java:928)
        at com.nineoldandroids.animation.ValueAnimator.start(ValueAnimator.java:951)
        at com.nineoldandroids.animation.AnimatorSet.start(AnimatorSet.java:502)
        at com.nineoldandroids.animation.AnimatorSet.start(AnimatorSet.java:502)
        at com.nhaarman.listviewanimations.itemmanipulation.animateaddition.AnimateAdditionAdapter.getView(AnimateAdditionAdapter.java:318)
        at android.widget.AbsListView.obtainView(AbsListView.java:2232)
        at android.widget.ListView.measureHeightOfChildren(ListView.java:1253)
        at android.widget.ListView.onMeasure(ListView.java:1162)
        at android.view.View.measure(View.java:15775)
        at android.widget.RelativeLayout.measureChildHorizontal(RelativeLayout.java:681)
        at android.widget.RelativeLayout.onMeasure(RelativeLayout.java:461)
        at android.view.View.measure(View.java:15775)
        at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:4942)
        at android.widget.FrameLayout.onMeasure(FrameLayout.java:310)
        at android.view.View.measure(View.java:15775)
        at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:4942)
        at android.widget.FrameLayout.onMeasure(FrameLayout.java:310)
        at android.view.View.measure(View.java:15775)
        at android.widget.LinearLayout.measureVertical(LinearLayout.java:850)
        at android.widget.LinearLayout.onMeasure(LinearLayout.java:588)
        at android.view.View.measure(View.java:15775)
        at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:4942)
        at android.widget.FrameLayout.onMeasure(FrameLayout.java:310)
        at com.android.internal.policy.impl.PhoneWindow$DecorView.onMeasure(PhoneWindow.java:2193)
        at android.view.View.measure(View.java:15775)
        at android.view.ViewRootImpl.performMeasure(ViewRootImpl.java:2212)
        at android.view.ViewRootImpl.measureHierarchy(ViewRootImpl.java:1291)
        at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:1486)
        at android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:1181)
        at android.view.ViewRootImpl$TraversalRunnable.run(ViewRootImpl.java:4942)
        at android.view.Choreographer$CallbackRecord.run(Choreographer.java:776)
        at android.view.Choreographer.doCallbacks(Choreographer.java:579)
        at android.view.Choreographer.doFrame(Choreographer.java:548)
        at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:762)
        at android.os.Handler.handleCallback(Handler.java:800)
        at android.os.Handler.dispatchMessage(Handler.java:100)
        at android.os.Looper.loop(Looper.java:194)
        at android.app.ActivityThread.main(ActivityThread.java:5391)
        at java.lang.reflect.Method.invokeNative(Native Method)
        at java.lang.reflect.Method.invoke(Method.java:525)
        at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:833)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:600)
        at dalvik.system.NativeStart.main(Native Method)

I'm not getting this exception when I commented out the notifyDataSetChanged() on the add() method. However that's the only way I know to refresh my listview and hopefully see an addition animation to it.

Are you perhaps doing this: convertView = inflater.inflate(R.layout.cell, null);

Instead of this? convertView = inflater.inflate(R.layout.page_cell, parent, false);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM