【发布时间】:2015-07-13 09:54:24
【问题描述】:
我最近开始使用 RecyclerView 替换旧的 ListView。但是每当我在android.support.v7.widget.RecyclerView 元素中使用android:scrollbars="vertical" 时,我的应用程序就会崩溃并出现以下错误:
Attempt to invoke virtual method 'boolean android.support.v7.widget.RecyclerView$LayoutManager.canScrollVertically()' on a null object reference
这就是我使用 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",一切都会很好。感谢您的帮助。
【问题讨论】:
-
@JigneshJain 我确实尝试添加
android:scrollbars="vertical"并且它使我的应用程序崩溃,如问题中所述。 -
@JigneshJain 根据论坛的说法,问题出在旧版本的支持库上。我在我的项目中使用 22.0。让我试试改成21.0.2看看能不能解决问题。
-
@JigneshJain Nop。做过某事。仍然崩溃。
标签: android android-recyclerview