我最近将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设计。
项目工作正常,没有任何例外。 这是我的小部件
<android.support.v7.widget.RecyclerView
android:id="@+id/my_recycler_view"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="8"
android:layout_gravity="center"
android:gravity="center"
android:scrollbars="vertical"
android:fadeScrollbars="false"
/>
这是我的代码
recyclerView= (RecyclerView) findViewById(R.id.my_recycler_view);
layoutManager=new GridLayoutManager(this,2);
recyclerView.setLayoutManager(layoutManager);
recyclerView.setHasFixedSize(true);
recyclerView.setItemAnimator(new DefaultItemAnimator());
如何解决这个问题?
答案 0 :(得分:31)
当我从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。
答案 1 :(得分:11)
您忘了设置LayoutManager: https://developer.android.com/reference/android/support/v7/widget/RecyclerView.html#setLayoutManager(android.support.v7.widget.RecyclerView.LayoutManager)
答案 2 :(得分:6)
我遇到了同样的错误。 RecyclerView第1216行是:
return mLayout.canScrollVertically() ? mLayout.computeVerticalScrollRange(mState) : 0;
当时mLayout为空是问题,我不知道为什么它是null,但是看起来你必须在setContentView()
或inflate()
之后立即在RecyclerView上调用setLayoutManager()。在我尝试访问RecyclerView并调用其setLayoutManager()
之前,我的代码正在运行后台线程。一旦我改为以下,它就可以了
即。
this.setContentView(R.layout.schedule);
rv = (RecyclerView) this.findViewById(R.id.schedule_listview);
rv.setLayoutManager(new LinearLayoutManager(this));
我仍然不太明白为什么要这样做
答案 3 :(得分:5)
您不需要删除任何行,只需在代码中添加以下行:
recyclerView.setLayoutManager(new LinearLayoutManager(recyclerView.getContext()));
这样,RecyclerView上的垂直滚动没有问题。
希望它有所帮助!
答案 4 :(得分:0)
万一人们仍然想知道,您应该通过SDK Manager更新到API 22的最新SDK平台,并通过SDK Manager更新支持存储库,并在{{1}中更新到最新的回收站视图文件使用最新版本(22.0.0)
答案 5 :(得分:0)
我刚删除了属性android:scrollbars =“vertical”,一切都会很好