【问题标题】:'void android.widget.ImageView.setVisibility(int)' on a null object reference空对象引用上的“void android.widget.ImageView.setVisibility(int)”
【发布时间】:2022-06-15 18:28:55
【问题描述】:

我正在尝试检查用户是否正在关注另一个用户。如果是,那么follow_btn 是不可见的,如果不是,那么它是可见的。但我不断收到此错误Attempt to invoke virtual method 'void android.widget.ImageView.setVisibility(int)' on a null object reference

我调用的函数是不同的方法,但我不断收到该错误。我还检查了 if 语句中的按钮是否为空,我被告知它为空。我不知道还有什么可以帮助我的吗?

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

        }
    });
}

}

【问题讨论】:

  • public ImageView follow_btn; 为什么你有这两个?虽然摆脱它可能无法解决您的问题,但令人困惑

标签: java android


猜你喜欢
  • 2017-09-03
  • 2020-06-12
  • 1970-01-01
  • 2020-11-28
  • 1970-01-01
  • 1970-01-01
  • 2020-10-02
  • 2019-03-30
  • 1970-01-01
相关资源
最近更新 更多