RecyclerView列表滑动空指针问题

当RecyclerView在快速下拉并分页加载大量数据时,可能出现空指针异常。通过关闭RecyclerView的更新动画可以有效解决此问题。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

RecyclerView快速下拉,同时在分页加载数据,当数据很多时,则会出现空指针问题

java.lang.NullPointerException: Attempt to invoke virtual method 'android.view.ViewGroup$LayoutParams android.view.View.getLayoutParams()' on a null object reference
  	at android.support.v7.widget.ac$2.a(SourceFile:395)
  	at android.support.v17.leanback.widget.GridLayoutManager.c(SourceFile:1023)
  	at android.support.v17.leanback.widget.GridLayoutManager$2.b(SourceFile:1587)
  	at android.support.v17.leanback.widget.u.b(SourceFile:388)
  	at android.support.v17.leanback.widget.GridLayoutManager.A(SourceFile:1681)
  	at android.support.v17.leanback.widget.GridLayoutManager.y(SourceFile:2065)
  	at android.support.v17.leanback.widget.GridLayoutManager.scrollVerticallyBy(SourceFile:2010)
  	at android.support.v7.widget.RecyclerView$s.run(SourceFile:4413)
  	at android.view.Choreographer$CallbackRecord.run(Choreographer.java:775)
  	at android.view.Choreographer.doCallbacks(Choreographer.java:588)
  	at android.view.Choreographer.doFrame(Choreographer.java:557)
  	at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:761)
  	at android.os.Handler.handleCallback(Handler.java:739)
  	at android.os.Handler.dispatchMessage(Handler.java:95)
  	at android.os.Looper.loop(Looper.java:135)
  	at android.app.ActivityThread.main(ActivityThread.java:5276)
  	at java.lang.reflect.Method.invoke(Native Method)
  	at java.lang.reflect.Method.invoke(Method.java:372)
  	at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:960)
  	at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:755)`

解决方法:关闭RecyclerView更新动画

recyclerView.getItemAnimator().setAddDuration(0);
recyclerView.getItemAnimator().setChangeDuration(0);
recyclerView.getItemAnimator().setMoveDuration(0);
recyclerView.getItemAnimator().setRemoveDuration(0);
((SimpleItemAnimator) RecyclerView.getItemAnimator()).setSupportsChangeAnimations(false);
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值