gpt4 book ai didi

java - LayoutManager 上的空对象引用

转载 作者:行者123 更新时间:2023-11-29 08:43:53 27 4
gpt4 key购买 nike

我在尝试为我的 recyclerview 设置布局管理器时遇到此错误。我已经创建了一个布局管理器并设置了我的 recyclerview 来使用它。由于某种原因,layoumanager 为空。

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>

最佳答案

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

因此,mRecyclerView = (RecyclerView) rootView.findViewById(R.id.reviews_recyclerview); 导致 mRecyclerViewnull

因此,请确保膨胀 R.layout.review_fragment:

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

关于java - LayoutManager 上的空对象引用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37929469/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com