作者:手机用户2502904705 | 来源:互联网 | 2023-05-18 12:59
如何解决《渲染问题:AndroidStudio1.1.0中android.support.v7.widget.RecyclerView的java.lang.NullPointerException》经验,为你挑选了4个好方法。
我最近更新了android sdk到api 22和android studio 1.1.0.之后我在RecyclerView上得到渲染问题.这是我得到的
java.lang.NullPointerException
at android.support.v7.widget.RecyclerView.computeVerticalScrollRange(RecyclerView.java:1216)
at android.view.View.onDrawScrollBars(View.java:12943)
at android.view.View.draw(View.java:15237)
at android.support.v7.widget.RecyclerView.draw(RecyclerView.java:2440)
at android.view.View.draw(View.java:15140)
at android.view.ViewGroup.drawChild_Original(ViewGroup.java:3405)
at android.view.ViewGroup_Delegate.drawChild(ViewGroup_Delegate.java:53)
at android.view.ViewGroup.drawChild(ViewGroup.java:3405)
at android.view.ViewGroup.dispatchDraw(ViewGroup.java:3198)
at android.view.View.draw(View.java:15138)
at android.view.ViewGroup.drawChild_Original(ViewGroup.java:3405)
at android.view.ViewGroup_Delegate.drawChild(ViewGroup_Delegate.java:53)
at android.view.ViewGroup.drawChild(ViewGroup.java:3405)
at android.view.ViewGroup.dispatchDraw(ViewGroup.java:3198)
at android.view.View.draw(View.java:15234)
at android.view.View.draw(View.java:15140)
at android.view.ViewGroup.drawChild_Original(ViewGroup.java:3405)
at android.view.ViewGroup_Delegate.drawChild(ViewGroup_Delegate.java:53)
at android.view.ViewGroup.drawChild(ViewGroup.java:3405)
at android.support.v4.widget.DrawerLayout.drawChild(DrawerLayout.java:1086)
at android.view.ViewGroup.dispatchDraw(ViewGroup.java:3198)
at android.view.View.draw(View.java:15138)
at android.view.ViewGroup.drawChild_Original(ViewGroup.java:3405)
at android.view.ViewGroup_Delegate.drawChild(ViewGroup_Delegate.java:53)
at android.view.ViewGroup.drawChild(ViewGroup.java:3405)
at android.view.ViewGroup.dispatchDraw(ViewGroup.java:3198)
at android.view.View.draw(View.java:15138)
at android.view.ViewGroup.drawChild_Original(ViewGroup.java:3405)
at android.view.ViewGroup_Delegate.drawChild(ViewGroup_Delegate.java:53)
at android.view.ViewGroup.drawChild(ViewGroup.java:3405)
at android.view.ViewGroup.dispatchDraw(ViewGroup.java:3198)
at android.view.View.draw(View.java:15234)
at android.view.View.draw(View.java:15140)
at android.view.ViewGroup.drawChild_Original(ViewGroup.java:3405)
at android.view.ViewGroup_Delegate.drawChild(ViewGroup_Delegate.java:53)
at android.view.ViewGroup.drawChild(ViewGroup.java:3405)
at android.view.ViewGroup.dispatchDraw(ViewGroup.java:3198)
at android.view.View.draw(View.java:15234)
我在xml预览/设计上遇到这个异常,所以我无法查看xml设计.
该项目工作正常,没有任何例外.这是我的小部件
这是我的代码
recyclerView= (RecyclerView) findViewById(R.id.my_recycler_view);
layoutManager=new GridLayoutManager(this,2);
recyclerView.setLayoutManager(layoutManager);
recyclerView.setHasFixedSize(true);
recyclerView.setItemAnimator(new DefaultItemAnimator());
如何解决这个问题?
1> Simon..:
当我从RecyclerView的xml文件中取出这一行时,我的问题解决了:
android:scrollbars="vertical"
我使用以下依赖项:
dependencies {
compile fileTree(dir: 'libs', include: ['*.jar'])
compile 'com.android.support:appcompat-v7:22.0.0'
compile 'com.android.support:recyclerview-v7:22.0.0'
compile 'com.android.support:cardview-v7:22.0.0'
}
编辑:这个问题的评论员指出了这个问题的根源 - 为了解决这个问题,你可以确保在显示带有滚动条属性集的RecyclerView之前设置了RecyclerView的LayoutManager.
这不是一个真正的解决方案.为了不崩溃,删除开发人员想要存在的东西永远不是解决方案.发生此错误的原因是显示了recyclerview但未设置布局管理器.
2> tinsukE..:
你只是忘了设置一个LayoutManager:https://developer.android.com/reference/android/support/v7/widget/RecyclerView.html#setLayoutManager( android.support.v7.widget.RecyclerView.LayoutManager
)
3> 小智..:
我有同样的错误.RecyclerView的第1216行是:
return mLayout.canScrollVertically() ? mLayout.computeVerticalScrollRange(mState) : 0;
当时mLayout为空是问题,我不知道为什么它是null,但是看起来你必须在setContentView()
或之后立即在RecyclerView上调用setLayoutManager()inflate()
.在我尝试访问RecyclerView并调用它之前,我的代码正在运行后台线程setLayoutManager()
.一旦我改为以下,它就有效
即
this.setContentView(R.layout.schedule);
rv = (RecyclerView) this.findViewById(R.id.schedule_listview);
rv.setLayoutManager(new LinearLayoutManager(this));
我仍然不太明白为什么要这样做
4> zapotec..:
您不需要删除任何行,但要在代码中添加以下行:
recyclerView.setLayoutManager(new LinearLayoutManager(recyclerView.getContext()));
这样,RecyclerView上的垂直滚动就没有问题.
希望能帮助到你!