问题
I have recently updated android sdk to api 22 and android studio 1.1.0. After that I am getting rendering issues on RecyclerView. Here is what I am getting
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)
I am getting this exception on xml preview/design, so I am not able to view the xml design.
The project is working fine without any exception. Here is my widget
<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"
/>
This is my code
recyclerView= (RecyclerView) findViewById(R.id.my_recycler_view);
layoutManager=new GridLayoutManager(this,2);
recyclerView.setLayoutManager(layoutManager);
recyclerView.setHasFixedSize(true);
recyclerView.setItemAnimator(new DefaultItemAnimator());
How to solve this issue?
回答1:
My problem was solved when I took out this line from the xml file for the RecyclerView:
android:scrollbars="vertical"
I am using the following dependencies:
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'
}
EDIT: The root of this issue was pointed out by commentators on this solution - to solve the problem, you can just ensure that the LayoutManager for the RecyclerView is set before you display the RecyclerView with the scrollbars property set.
回答2:
You just forgot to set a LayoutManager: https://developer.android.com/reference/android/support/v7/widget/RecyclerView.html#setLayoutManager(android.support.v7.widget.RecyclerView.LayoutManager)
回答3:
I have had the same error. line 1216 of RecyclerView is:
return mLayout.canScrollVertically() ? mLayout.computeVerticalScrollRange(mState) : 0;
mLayout being null at the time is the issue, I am not sure why it is null, but it appears you have to call setLayoutManager() on your RecyclerView immediately after setContentView()
or inflate()
. My code was running a background thread before I tried to access the RecyclerView and call its setLayoutManager()
. Once I changed to the following, it works
i.e.
this.setContentView(R.layout.schedule);
rv = (RecyclerView) this.findViewById(R.id.schedule_listview);
rv.setLayoutManager(new LinearLayoutManager(this));
I am still not quite understand why have to do that though
回答4:
You dont need to remove any line, but to add the following line to your code:
recyclerView.setLayoutManager(new LinearLayoutManager(recyclerView.getContext()));
This way, there is no problem for the Vertical scrolling on the RecyclerView.
Hope it helps!
回答5:
Just in case people are still wondering, you should update to the latest SDK platform for API 22 via the SDK Manager, and update to the latest recycler view by updating the Support repository via the SDK Manager, and in your build.gradle
file to use the latest version (22.0.0)
回答6:
i just removed the property android:scrollbars="vertical" and everything will be fine
来源:https://stackoverflow.com/questions/29005644/rendering-problems-java-lang-nullpointerexception-at-android-support-v7-widget