在片段中设置回收器视图时出现空指针异常

时间:2016-06-19 16:12:11

标签: android android-layout android-fragments android-recyclerview

我在Android应用程序的运行时遇到了这个特定的错误:

java.lang.NullPointerException: Attempt to invoke virtual method`'void android.support.v7.widget.RecyclerView.setHasFixedSize(boolean)' on a null object reference
                                                                               at com.monkeyengineer.project.MyFragment.onCreateView(MyFragment.java:36)

我已经检查过android Recycler视图和适配器文档,但它没有帮助我解决为什么会崩溃。

这是我的片段中的代码,我在容器视图上调用findViewById方法(为简洁起见,省略了import语句):

public class MyFragment extends Fragment {

    private List<Project> data;
    private RecyclerView recyclerView;
    private TextView textView;

    private boolean mIsRecyclerViewVisible = true;

    public MyFragment() {
        // Required empty public constructor
    }


    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {

        View mView = inflater.inflate(R.layout.my_fragment, container, false);
        textView = (TextView) container.findViewById(R.id.text_view);
        recyclerView = (RecyclerView) container.findViewById(R.id.recycler_view);
        recyclerView.setHasFixedSize(true);

        LinearLayoutManager mLayoutManager = new LinearLayoutManager(getContext());
        mLayoutManager.setOrientation(LinearLayoutManager.VERTICAL);
        recyclerView.setLayoutManager(mLayoutManager);

        Adapter adapter = new ProjectsAdapter(data);
        recyclerView.setAdapter(adapter);

        if (data != null) {
            textView.setVisibility(View.INVISIBLE);
            mIsRecyclerViewVisible = true;
        } else {
            recyclerView.setVisibility(View.INVISIBLE);
            mIsRecyclerViewVisible = false;
        }
        return mView;
    }

    @Override
    public void onStart() {
        super.onStart();
        if (mIsRecyclerViewVisible) {
            recyclerView.setVisibility(View.VISIBLE);
            textView.setVisibility(View.INVISIBLE);
        } else {
            recyclerView.setVisibility(View.INVISIBLE);
            textView.setVisibility(View.VISIBLE);
        }
    }

    @Override
    public void onStop() {
        super.onStop();
        mIsRecyclerViewVisible = data != null;
    }
}

这是我的片段布局:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    tools:context="com.monkeyengineer.project.MyFragment"
    android:layout_width="match_parent"
    android:layout_height="match_parent" >

    <android.support.v7.widget.RecyclerView
        android:id="@+id/recycler_view"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:scrollbars="vertical" />

    <TextView
        android:id="@+id/text_view"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:height="45dp"
        android:drawableLeft="@android:drawable/ic_input_add"
        android:drawableStart="@android:drawable/ic_input_add"
        android:drawablePadding="8dp"
        android:gravity="start|center"
        android:padding="8dp"
        android:text="Add stuff."
        android:textStyle="bold"
        android:layout_gravity="center_horizontal"
        android:layout_centerVertical="true"
        android:layout_centerHorizontal="true" />

</RelativeLayout>

那么为什么它会给出一个空指针异常,它必须是因为Recycler视图对象被赋值为空值但是我想不出原因。

感谢。

1 个答案:

答案 0 :(得分:2)

你错了。将观看次数从container扩展为mView。改变如下:

    textView = (TextView) mView.findViewById(R.id.text_view);
    recyclerView = (RecyclerView) mView.findViewById(R.id.recycler_view);