Android 在RecyclerView中设置scrollbars属性会使我的应用程序崩溃
我最近开始使用RecyclerView替换旧的ListView。但是每当我在Android 在RecyclerView中设置scrollbars属性会使我的应用程序崩溃,android,android-recyclerview,Android,Android Recyclerview,我最近开始使用RecyclerView替换旧的ListView。但是每当我在android.support.v7.widget.RecyclerView元素中使用android:scrollbars=“vertical”时,我的应用程序就会崩溃,出现以下错误: 尝试在空对象引用上调用虚拟方法“boolean android.support.v7.widget.RecyclerView$LayoutManager.CanScrollVertical()” 这就是我使用RecyclerView的方式
android.support.v7.widget.RecyclerView
元素中使用android:scrollbars=“vertical”
时,我的应用程序就会崩溃,出现以下错误:
尝试在空对象引用上调用虚拟方法“boolean android.support.v7.widget.RecyclerView$LayoutManager.CanScrollVertical()”
这就是我使用RecyclerView的方式:
View rootView;
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
rootView = inflater.inflate(R.layout.my_fragment, container, false);
return rootView;
}
protected void onPostExecute(Boolean result) {
RecyclerView rv = (RecyclerView) rootView.findViewById(R.id.recyclerview);
LinearLayoutManager llm = new LinearLayoutManager(context);
rv.setLayoutManager(llm);
RecycleViewAdapter adapter = new RecycleViewAdapter(myRecyclerViewAdapter);
rv.setHasFixedSize(true);
rv.setAdapter(adapter);
}
这是my_fragment.xml
文件:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<android.support.v7.widget.RecyclerView
android:id="@+id/recyclerview"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:scrollbars="vertical">
</android.support.v7.widget.RecyclerView>
</LinearLayout>
如果我不使用android:scrollbars=“vertical”,一切都会非常好。感谢您的帮助。使用
RecyclerView
时,您实际上不需要指定滚动条
显示;这是LayoutManager
作业,您可以在其中指定是垂直方向还是水平方向,当然,滚动条显示将自动更改您选择的任何方向
//for vertical scrollbar and orientation
LinearLayoutManager(context, LinearLayoutManager.VERTICAL, false);
//for horizontal scrollbar and orientation
LinearLayoutManager(context, LinearLayoutManager.HORIZONTAL, false);
检查此链接@JigneshJain我确实尝试添加了
android:scrollbars=“vertical”
,它使我的应用程序崩溃,如问题中所述。检查此链接@JigneshJain根据论坛,问题在于旧版本的支持库。我在我的项目中使用22.0。让我试着把它改成21.0.2,看看它是否解决了这个问题。@JigneshJain-Nop。是我干的。仍然崩溃。我只是尝试了一下,但它仍然没有显示滚动条。@Basitseed列表有多大??它足够大,可以有一个滚动条吗?它有7个元素,是的,当内容溢出时,它足够大,可以有一个滚动条。@basitseed你的渐变中有什么?为了实现RecylerListView,我使用的SDK版本22具有以下依赖项compile'com.android.support:cardview-v7:22.0.+'compile'com.android.support:recyclerview-v7:22.0.+'compile'com.android.support:support-v4:22.0'