无法将setLayoutManager设置为RecyclerView。在RecyclerView上显示空指针

时间:2016-02-17 11:34:10

标签: android nullpointerexception android-recyclerview

我的活动的onCreate功能中包含以下代码:

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

我确实在以前的软件文章中尝试过类似的查询,但它们似乎都没有解决我的问题。你能告诉我我的代码在哪里出错吗?感谢。

3 个答案:

答案 0 :(得分:0)

您在设置布局之前尝试查找RecyclerView。这样做

protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.my_layout); // setting the layout first

    LinearLayoutManager manager = new LinearLayoutManager(this);
    manager.setOrientation(LinearLayoutManager.VERTICAL);
    RecyclerView recyclerView = (RecyclerView) findViewById(R.id.recycler_view);
    recyclerView.setLayoutManager(manager);
}

答案 1 :(得分:0)

为什么在RecyclerView中有一个CardView和一个listView?我从未见过这样的布局。您的布局有问题。

尝试从布局文件中删除recyclerView的子项。

答案 2 :(得分:0)

1)像#34; @Much Overflow&#34;回答,将您的onCreate()方法更改为:

protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.my_layout); // setting the layout first

    LinearLayoutManager manager = new LinearLayoutManager(this);
    manager.setOrientation(LinearLayoutManager.VERTICAL);
    RecyclerView recyclerView = (RecyclerView) findViewById(R.id.recycler_view);
    recyclerView.setLayoutManager(manager);
}

2)RecyclerView xml中删除多余的孩子。

您的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.RecyclerView>
</android.support.design.widget.CoordinatorLayout>

如果您想在RecyclerView中添加观看次数,则应使用Adapter