【问题标题】:Binary XML file line #26: RecyclerView has no LayoutManager androidx.recyclerview.widget.RecyclerView{a9ab2d5 VFED二进制 XML 文件第 26 行:RecyclerView 没有 LayoutManager androidx.recyclerview.widget.RecyclerView{a9ab2d5 VFED
【发布时间】:2021-11-08 12:37:53
【问题描述】:

我在 android studio 上使用 Kotlin 编程语言创建 Recyclerview,当我运行我的代码时出现错误。这是我遇到的错误

E/AndroidRuntime: FATAL EXCEPTION: main
    Process: com.its*******.*******, PID: 18742
    java.lang.RuntimeException: Unable to start activity ComponentInfo{******************************}: android.view.InflateException: Binary XML file line #26: RecyclerView has no LayoutManager androidx.recyclerview.widget.RecyclerView{a9ab2d5 VFED..... ......I. 0,0-0,0 #7f080187 app:id/reyclerview_message_list}, adapter:null, layout:null, context:com.its*******.*******.******@36b9430
        at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:3191)
        at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3328)
        at android.app.servertransaction.LaunchActivityItem.execute(LaunchActivityItem.java:78)
        at android.app.servertransaction.TransactionExecutor.executeCallbacks(TransactionExecutor.java:113)
        at android.app.servertransaction.TransactionExecutor.execute(TransactionExecutor.java:71)
        at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2054)
        at android.os.Handler.dispatchMessage(Handler.java:106)
        at android.os.Looper.loop(Looper.java:233)
        at android.app.ActivityThread.main(ActivityThread.java:7225)
        at java.lang.reflect.Method.invoke(Native Method)
        at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:499)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:962)
     Caused by: android.view.InflateException: Binary XML file line #26: RecyclerView has no LayoutManager androidx.recyclerview.widget.RecyclerView{a9ab2d5 VFED..... ......I. 0,0-0,0 #7f080187 app:id/reyclerview_message_list}, adapter:null, layout:null, context:com.itsshyam640.cooltalks.userChat@36b9430
     Caused by: java.lang.IllegalStateException: RecyclerView has no LayoutManager androidx.recyclerview.widget.RecyclerView{a9ab2d5 VFED..... ......I. 0,0-0,0 #7f080187 app:id/reyclerview_message_list}, adapter:null, layout:null, context:com.its*******.*******.*****@36b9430
        at androidx.recyclerview.widget.RecyclerView.generateLayoutParams(RecyclerView.java:4514)
        at android.view.LayoutInflater.rInflate(LayoutInflater.java:902)
        at android.view.LayoutInflater.rInflateChildren(LayoutInflater.java:861)
        at android.view.LayoutInflater.rInflate(LayoutInflater.java:903)
        at android.view.LayoutInflater.rInflateChildren(LayoutInflater.java:861)
        at android.view.LayoutInflater.inflate(LayoutInflater.java:515)
        at android.view.LayoutInflater.inflate(LayoutInflater.java:423)
        at android.view.LayoutInflater.inflate(LayoutInflater.java:374)
        at androidx.appcompat.app.AppCompatDelegateImpl.setContentView(AppCompatDelegateImpl.java:699)
        at androidx.appcompat.app.AppCompatActivity.setContentView(AppCompatActivity.java:195)
        at com.its*******.*******.*****.onCreate(userChat.java:35)
        at android.app.Activity.performCreate(Activity.java:7378)
        at android.app.Activity.performCreate(Activity.java:7369)
        at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1271)
        at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:3171)
        at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3328)
        at android.app.servertransaction.LaunchActivityItem.execute(LaunchActivityItem.java:78)
        at android.app.servertransaction.TransactionExecutor.executeCallbacks(TransactionExecutor.java:113)
        at android.app.servertransaction.TransactionExecutor.execute(TransactionExecutor.java:71)
        at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2054)
        at android.os.Handler.dispatchMessage(Handler.java:106)
        at android.os.Looper.loop(Looper.java:233)
        at android.app.ActivityThread.main(ActivityThread.java:7225)
        at java.lang.reflect.Method.invoke(Native Method)
        at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:499)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:962)

这是我的主文件代码,我只根据要求发送了所需的代码

