我正在使用Firebase来检索数据到RecyclerView,所以我使用了这些方法自爆,但它给了我一个错误:
java.lang.NullPointerException: Attempt to invoke virtual method 'int java.util.ArrayList.size()' on a null object reference
这是我的适配器:
public class RecyclerAdapter extends RecyclerView.Adapter<RecyclerAdapter.PostViewHolder> {
public ArrayList<Getting_Posts> mdataSet;
public RecyclerAdapter(ArrayList<Getting_Posts> dataSet) {
mdataSet = dataSet;
}
@Override
public RecyclerAdapter.PostViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View inflatedView = LayoutInflater.from(parent.getContext()).inflate(R.layout.post_card_design, parent, false);
return new RecyclerAdapter.PostViewHolder(inflatedView);}
@Override
public void onBindViewHolder(PostViewHolder holder, int position) {
Getting_Posts post = mdataSet.get(position);}
@Override
public int getItemCount() {
return mdataSet.size();}
public void add(int position, Getting_Posts post) {
mdataSet.add(position, post);
notifyItemInserted(position);}
public class PostViewHolder extends RecyclerView.ViewHolder {
TextView Name;
public PostViewHolder(View inflatedView) {
super(inflatedView);
Name = (TextView) inflatedView.findViewById(R.id.PostUsername);}}}
这是我的活动课:
private RecyclerView mPostList;
ArrayList<Getting_Posts> posts;
private RecyclerAdapter mAdapter;
mPostList = (RecyclerView) v.findViewById(R.id.MainPostList);
mPostList.setHasFixedSize(true);
mPostList.setItemAnimator(new DefaultItemAnimator());
LinearLayoutManager layoutManager = new LinearLayoutManager(getActivity());
mPostList.setLayoutManager(layoutManager);
mAdapter = new RecyclerAdapter(posts);
mPostList.setAdapter(mAdapter);
mAuth = FirebaseAuth.getInstance();
mCurrentUser = mAuth.getCurrentUser();
//Firebase
mDatabase = FirebaseDatabase.getInstance().getReference().child("Posts");
mDatabaseFriends = FirebaseDatabase.getInstance().getReference().child("Following").child(mCurrentUser.getUid());
return v;
}
@Override
public void onStart() {
mDatabaseFriends.addValueEventListener(new ValueEventListener() {
@Override
public void onDataChange(DataSnapshot dataSnapshot) {
for(DataSnapshot following : dataSnapshot.getChildren()) {
mDatabase.orderByChild("UID").equalTo(following.getKey()).addValueEventListener(new ValueEventListener() {
@Override
public void onDataChange(DataSnapshot dataSnapshot) {
Getting_Posts model = dataSnapshot.getValue(Getting_Posts.class);
mAdapter.add(mAdapter.mdataSet.size(), dataSnapshot.getValue(Getting_Posts.class));
mAdapter.notifyDataSetChanged();}
@Override
public void onCancelled(DatabaseError databaseError) {
}});}}
@Override
public void onCancelled(DatabaseError databaseError) {
}
});
super.onStart();
}}
这是我的Getter和setter类:
public class GettingTester {
private String Name;
public GettingTester(String Name) {
this.Name = Name;
}
public String getName() {
return Name;
}
public void setName(String name) {
Name = name;
}
最后是我的json:
"Following" : {
"ORBBKfZAAUhqI1h7ojQDzYOjgkp1" : {
"pypD1SYZkbcYesk09WuMUY1AkTf1": true,
//I need to show this UID posts only but,
},
"Posts" : {
"-KduqnVVczZf5uibQiZ-" : {
"Describe" : "gg",
"Time" : 14881230655,
"UID" : "pypD1SYZkbcYesk09WuMUY1AkTf1",
"Username" : "Jone",
"Wallpaper" : "https://firebasestorage.googleapis.com/v0/b/test-3f4c5.appspot.com/o/Posts%2Fcropped906285501.jpg?alt=media&token=0c1a3a3d-6e48-4c4e-ba59-f5646bf8965f"
},
"-Ke5gJ00CxbjhOuhzLIx" : {
"Describe" : "hajj",
"Time" : 1488318465,
"UID" : "pypD1SYZkbcYesk09WuMUY1AkTf1",
"Username" : "Dom",
"Wallpaper" : "https://firebasestorage.googleapis.com/v0/b/test-3f4c5.appspot.com/o/Posts%2Fcropped1717103943.jpg?alt=media&token=a85b2488-5ac7-49a7-9ad0-dbf4e6f29389"
},
"-KeCuiFmUCpN19zwsTsR" : {
"Describe" : "a",
"Profile" : "https://lh4.googleusercontent.com/-suB77riNoX8/AAAAAAAAAAI/AAAAAAAAAAA/ADPlhfKMzINn-Ki538Sqf6SRGaXC81-WuQ/s200-c/photo.jpg",
"Time" : 1488439652,
"UID" : "z2SNUlLd6mQM8nnlkU2VUws5Ggl2",
"Username" : "Dom",
"Wallpaper" : "https://firebasestorage.googleapis.com/v0/b/test-3f4c5.appspot.com/o/Posts%2Fcropped1913886685.jpg?alt=media&token=c8ad26b2-8f09-453b-b48a-aad9e4d8b5c3"
},
}
我只需要显示关注用户的帖子。
最佳答案
在此代码中:
mAdapter.add(mAdapter.mdataSet.size(), dataSnapshot.getValue(Getting_Posts.class));
上面的代码第一次执行时,
mdataSet
是null
。这是为什么?因为在上述代码之前唯一修改mdataSet
的代码来自RecyclerAdapter
(mAdapter
)的init:mAdapter = new RecyclerAdapter(posts);
mdataSet
对象正在复制posts
对象的值,我们知道,它确实为null。只需将值赋给
posts
对象,就会使它工作:ArrayList<Getting_Posts> posts = new ArrayList<>();
希望这可以帮助