无法解析 'ConstraintLayout' 中的方法 'setAdapter'

Cannot resolve method 'setAdapter' in 'ConstraintLayout'

我是 android 开发的新手,正在尝试将回收器视图与 setAdapter 绑定,但出现此错误“无法解析 'ConstraintLayout' 中的方法 'setAdapter'”

我的mainActivity.java代码是:

我的主要objective是用ActivityMainBinding绑定recyclerView和setAdapter

package com.example.baatcheet;

import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;
import androidx.recyclerview.widget.LinearLayoutManager;
import com.example.baatcheet.usersAdapter.*;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.Toast;

import com.example.baatcheet.databinding.ActivityMainBinding;
import com.google.firebase.database.DataSnapshot;
import com.google.firebase.database.DatabaseError;
import com.google.firebase.database.FirebaseDatabase;
import com.google.firebase.database.ValueEventListener;

import java.util.ArrayList;

public class MainActivity extends AppCompatActivity {

   ActivityMainBinding binding;
   FirebaseDatabase database;
   ArrayList<User> users;
   usersAdapter UserAdapter;
   @Override
   protected void onCreate(Bundle savedInstanceState) {
       super.onCreate(savedInstanceState);
       binding = ActivityMainBinding.inflate(getLayoutInflater());
       
       setContentView(binding.getRoot());
       database = FirebaseDatabase.getInstance();
       users = new ArrayList<>();
       UserAdapter = new usersAdapter(this, users);
       binding.recyclerView.setAdapter(UserAdapter);



       database.getReference().child("users").addValueEventListener(new ValueEventListener() {
           @Override
           public void onDataChange(@NonNull DataSnapshot snapshot) {
               users.clear();
               for(DataSnapshot snapshot1 : snapshot.getChildren()){
                   User user = snapshot1.getValue(User.class);
                   users.add(user);
               }
               UserAdapter.notifyDataSetChanged();
           }

           @Override
           public void onCancelled(@NonNull DatabaseError error) {

           }
       });




   }

   @Override
   public boolean onOptionsItemSelected(@NonNull MenuItem item) {
       switch (item.getItemId()){
           case R.id.search:
               Toast.makeText(this,"Search Clicked" , Toast.LENGTH_SHORT).show();
               break;
           case R.id.settings:
               Toast.makeText(this , " setting Clicked" , Toast.LENGTH_SHORT).show();
               break;

       }
       return super.onOptionsItemSelected(item);
   }

   @Override
   public boolean onCreateOptionsMenu(Menu menu) {
       getMenuInflater().inflate(R.menu.top_menu,menu);
       return super.onCreateOptionsMenu(menu);
   }
}


这是我出错的部分 -> binding.recyclerView.setAdapter(UserAdapter);

它告诉我重命名引用 这到底是什么意思?

activity_main.xml 文件是:

<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/recyclerView"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity">

    <com.google.android.material.bottomnavigation.BottomNavigationView
        android:id="@+id/bottomNavigationView2"
        android:layout_width="match_parent"
        android:layout_height="0dp"
        app:elevation="30dp"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:menu="@menu/menu">

    </com.google.android.material.bottomnavigation.BottomNavigationView>

    <androidx.recyclerview.widget.RecyclerView
        android:layout_width="match_parent"
        android:layout_height="0dp"
        app:layout_constraintBottom_toTopOf="@+id/bottomNavigationView2"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        tools:listitem="@layout/row_conversation"
        app:layoutManager="androidx.recyclerview.widget.LinearLayoutManager"
        />

</androidx.constraintlayout.widget.ConstraintLayout>

users.java代码是:

package com.example.baatcheet;

public class User {
    private String uid,name,phoneNumber,profileImage;
    public User(){

    }


    public User(String uid, String name, String phoneNumber, String profileImage) {
        this.uid = uid;
        this.name = name;
        this.phoneNumber = phoneNumber;
        this.profileImage = profileImage;
    }

    public void setUid(String uid) {
        this.uid = uid;
    }

    public void setName(String name) {
        this.name = name;
    }

    public void setPhoneNumber(String phoneNumber) {
        this.phoneNumber = phoneNumber;
    }

    public void setProfileImage(String profileImage) {
        this.profileImage = profileImage;
    }

    public String getUid() {
        return uid;
    }

    public String getName() {
        return name;
    }

    public String getPhoneNumber() {
        return phoneNumber;
    }

    public String getProfileImage() {
        return profileImage;
    }
}

usersAdapter.java代码是:

package com.example.baatcheet;

import android.content.Context;
import android.view.ContextMenu;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import com.example.baatcheet.databinding.RowConversationBinding;

import java.util.ArrayList;

public class usersAdapter extends RecyclerView.Adapter<usersAdapter.UsersViewHolder>{
    Context context;
    ArrayList<User> users;
    public  usersAdapter(Context context , ArrayList<User> users){
        this.context = context;
        this.users = users;
    }
    {

    }
    @NonNull
    @Override
    public UsersViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(context).inflate(R.layout.row_conversation,parent,false);
        return new UsersViewHolder(view);
    }

    @Override
    public void onBindViewHolder(@NonNull UsersViewHolder holder, int position) {

        User user = users.get(position);

        holder.binding.userName.setText(user.getName());
    }

    @Override
    public int getItemCount() {
        return users.size();
    }

    public class UsersViewHolder extends RecyclerView.ViewHolder{

        RowConversationBinding binding;
        public UsersViewHolder(@NonNull View itemView) {
            super(itemView);
            binding = RowConversationBinding.bind(itemView);
        }
    }
}

在您的布局文件中,根 ConstraintLayout 具有 @+id/recyclerView id:

<androidx.constraintlayout.widget.ConstraintLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/recyclerView"
    ... />

您应该将此 ID 移动到 RecyclerView 本身:

<androidx.recyclerview.widget.RecyclerView
    android:id="@+id/recyclerView"
    ... />

执行此操作后,binding.recyclerView 将引用 RecyclerView 而不是 ConstraintLayout

如果出于其他原因需要通过视图绑定访问 ConstraintLayout,可以使用 binding.root.