渲染问题:Android Studio 1.1.0中android.support.v7.widget.RecyclerView的java.lang.NullPointerException

我最近更新了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) 

我得到这个exception的XML预览/devise,所以我无法查看XMLdevise。

该项目工作正常,没有任何例外。 这是我的小工具

  <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()); 

如何解决这个问题?

Solutions Collecting From Web of "渲染问题:Android Studio 1.1.0中android.support.v7.widget.RecyclerView的java.lang.NullPointerException"

当我从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' } 

编辑:解决scheme的评论员指出了这个问题的根源 – 为了解决这个问题,你可以确保RecyclerView的LayoutManager被设置,然后显示带有滚动条属性集的RecyclerView。

你只是忘了设置一个LayoutManager: https : //developer.android.com/reference/android/support/v7/widget/RecyclerView.html#setLayoutManager ( android.support.v7.widget.RecyclerView.LayoutManager )

我有同样的错误。 RecyclerView 1216行是:

 return mLayout.canScrollVertically() ? mLayout.computeVerticalScrollRange(mState) : 0; 

mLayout当时是空的问题,我不知道为什么它是空的,但它似乎你必须立即在setContentView()inflate()后在您的RecyclerView上调用setLayoutManager inflate() 。 我的代码是在我试图访问RecyclerView并调用它的setLayoutManager()之前运行后台线程。 一旦我改变到以下,它的作品

 this.setContentView(R.layout.schedule); rv = (RecyclerView) this.findViewById(R.id.schedule_listview); rv.setLayoutManager(new LinearLayoutManager(this)); 

我还是不太明白为什么要这样做

您不需要删除任何行,但要将以下行添加到您的代码中:

 recyclerView.setLayoutManager(new LinearLayoutManager(recyclerView.getContext())); 

这样,RecyclerView上的垂直滚动就没有问题了。

希望能帮助到你!

以防人们仍然想知道,你应该通过SDKpipe理器更新到API 22的最新SDK平台,并通过SDKpipe理器更新支持存储库并更新到最新的回收视图,并在你的build.gradle文件中使用最新版本(22.0.0)

我刚刚删除属性的android:scrollbars =“垂直”,一切都会好起来的