public class userChat extends AppCompatActivity {
    private RecyclerView recyclerView_message_list;
    private ArrayList<userChatModel> userChatModels = new ArrayList<>();
    FirebaseDatabase database;
    FirebaseAuth auth;
    @Override

    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_user_chat);
        getSupportActionBar().hide();

        database = FirebaseDatabase.getInstance();
        auth = FirebaseAuth.getInstance();


        String senderId = auth.getUid();
        String recievedId = getIntent().getStringExtra("userId");
        String userName = getIntent().getStringExtra("userName");
        String userProfileImage = getIntent().getStringExtra("userProfileImage");

        TextView username = findViewById(R.id.top_username);
        username.setText(userName);
        Picasso.get().load(userProfileImage).placeholder(R.drawable.user).into((ImageView) findViewById(R.id.user_profile));

        findViewById(R.id.back_button).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(userChat.this,MainActivity.class);
                startActivity(intent);
            }
        });


        recyclerView_message_list = (RecyclerView) findViewById(R.id.recyclerView_message_list);
        final userChatAdapter userChatAdapter = new userChatAdapter(userChatModels,this);
        recyclerView_message_list.setAdapter(userChatAdapter);

        LinearLayoutManager layoutManager = new LinearLayoutManager(this,LinearLayoutManager.VERTICAL,false);
        recyclerView_message_list.setLayoutManager(layoutManager);



        final String senderMessages = senderId + recievedId;
        final String recieverMessages = recievedId + senderId;

        database.getReference().child("Chats").child(senderMessages).addValueEventListener(new ValueEventListener() {
            @Override
            public void onDataChange(@NonNull DataSnapshot snapshot) {
                userChatModels.clear();
                for(DataSnapshot snapshot1 : snapshot.getChildren()){
                    userChatModel userChatModel = snapshot1.getValue(userChatModel.class);
                    userChatModels.add(userChatModel);
                }
                userChatAdapter.notifyDataSetChanged();
            }

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

            }
        });

这是我的 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:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@color/backgroundColor"
    tools:context=".userChat">

    <androidx.appcompat.widget.Toolbar
        android:id="@+id/user_chat_toolbar"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:background="?attr/colorPrimary"
        android:elevation="30dp"
        android:minHeight="?attr/actionBarSize"
        android:theme="?attr/actionBarTheme"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintHorizontal_bias="0.0"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent">

        <androidx.constraintlayout.widget.ConstraintLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            tools:layout_editor_absoluteX="16dp">


            <de.hdodenhof.circleimageview.CircleImageView
                android:id="@+id/back_button"
                android:layout_width="50dp"
                android:layout_height="50dp"
                android:background="@drawable/round_gradient"
                android:src="@drawable/ic_keyboard_backspace_black_24dp"
                app:layout_constraintBottom_toBottomOf="parent"
                app:layout_constraintStart_toStartOf="parent"
                app:layout_constraintTop_toTopOf="parent" />

            <de.hdodenhof.circleimageview.CircleImageView
                android:id="@+id/user_profile"
                android:layout_width="50dp"
                android:layout_height="50dp"
                android:layout_margin="5dp"
                android:contentDescription="@string/profile_image"
                android:src="@drawable/user"
                app:layout_constraintBottom_toBottomOf="@id/back_button"
                app:layout_constraintStart_toEndOf="@id/back_button"
                app:layout_constraintTop_toTopOf="@id/back_button" />

            <TextView
                android:id="@+id/top_username"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_marginStart="8dp"
                android:layout_marginLeft="8dp"
                android:text="@string/username"
                android:textColor="@color/textColor"
                app:layout_constraintStart_toEndOf="@+id/user_profile"
                app:layout_constraintTop_toTopOf="@+id/user_profile" />

            <ImageView
                android:id="@+id/audioCall"
                android:layout_width="40dp"
                android:layout_height="40dp"
                android:layout_margin="5dp"
                android:contentDescription="@string/audio_call_image"
                app:layout_constraintBottom_toBottomOf="@+id/videoCall"
                app:layout_constraintEnd_toStartOf="@+id/videoCall"
                app:layout_constraintTop_toTopOf="@+id/videoCall"
                app:srcCompat="@android:drawable/stat_sys_phone_call_forward" />

            <ImageView
                android:id="@+id/videoCall"
                android:layout_width="40dp"
                android:layout_height="40dp"
                android:layout_margin="5dp"
                android:contentDescription="@string/video_call_image"
                android:src="@drawable/camera_pattern"
                app:layout_constraintBottom_toBottomOf="@+id/more_actions"
                app:layout_constraintEnd_toStartOf="@+id/more_actions"
                app:layout_constraintTop_toTopOf="@+id/more_actions" />

            <ImageView
                android:id="@+id/more_actions"
                android:layout_width="40dp"
                android:layout_height="40dp"
                android:layout_margin="5dp"
                android:contentDescription="@string/more_action_image"
                android:src="@drawable/ic_more_vert_black_24dp"
                app:layout_constraintBottom_toBottomOf="parent"
                app:layout_constraintEnd_toEndOf="parent"
                app:layout_constraintTop_toTopOf="parent" />
        </androidx.constraintlayout.widget.ConstraintLayout>

    </androidx.appcompat.widget.Toolbar>

    <LinearLayout
        android:layout_height="match_parent"
        android:layout_width="match_parent"
        android:orientation="vertical">

        <androidx.recyclerview.widget.RecyclerView
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:id="@+id/recyclerView_message_list" />
    </LinearLayout>

