я следующий код в моей onCreate
функции моей деятельности:Невозможно установитьLayoutManager в RecyclerView. Отображение нулевого указателя на RecyclerView
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
LinearLayoutManager manager = new LinearLayoutManager(this);
manager.setOrientation(LinearLayoutManager.VERTICAL);
RecyclerView recyclerView = (RecyclerView) findViewById(R.id.recycler_view);
recyclerView.setLayoutManager(manager);
setContentView(R.layout.my_layout);
// some more code
и my_layout.xml
выглядит следующим образом:
<?xml version="1.0" encoding="utf-8"?>
<android.support.design.widget.CoordinatorLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="@+id/annonce.main.coordinator"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:ignore="RtlHardcoded">
<android.support.design.widget.AppBarLayout...>
<android.support.v7.widget.RecyclerView
android:id="@+id/recycler_view"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:scrollbars="none"
app:behavior_overlapTop="78dp"
android:fillViewport="true"
android:layout_gravity="fill_vertical"
app:layout_behavior="@string/appbar_scrolling_view_behavior">
<android.support.v7.widget.CardView
xmlns:card_view="http://schemas.android.com/apk/res-auto"
android:id="@+id/card_view"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginLeft="8dp"
android:layout_marginRight="8dp"
android:layout_marginBottom="8dp"
card_view:cardCornerRadius="4dp">
<ListView
android:id="@+id/listView"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:choiceMode="singleChoice"
android:scrollbars="vertical"
app:behavior_overlapTop="78dp"
app:layout_behavior="@string/appbar_scrolling_view_behavior">
</ListView>
</android.support.v7.widget.CardView>
</android.support.v7.widget.RecyclerView>
</android.support.design.widget.CoordinatorLayout>
Я получаю сообщение об ошибке:
Caused by: java.lang.NullPointerException: Attempt to invoke virtual method void
android.support.v7.widget.RecyclerView.setLayoutManager(android.support.v7.widget.RecyclerView$LayoutManager) on a null object reference` in the line recyclerView.setLayoutManager(manager);
Я попытался выполнить аналогичные запросы в предыдущих статьях sof, но ни один из них не выглядит d, чтобы решить мою проблему. Не могли бы вы рассказать мне, где я ошибаюсь в своем коде? Благодарю.
движение 'setContentView (R.layout.my_layout);' к началу после 'super.onCreate (savedInstanceState);' –
Это бросает ошибку 'Вызванный: java.lang.IllegalStateException: RecyclerView не имеет LayoutManager', который является очень я до этого добавил код LinearLayoutManager. –
Извините, я только что заметил. Ваш макет неправильный. RecyclerView может содержать только представления, заполненные ** RecylerViewAdapter **. Проверьте эту ссылку, чтобы узнать, как использовать RecyclerView http://code.tutsplus.com/tutorials/getting-started-with-recyclerview-and-cardview-on-android--cms-23465 –