I am trying to check if a user is following another user. If they are then the follow_btn
is invisible, if not then it's visible. But I keep getting this error Attempt to invoke virtual method 'void android.widget.ImageView.setVisibility(int)' on a null object reference
.
I have called the function is different methods but I keep getting that error. I also check if the button was null in an if statement and I'm being told it's null. I don't know what else to do can someone help me ?
layout_main.xml:
<ImageView
android:id="@ id/follow_btn"
android:layout_width="15dp"
android:layout_height="15dp"
android:layout_marginTop="25dp"
android:layout_marginEnd="10dp"
app:layout_constraintBottom_toBottomOf="@ id/image_profile"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="@ id/image_profile"
app:layout_constraintTop_toTopOf="@ id/image_profile"
app:srcCompat="@drawable/add" />
PostAdapter.Java:
public class PostAdapter extends RecyclerView.Adapter<PostAdapter.ViewHolder> {
public Context mContext;
public List<Post> mPost;
private FirebaseUser firebaseUser;
public static String mGroupId;
public String profileid;
public ImageView follow_btn;
public PostAdapter(Context mContext, List<Post> mPost) {
this.mContext = mContext;
this.mPost = mPost;
}
@NonNull
@Override
public ViewHolder onCreateViewHolder(@NonNull ViewGroup viewGroup, int viewType) {
View view = LayoutInflater.from(mContext).inflate(R.layout.layout_main, viewGroup, false);
return new ViewHolder(view);
}
@Override
public void onBindViewHolder(@NonNull ViewHolder viewHolder, int i) {
if (follow_btn != null) {
checkFollow();
} else {
Toast.makeText(mContext, "Null.", Toast.LENGTH_SHORT).show();
}
}
public class ViewHolder extends RecyclerView.ViewHolder {
// creating a variable for exoplayerview.
public PlayerView post_video;
// creating a variable for exoplayer
public SimpleExoPlayer exoPlayer;
public ImageView image_profile, like, comment, save, follow_btn; //more
//public ExoVideoView post_video;
public TextView username, description, comments, dateAdded, likes;
FirebaseUser firebaseUser;
public ViewHolder(@NonNull View itemView) {
super(itemView);
image_profile = itemView.findViewById(R.id.image_profile);
post_video = itemView.findViewById(R.id.thumbnail);
like = itemView.findViewById(R.id.like);
comment = itemView.findViewById(R.id.comment);
comments = itemView.findViewById(R.id.comments);
dateAdded = itemView.findViewById(R.id.date_post_added);
save = itemView.findViewById(R.id.save);
username = itemView.findViewById(R.id.username);
likes = itemView.findViewById(R.id.likes);
description = itemView.findViewById(R.id.description);
//more = itemView.findViewById(R.id.more);
follow_btn = itemView.findViewById(R.id.follow_btn);
firebaseUser = FirebaseAuth.getInstance().getCurrentUser();
SharedPreferences prefs = mContext.getSharedPreferences("PREFS", Context.MODE_PRIVATE);
profileid = prefs.getString("profileid", "none");
}
}
private void checkFollow() {
DatabaseReference reference = FirebaseDatabase.getInstance().getReference()
.child("Follow").child(firebaseUser.getUid()).child("following");
reference.addValueEventListener(new ValueEventListener() {
@Override
public void onDataChange(@NonNull DataSnapshot dataSnapshot) {
if (dataSnapshot.child(profileid).exists()) {
//.setText("following");
follow_btn.setVisibility(View.INVISIBLE);
} else {
//edit_profile.setText("follow");
follow_btn.setVisibility(View.VISIBLE);
}
}
@Override
public void onCancelled(@NonNull DatabaseError error) {
}
});
}
}
CodePudding user response:
remove your public ImageView follow_btn;
from top declaration. (above your constructor).
inside onBindViewHolder
you can use childViews with it's ViewHolder
.
@Override
public void onBindViewHolder(@NonNull ViewHolder viewHolder, int i) {
if (viewHolder.follow_btn != null) {
checkFollow();
} else {
Toast.makeText(mContext, "Null.", Toast.LENGTH_SHORT).show();
}
}
CodePudding user response:
Pass ViewHolder in argument:-
@Override
public void onBindViewHolder(@NonNull ViewHolder viewHolder, int i) {
if (follow_btn != null) {
checkFollow(viewHolder);
} else {
Toast.makeText(mContext, "Null.", Toast.LENGTH_SHORT).show();
}
}
private void checkFollow(ViewHolder viewHolder) {
DatabaseReference reference = FirebaseDatabase.getInstance().getReference()
.child("Follow").child(firebaseUser.getUid()).child("following");
reference.addValueEventListener(new ValueEventListener() {
@Override
public void onDataChange(@NonNull DataSnapshot dataSnapshot) {
if (dataSnapshot.child(profileid).exists()) {
//.setText("following");
viewHolder.follow_btn.setVisibility(View.INVISIBLE);
} else {
//edit_profile.setText("follow");
viewHolder.follow_btn.setVisibility(View.VISIBLE);
}
}
@Override
public void onCancelled(@NonNull DatabaseError error) {
}
});
I hope this will help you..