我一直收到此错误,但似乎无法解决不断弹出的问题。
public class PostAdapter extends RecyclerView.Adapter<PostAdapter.ViewHolder>{
private Context mContext;
private List<Post> mPost;
private FirebaseUser firebaseUser;
private DatabaseReference databaseReference;
public PostAdapter(Context mContext, List<Post> mPost){
this.mContext = mContext;
this.mPost = mPost;
}
@NonNull
@Override
public ViewHolder onCreateViewHolder(@NonNull ViewGroup viewGroup, int i) {
View view = LayoutInflater.from(mContext).inflate(R.layout.post_item, viewGroup, false);
return new PostAdapter.ViewHolder(view);
}
@Override
public void onBindViewHolder(@NonNull final ViewHolder viewHolder, int i) {
firebaseUser = FirebaseAuth.getInstance().getCurrentUser();
final Post post = mPost.get(i);
Glide.with(mContext).load(post.getPostImage()).into(viewHolder.post_image);
if (post.getDescription().equals("")){
viewHolder.description.setVisibility(View.GONE);
} else {
viewHolder.description.setVisibility(View.VISIBLE);
viewHolder.description.setText(post.getDescription());
}
publisherInfo(viewHolder.image_profile, viewHolder.username, viewHolder.publisher, post.getPublisher());
isLiked(post.getPostid(), viewHolder.like);
numberLikes(viewHolder.likes, post.getPostid());
getComments(post.getPostid(), viewHolder.comments);
isSaved(post.getPostid(), viewHolder.save);
viewHolder.image_profile.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
SharedPreferences.Editor editor = mContext.getSharedPreferences("PREPS", Context.MODE_PRIVATE).edit();
editor.putString("profileid", post.getPublisher());
editor.apply();
((FragmentActivity)mContext).getSupportFragmentManager().beginTransaction().replace(R.id.fragment_container,
new ProfileFragment()).commit();
}
});
viewHolder.username.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
SharedPreferences.Editor editor = mContext.getSharedPreferences("PREPS", Context.MODE_PRIVATE).edit();
editor.putString("profileid", post.getPublisher());
editor.apply();
((FragmentActivity)mContext).getSupportFragmentManager().beginTransaction().replace(R.id.fragment_container,
new ProfileFragment()).commit();
}
});
viewHolder.publisher.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
SharedPreferences.Editor editor = mContext.getSharedPreferences("PREPS", Context.MODE_PRIVATE).edit();
editor.putString("profileid", post.getPublisher());
editor.apply();
((FragmentActivity)mContext).getSupportFragmentManager().beginTransaction().replace(R.id.fragment_container,
new ProfileFragment()).commit();
}
});
viewHolder.post_image.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
SharedPreferences.Editor editor = mContext.getSharedPreferences("PREPS", Context.MODE_PRIVATE).edit();
editor.putString("postid", post.getPostid());
editor.apply();
((FragmentActivity)mContext).getSupportFragmentManager().beginTransaction().replace(R.id.fragment_container,
new PostDetailFragment()).commit();
}
});
viewHolder.save.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if (viewHolder.save.getTag().equals("Save")){
FirebaseDatabase.getInstance().getReference().child("Saves").child(firebaseUser.getUid())
.child(post.getPostid()).setValue(true);
} else {
FirebaseDatabase.getInstance().getReference().child("Saves").child(firebaseUser.getUid())
.child(post.getPostid()).removeValue();
}
}
});
viewHolder.like.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
if (viewHolder.like.getTag().equals("like")){
FirebaseDatabase.getInstance().getReference().child("Likes").child(post.getPostid())
.child(firebaseUser.getUid()).setValue(true);
addNotifications(post.getPublisher(), post.getPostid());
} else {
FirebaseDatabase.getInstance().getReference().child("Likes").child(post.getPostid())
.child(firebaseUser.getUid()).removeValue();
}
}
});
viewHolder.comment.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent intent = new Intent(mContext, CommentsActivity.class);
intent.putExtra("postId", post.getPostid());
intent.putExtra("publisherId", post.getPostid());
mContext.startActivity(intent);
}
});
viewHolder.comments.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent intent = new Intent(mContext, CommentsActivity.class);
intent.putExtra("postId", post.getPostid());
intent.putExtra("publisherId", post.getPostid());
mContext.startActivity(intent);
}
});
viewHolder.likes.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent intent = new Intent(mContext, FollowersActivity.class);
intent.putExtra("id", post.getPostid());
intent.putExtra("title", "likes");
mContext.startActivity(intent);
}
});
}
@Override
public int getItemCount() {
return mPost.size();
}
public class ViewHolder extends RecyclerView.ViewHolder {
public ImageView image_profile, post_image, like, comment, save;
public TextView username, likes, publisher, description, comments;
ViewHolder(@NonNull View itemView){
super(itemView);
image_profile = itemView.findViewById(R.id.image_profile);
post_image = itemView.findViewById(R.id.post_image);
comments = itemView.findViewById(R.id.comments);
like = itemView.findViewById(R.id.like);
comment = itemView.findViewById(R.id.comment);
save = itemView.findViewById(R.id.save);
username = itemView.findViewById(R.id.username);
likes = itemView.findViewById(R.id.likes);
publisher = itemView.findViewById(R.id.publisher);
description = itemView.findViewById(R.id.description);
}
}
private void getComments (String postid, final TextView comments){
databaseReference = FirebaseDatabase.getInstance().getReference().child("Comments").child(postid);
databaseReference.addValueEventListener(new ValueEventListener() {
@SuppressLint("SetTextI18n")
@Override
public void onDataChange(@NonNull DataSnapshot dataSnapshot) {
comments.setText("View All "+dataSnapshot.getChildrenCount() + " Comments");
}
@Override
public void onCancelled(@NonNull DatabaseError databaseError) {
}
});
}
private void isLiked(String postid, final ImageView imageView){
firebaseUser = FirebaseAuth.getInstance().getCurrentUser();
databaseReference = FirebaseDatabase.getInstance().getReference().child("Likes").child(postid);
databaseReference.addValueEventListener(new ValueEventListener() {
@Override
public void onDataChange(@NonNull DataSnapshot dataSnapshot) {
if (dataSnapshot.child(firebaseUser.getUid()).exists()){
imageView.setImageResource(R.drawable.liked);
imageView.setTag("liked");
} else {
imageView.setImageResource(R.drawable.like);
imageView.setTag("like");
}
}
@Override
public void onCancelled(@NonNull DatabaseError databaseError) {
}
});
}
private void addNotifications(String userid, String postid){
databaseReference = FirebaseDatabase.getInstance().getReference("Notifications").child(userid);
HashMap<String, Object> hashMap = new HashMap<>();
hashMap.put("userid", firebaseUser.getUid());
hashMap.put("text", "liked your post");
hashMap.put("postid", postid);
hashMap.put("ispost", true);
databaseReference.push().setValue(hashMap);
}
private void numberLikes(final TextView likes, String postid){
databaseReference = FirebaseDatabase.getInstance().getReference().child("Likes")
.child(postid);
databaseReference.addValueEventListener(new ValueEventListener() {
@SuppressLint("SetTextI18n")
@Override
public void onDataChange(@NonNull DataSnapshot dataSnapshot) {
likes.setText(dataSnapshot.getChildrenCount()+" likes");
}
@Override
public void onCancelled(@NonNull DatabaseError databaseError) {
}
});
}
private void publisherInfo(final ImageView image_profile, final TextView username, final TextView publisher, final String userid){
databaseReference = FirebaseDatabase.getInstance().getReference("Users").child(userid);
databaseReference.addValueEventListener(new ValueEventListener() {
@Override
public void onDataChange(@NonNull DataSnapshot dataSnapshot) {
User user = dataSnapshot.getValue(User.class);
assert user != null;
Glide.with(mContext).load(user.getImageurl()).into(image_profile);
username.setText(user.getUsername());
publisher.setText(user.getUsername());
}
@Override
public void onCancelled(@NonNull DatabaseError databaseError) {
}
});
}
private void isSaved(final String postid, final ImageView imageView){
FirebaseUser firebaseUser = FirebaseAuth.getInstance().getCurrentUser();
assert firebaseUser != null;
databaseReference = FirebaseDatabase.getInstance().getReference().child("Saves")
.child(firebaseUser.getUid());
databaseReference.addValueEventListener(new ValueEventListener() {
@Override
public void onDataChange(@NonNull DataSnapshot dataSnapshot) {
if (dataSnapshot.child(postid).exists()){
imageView.setImageResource(R.drawable.saved);
imageView.setTag("Saved");
} else {
imageView.setImageResource(R.drawable.save);
imageView.setTag("Save");
}
}
@Override
public void onCancelled(@NonNull DatabaseError databaseError) {
}
});
}
}
这是我不断遇到的错误。
java.lang.NullPointerException:无法为child()中的参数'pathString'传递null com.google.firebase.database.DatabaseReference.child(com.google.firebase:firebase-database @@ 16.0.5:101) 在com.editionstudio.socialnetwork.Adapter.PostAdapter.isLiked(PostAdapter.java:231) 在com.editionstudio.socialnetwork.Adapter.PostAdapter.onBindViewHolder(PostAdapter.java:71) 在com.editionstudio.socialnetwork.Adapter.PostAdapter.onBindViewHolder(PostAdapter.java:35)