LayoutManager上的空对象引用

时间:2016-06-20 18:33:35

标签: java android android-studio nullpointerexception android-recyclerview

尝试为我的recyclerview设置layoutmanager时出现此错误。我创建了一个布局管理器并设置我的recyclerview来使用它。由于某种原因,layoumanager是null。

java.lang.RuntimeException: Unable to start activity ComponentInfo{com.jcaseydev.popularmovies/com.jcaseydev.popularmovies.ui.ReviewsActivity}: 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
                                                                           at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2625)
                                                                           at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2686)
                                                                           at android.app.ActivityThread.-wrap12(ActivityThread.java)
                                                                           at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1440)
                                                                           at android.os.Handler.dispatchMessage(Handler.java:102)
                                                                           at android.os.Looper.loop(Looper.java:154)
                                                                           at android.app.ActivityThread.main(ActivityThread.java:5969)
                                                                           at java.lang.reflect.Method.invoke(Native Method)
                                                                           at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:801)
                                                                           at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:691)
                                                                        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
                                                                           at com.jcaseydev.popularmovies.ui.ReviewsFragment.onCreateView(ReviewsFragment.java:65)
                                                                           at android.support.v4.app.Fragment.performCreateView(Fragment.java:2074)
                                                                           at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1104)
                                                                           at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1286)
                                                                           at android.support.v4.app.BackStackRecord.run(BackStackRecord.java:758)
                                                                           at android.support.v4.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:1671)
                                                                           at android.support.v4.app.FragmentController.execPendingActions(FragmentController.java:388)
                                                                           at android.support.v4.app.FragmentActivity.onStart(FragmentActivity.java:619)
                                                                           at android.app.Instrumentation.callActivityOnStart(Instrumentation.java:1248)
                                                                           at android.app.Activity.performStart(Activity.java:6686)
                                                                           at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2588)
                                                                           at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2686) 
                                                                           at android.app.ActivityThread.-wrap12(ActivityThread.java) 
                                                                           at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1440) 
                                                                           at android.os.Handler.dispatchMessage(Handler.java:102) 
                                                                           at android.os.Looper.loop(Looper.java:154) 
                                                                           at android.app.ActivityThread.main(ActivityThread.java:5969) 
                                                                           at java.lang.reflect.Method.invoke(Native Method)

这是recylerview类。

   public ReviewsFragment(){}
RecyclerView mRecyclerView;
RecyclerView.LayoutManager linearLayoutManager;
RecyclerView.Adapter mAdapter;
private int movieId;
private List<Reviews> movieReviews = new ArrayList<>();


@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
    View rootView = inflater.inflate(R.layout.activity_review, container, false);

    FetchMovieReviews fmr = new FetchMovieReviews();
   // fmr.execute();

    //get intent
    Intent intent = getActivity().getIntent();

    //key for the intent extra
    String MOVIE_ID = "movie_id";
    if(intent != null && intent.hasExtra(MOVIE_ID)){
       movieId = intent.getIntExtra(MOVIE_ID, 49026);
    }

    mRecyclerView = (RecyclerView) rootView.findViewById(R.id.reviews_recyclerview);
    linearLayoutManager = new LinearLayoutManager(getActivity());
    mRecyclerView.setLayoutManager(linearLayoutManager);

    movieReviews.add(0, new Reviews("test", "TESTING"));
    movieReviews.add(1, new Reviews("test", "TESTING"));
    movieReviews.add(2, new Reviews("tset", "TESTING"));

    mAdapter = new MyAdapter(movieReviews);
    mRecyclerView.setAdapter(mAdapter);
    return rootView;
}

@Override
public void onAttach(Context context) {
    super.onAttach(context);



}

public class MyAdapter extends RecyclerView.Adapter<MyAdapter.ViewHolder>{
    private List<Reviews> mDataset;

    public class ViewHolder extends RecyclerView.ViewHolder{
        public TextView mTextView;
        public ViewHolder(View itemView) {
            super(itemView);
            mTextView = (TextView) itemView;
        }
    }

    public MyAdapter(List<Reviews> reviews){
        mDataset = reviews;
    }

    @Override
    public MyAdapter.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.review_list_item, parent, false);

        ViewHolder vh = new ViewHolder(view);
        return vh;
    }

    @Override
    public void onBindViewHolder(MyAdapter.ViewHolder holder, int position) {
        holder.mTextView.setText(mDataset.get(position).getAuthor());
    }

    @Override
    public int getItemCount() {
        return mDataset.size();
    }
}

我不确定问题是什么。

这是review_fragment xml

 <?xml version="1.0" encoding="utf-8"?>
<LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="wrap_content">

<android.support.v7.widget.RecyclerView
    android:layout_height="match_parent"
    android:layout_width="match_parent"
    android:id="@+id/reviews_recyclerview"/>

</LinearLayout>

这是activity_review xml

<?xml version="1.0" encoding="utf-8"?>
<FrameLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/review_activity"
android:layout_width="match_parent"
android:layout_height="match_parent">
</FrameLayout>

2 个答案:

答案 0 :(得分:3)

LayoutManager不是null,您的RecyclerView是。 您正在夸大R.layout.activity_review而不是R.layout.review_fragment

正因为如此,mRecyclerView = (RecyclerView) rootView.findViewById(R.id.reviews_recyclerview);导致mRecyclerView成为null

因此,请务必充气R.layout.review_fragment

View rootView = inflater.inflate(R.layout.review_fragment, container, false);

答案 1 :(得分:1)

在这一行中,您实际上是在夸大活动视图:

View rootView = inflater.inflate(R.layout.activity_review, container, false);

但是您要查找的回收站视图不在该布局中,而是在片段布局中。将该行更改为:

View rootView = inflater.inflate(R.layout.review_fragment, container, false);