我正在创建一个在回收者视图中显示视频的应用程序,我已经可以将这些视频存储在Database Firestore中,但是在运行我的应用程序时出现此错误:
java.lang.NullPointerException: Attempt to invoke interface method 'int java.util.List.size()' on a null object reference
at com.example.carlos.trendy.FeedRecyclerAdapter.getItemCount(FeedRecyclerAdapter.java:35)
我的代码:
public class FeedRecyclerAdapter extends RecyclerView.Adapter<FeedRecyclerAdapter.ViewHolder> {
public List<Feed>feedList;
public FeedRecyclerAdapter(List<Feed> feedList){
this.feedList = feedList;
}
@Override
public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.feed_row, parent, false);
return new ViewHolder(view);
}
@Override
public void onBindViewHolder(ViewHolder holder, int position) {
String desc_data = feedList.get(position).getDesc();
holder.setDesc(desc_data);
}
@Override
public int getItemCount() {
return feedList.size();///ERROR
}
public class ViewHolder extends RecyclerView.ViewHolder{
private TextView desc;
private View mView;
public ViewHolder(View itemView) {
super(itemView);
mView=itemView;
}
public void setDesc(String descText){
desc = mView.findViewById(R.id.post_desc);
desc.setText(descText);
}
}
}
FeedActivity:
public class FeedActivity extends AppCompatActivity {
private Context mContext = FeedActivity.this;
private static final int ACTIVITY_NUM = 0;
private FirebaseFirestore firebaseFirestore;
private RecyclerView mFeedList;
private List<Feed> feedList;
private FeedRecyclerAdapter feedRecyclerAdapter;
public void videoPicker(View view){
Intent intent = new Intent(FeedActivity.this, postActivity.class);
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
startActivity(intent);
}
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_feed);
mFeedList = this.findViewById(R.id.feed_list);
feedRecyclerAdapter = new FeedRecyclerAdapter(feedList);
mFeedList.setLayoutManager(new LinearLayoutManager(this));
mFeedList.setAdapter(feedRecyclerAdapter);
feedList = new ArrayList<>();
firebaseFirestore = FirebaseFirestore.getInstance();
firebaseFirestore.collection("Posts").addSnapshotListener(new EventListener<QuerySnapshot>() {
@Override
public void onEvent(QuerySnapshot documentSnapshots, FirebaseFirestoreException e) {
for (DocumentChange documentChange: documentSnapshots.getDocumentChanges()){
if (documentChange.getType() == DocumentChange.Type.ADDED){
Feed feed = documentChange.getDocument().toObject(Feed.class);
feedList.add(feed);
feedRecyclerAdapter.notifyDataSetChanged();
}
}
}
});
setupBottomNavigationView();
}
private void setupBottomNavigationView() {
BottomNavigationView bottomNavigationView = findViewById(R.id.bottomNavViewBar);
BottomNavigationViewHelper.enableNavigation(mContext, bottomNavigationView);
Menu menu = bottomNavigationView.getMenu();
MenuItem menuItem = menu.getItem(ACTIVITY_NUM);
menuItem.setChecked(true);
}
@Override
protected void onStart() {
super.onStart();
}
public static class FeedViewHolder extends RecyclerView.ViewHolder {
View mView;
public FeedViewHolder(View itemView) {
super(itemView);
itemView = mView;
}
public void setDesc(String desc){
TextView post_desc = mView.findViewById(R.id.post_desc);
post_desc.setText(desc);
}
}
}
有人可以帮助我吗?
答案 0 :(得分:1)
您在此处初始化适配器:
feedRecyclerAdapter = new FeedRecyclerAdapter(feedList);
但feedList
为空。
您必须对其进行初始化
移动所有这些行:
feedRecyclerAdapter = new FeedRecyclerAdapter(feedList);
mFeedList.setLayoutManager(new LinearLayoutManager(this));
mFeedList.setAdapter(feedRecyclerAdapter);
在此行之后:
feedList = new ArrayList<>();