gpt4 book ai didi

android - 空引用上的 Firebase ArrayList.size()

转载 作者:行者123 更新时间:2023-11-30 00:39:45 24 4
gpt4 key购买 nike

我正在使用 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);}}}

这是我的 Activity 课:

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));

第一次执行上述代码时,mdataSetnull。这是为什么?因为在上面代码之前唯一修改mdataSet的代码来自RecyclerAdapter (mAdapter)的init:

mAdapter = new RecyclerAdapter(posts);

mdataSet 对象正在复制 posts 对象的值,我们知道,确实是 null。

简单地为 posts 对象赋值将使这个 wokrs:

ArrayList<Getting_Posts> posts = new ArrayList<>();

希望对你有帮助

关于android - 空引用上的 Firebase ArrayList.size(),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42718189/

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