这是我的 userChat 适配器代码

public class userChatAdapter extends RecyclerView.Adapter{

    ArrayList<userChatModel> messageModels;
    Context context;
    int SENDER_VIEW_TYPE = 1;
    int RECIEVER_VIEW_TYPE = 2;

    public userChatAdapter(ArrayList<userChatModel> messageModels, Context context) {
        this.messageModels = messageModels;
        this.context = context;
    }

    @NonNull
    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        if(viewType == SENDER_VIEW_TYPE){
            View view = LayoutInflater.from(context).inflate(R.layout.message_sent_layout,parent,false);
            return new senderViewHolder(view);
        }else {
            View view = LayoutInflater.from(context).inflate(R.layout.message_recieved_layout,parent,false);
            return new recieverViewHolder(view);
        }
    }

    @Override
    public int getItemViewType(int position) {
        if (messageModels.get(position).getUserId().equals(FirebaseAuth.getInstance().getCurrentUser())){
            return SENDER_VIEW_TYPE;
        }else {
            return RECIEVER_VIEW_TYPE;
        }
    }

    @Override
    public void onBindViewHolder(@NonNull RecyclerView.ViewHolder holder, int position) {
        userChatModel messageModel = messageModels.get(position);

        if(holder.getClass() == senderViewHolder.class){
            ((senderViewHolder)holder).senderMessage.setText(messageModel.getUserMessage());
        }else {
            ((recieverViewHolder)holder).recieverMessage.setText(messageModel.getUserMessage());
        }
    }

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

    public class recieverViewHolder extends RecyclerView.ViewHolder{

        TextView recieverMessage, recieverTime;

        public recieverViewHolder(@NonNull View itemView) {
            super(itemView);

            recieverMessage = itemView.findViewById(R.id.user_recieved_message);
            recieverTime = itemView.findViewById(R.id.user_recieved_message_time);
        }
    }

    public class senderViewHolder extends RecyclerView.ViewHolder{

        TextView senderMessage, senderTime;
        public senderViewHolder(@NonNull View itemView) {
            super(itemView);

            senderMessage = itemView.findViewById(R.id.user_sent_message);
            senderTime = itemView.findViewById(R.id.user_sent_message_time);
        }
    }
}

我尝试了很多方法,但无法解决错误....我应用了许多 stackoverflow 解决方案。

提前致谢!

【问题讨论】:

  • 你能把firebase代码注释掉,这样RecyclerView就不会显示任何数据并告诉你是否得到同样的错误

标签: java android firebase android-activity android-recyclerview


【解决方案1】:

尝试添加

<androidx.recyclerview.widget.RecyclerView
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:id="@+id/recyclerView_message_list"
          + app:layoutManager="androidx.recyclerview.widget.LinearLayoutManager"
     />

它决定了您的布局是线性、网格还是交错。 您可以在此处找到有关不同 layoutManager 的更多信息: Android - What is Layout Manager?

在文档中: https://developer.android.com/reference/androidx/recyclerview/widget/RecyclerView.LayoutManager

【讨论】:

  • 已添加。但对我不起作用!
  • @its_shyam640,在那之后你还会遇到同样的错误吗?
【解决方案2】:

首先从你的java文件中删除这些行,

LinearLayoutManager layoutManager = new LinearLayoutManager(this,LinearLayoutManager.VERTICAL,false);
recyclerView_message_list.setLayoutManager(layoutManager);

然后在XML下面添加sn-p

<androidx.recyclerview.widget.RecyclerView
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:id="@+id/recyclerView_message_list"
          + app:layoutManager="androidx.recyclerview.widget.LinearLayoutManager"
     />

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-05-13
    • 2023-03-03
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多