>我在下一行代码中收到以下错误
recentMessages = mDatabase.orderByKey().equalTo(obj).limitToLast(20);
错误如下
java.lang.NullPointerException: 尝试在空对象引用上调用虚拟方法 'com.google.firebase.database.ChildEventListener com.google.firebase.database.Query.addChildEventListener(com.google.firebase.database.ChildEventListener('
这是代码
public class LikesFeedFragment extends Fragment {
private RecyclerView mBlogList;
private DatabaseReference mDatabase ;
private DatabaseReference mCommentsDatabase;
private Query commentsQuery;
Query recentMessages;
private Query filterLike;
private boolean mProcessLike = false;
private FirebaseAuth auth;
private DatabaseReference mDatabaseLike;
private DatabaseReference ref;
@Override
public void onActivityCreated(Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
setHasOptionsMenu(true);
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View v = inflater.inflate(R.layout.fragment_feed, container, false);
setHasOptionsMenu(true);
mDatabase = FirebaseDatabase.getInstance().getReference().child("Blog");
mDatabaseLike = FirebaseDatabase.getInstance().getReference().child("Likes");
mCommentsDatabase = FirebaseDatabase.getInstance().getReference().child("Comentarios");
ref = FirebaseDatabase.getInstance().getReference().child("Likes");
ref.keepSynced(true);
mDatabase.keepSynced(true);
mDatabaseLike.keepSynced(true);
mCommentsDatabase.keepSynced(true);
auth = FirebaseAuth.getInstance();
ref.addValueEventListener(new ValueEventListener() {
@Override
public void onDataChange(DataSnapshot snapshot) {
for (DataSnapshot objSnapshot: snapshot.getChildren()) {
String obj = objSnapshot.getKey().toString();
Log.e("Doo 3", obj);
recentMessages = mDatabase.orderByKey().equalTo(obj).limitToLast(20);
}
}
@Override
public void onCancelled(DatabaseError firebaseError) {
Log.e("Read failed", firebaseError.getMessage());
}
});
mBlogList = (RecyclerView) v.findViewById(R.id.blog_list);
LinearLayoutManager linearLayoutManager = new LinearLayoutManager(getActivity());
linearLayoutManager.setReverseLayout(true);
linearLayoutManager.setStackFromEnd(true);
//linearLayoutManager.findFirstVisibleItemPosition();
mBlogList.setHasFixedSize(true);
mBlogList.setLayoutManager(linearLayoutManager);
mBlogList.setNestedScrollingEnabled(false);
return v;
}
@Override
public void onStart() {
super.onStart();
FirebaseRecyclerAdapter<FeedData,FeedViewHolder> firebaseRecyclerAdapter = new FirebaseRecyclerAdapter<FeedData, FeedViewHolder>(
FeedData.class,
R.layout.item_feed,
FeedViewHolder.class,
recentMessages) {
@Override
protected void populateViewHolder(final FeedViewHolder viewHolder, FeedData model, int position) {
final String post_key = getRef(position).getKey();
viewHolder.setTitle(model.getTitle());
viewHolder.setDesc(model.getDesc());
viewHolder.setImage(getContext(), model.getThumb_image());
viewHolder.setLikeBtn(post_key);
commentsQuery = mCommentsDatabase.orderByChild("id_comment").equalTo(post_key);
commentsQuery.addValueEventListener(new ValueEventListener() {
@Override
public void onDataChange(DataSnapshot dataSnapshot) {
long comentCount = dataSnapshot.getChildrenCount();
if (comentCount == 0) {
viewHolder.comments.setText("0 Comentarios");
}
else {
viewHolder.comments.setText(String.valueOf(comentCount) + " Comentarios");
}
}
@Override
public void onCancelled(DatabaseError databaseError) {
}
});
mDatabaseLike.child(post_key).addValueEventListener(new ValueEventListener() {
@Override
public void onDataChange(DataSnapshot dataSnapshot) {
long Count = dataSnapshot.getChildrenCount();
// Toast.makeText(getActivity(), (int) Count, Toast.LENGTH_SHORT).show();
Log.e("Doo", String.valueOf(Count));
if (Count == 0) {
viewHolder.likes.setText("0 likes");
// viewHolder.likes.setVisibility(View.INVISIBLE);
}
else {
//viewHolder.likes.setVisibility(View.VISIBLE);
viewHolder.likes.setText(String.valueOf(Count) + " likes");
}
}
@Override
public void onCancelled(DatabaseError databaseError){
}
});
viewHolder.imageBlog.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent intentBlog = new Intent(getActivity(), BlogSingleActivity.class);
intentBlog.putExtra("blog_id",post_key);
startActivity(intentBlog);
}
});
viewHolder.likeButton.setOnLikeListener(new OnLikeListener() {
@Override
public void liked(final LikeButton likeButton) {
mProcessLike = true;
mDatabaseLike.addValueEventListener(new ValueEventListener() {
@Override
public void onDataChange(DataSnapshot dataSnapshot) {
if (mProcessLike) {
if (!dataSnapshot.child(post_key).hasChild(auth.getCurrentUser().getUid())) {
mDatabaseLike.child(post_key).child(auth.getCurrentUser().getUid()).setValue("RandomValue");
likeButton.setLiked(true);
mProcessLike = false;
}
}
}
@Override
public void onCancelled(DatabaseError databaseError) {
}
});
}
@Override
public void unLiked(final LikeButton likeButton) {
mProcessLike = true;
mDatabaseLike.addValueEventListener(new ValueEventListener() {
@Override
public void onDataChange(DataSnapshot dataSnapshot) {
if (mProcessLike) {
if (dataSnapshot.child(post_key).hasChild(auth.getCurrentUser().getUid())) {
mDatabaseLike.child(post_key).child(auth.getCurrentUser().getUid()).removeValue();
likeButton.setLiked(false);
mProcessLike = false;
}
}
}
@Override
public void onCancelled(DatabaseError databaseError) {
}
});
}
});
}
};
mBlogList.setAdapter(firebaseRecyclerAdapter);
}
public static class FeedViewHolder extends RecyclerView.ViewHolder{
View mView;
LikeButton likeButton;
DatabaseReference mDatabaseLike;
FirebaseAuth mAuth;
ImageView imageBlog;
TextView likes;
TextView comments;
public FeedViewHolder(View itemView) {
super(itemView);
mView = itemView;
likeButton = (LikeButton) mView.findViewById(R.id.heart_button);
imageBlog = (ImageView) mView.findViewById(R.id.imageBlog);
mDatabaseLike = FirebaseDatabase.getInstance().getReference().child("Likes");
likes = (TextView) mView.findViewById(R.id.count_like);
comments = (TextView) mView.findViewById(R.id.comentCount);
mAuth = FirebaseAuth.getInstance();
mDatabaseLike.keepSynced(true);
}
public void setLikeBtn(final String post_key){
mDatabaseLike.addValueEventListener(new ValueEventListener() {
@Override
public void onDataChange(DataSnapshot dataSnapshot) {
if (dataSnapshot.child(post_key).hasChild(mAuth.getCurrentUser().getUid())){
likeButton.setLiked(true);
}else{likeButton.setLiked(false);}
}
@Override
public void onCancelled(DatabaseError databaseError) {
}
});
}
public void setTitle(String title){
TextView post_title = (TextView) mView.findViewById(R.id.post_title);
post_title.setText(title);
}
public void setDesc(String desc){
TextView desc_title = (TextView) mView.findViewById(R.id.post_desc);
desc_title.setText(desc);
}
public void setImage(final Context ctx, final String image) {
// Picasso.with(ctx).load(image).into(imageBlog);
Picasso.with(ctx).load(image).networkPolicy(NetworkPolicy.OFFLINE)
.placeholder(R.drawable.fondonav).into(imageBlog, new Callback() {
@Override
public void onSuccess() {
}
@Override
public void onError() {
Picasso.with(ctx).load(image).placeholder(R.drawable.fondonav).into(imageBlog);
}
});
}
}
@Override
public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
inflater.inflate(R.menu.menu_fragment, menu);
super.onCreateOptionsMenu(menu, inflater);
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
int id = item.getItemId();
if (id == R.id.add) {
Toast.makeText(getActivity(), "Crear publicacion", Toast.LENGTH_SHORT).show();
startActivity(new Intent(getActivity(), PostActivity.class));
return true;
}
return super.onOptionsItemSelected(item);
}
}
向onStart()
添加一个日志语句,以查看它是否在初始化recentMessages
运行的onDataChange()
(见下文(之前运行。 我怀疑它确实如此,导致recentMessages
在传递到FirebaseRecyclerAdapter
时为空。
我还质疑为什么在 for 循环中初始化recentMessages
。 如果快照没有子项,则循环不会运行,也不会设置recentMessages
。
public void onDataChange(DataSnapshot snapshot) {
for (DataSnapshot objSnapshot : snapshot.getChildren()) {
String obj = objSnapshot.getKey().toString();
Log.e("Doo 3", obj);
recentMessages = mDatabase.orderByKey().equalTo(obj).limitToLast(20);
}
}