You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Kotlin中RecyclerView无LayoutManager导致应用崩溃问题求助

Hey there, let's fix this crash right away! The root cause here is a misplaced attribute in your RecyclerView XML layout, which is triggering an InflateException before your code even gets a chance to set the LayoutManager.

What's Causing the Crash?

You added android:orientation="horizontal" to your RecyclerView in activity_home.xml — but RecyclerView doesn't recognize this attribute! This throws off the XML inflater immediately, as it can't resolve this invalid property, and it incorrectly reports that "RecyclerView has no LayoutManager" as a result.

Step-by-Step Fixes

1. Remove the Invalid Attribute from RecyclerView XML

Open activity_home.xml and delete the line android:orientation="horizontal" from your RecyclerView tag. Your corrected RecyclerView should look like this:

<androidx.recyclerview.widget.RecyclerView
    android:id="@+id/recyclerView"
    android:layout_width="match_parent"
    android:layout_height="150dp"
    android:background="@color/browser_actions_title_color"
    app:layout_constraintTop_toTopOf="parent"
    app:layout_constraintStart_toStartOf="parent"
    app:layout_constraintEnd_toEndOf="parent" />

Your code in HomeActivity.kt already correctly sets the horizontal layout direction via LinearLayoutManager.HORIZONTAL, so we don't need this XML attribute at all.

2. Fix Your CardView Layout (Bonus Bug Fix)

I noticed another issue in preview_card.xml: you're using ConstraintLayout attributes (like app:layout_constraintStart_toStartOf) but the root view is a CardView, not a ConstraintLayout. Those constraints are doing nothing right now, and your views are stacked on top of each other! Wrap the CardView's contents in a ConstraintLayout to fix this:

<?xml version="1.0" encoding="utf-8"?>
<androidx.cardview.widget.CardView xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:layout_width="wrap_content"
    android:layout_height="120dp"
    android:padding="10sp"
    app:cardCornerRadius="3dp"
    app:cardElevation="3dp"
    app:cardUseCompatPadding="true">

    <androidx.constraintlayout.widget.ConstraintLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent">

        <ImageView
            android:id="@+id/previewImage"
            android:layout_width="80dp"
            android:layout_height="80dp"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintTop_toTopOf="parent"
            android:src="@drawable/heart"
            android:contentDescription="@string/product_image" />

        <TextView
            android:id="@+id/previewName"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="@string/product_name"
            android:textSize="11sp"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintTop_toBottomOf="@+id/previewImage" />

        <TextView
            android:id="@+id/previewDesc"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="SAMPLE"
            android:textSize="8sp"
            android:textColor="@color/colorPrimary"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintTop_toBottomOf="@+id/previewName"
            tools:ignore="SmallSp" />

        <TextView
            android:id="@+id/previewPrice"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginTop="8dp"
            android:text="@string/samplePrice"
            android:textColor="@color/colorPrimary"
            android:textSize="11sp"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintBottom_toBottomOf="parent" />
    </androidx.constraintlayout.widget.ConstraintLayout>

</androidx.cardview.widget.CardView>

Why This Works

  • Removing the invalid android:orientation attribute fixes the InflateException, letting your code run and apply the LayoutManager you've defined.
  • Adding the ConstraintLayout wrapper ensures your CardView's views are positioned exactly as you intended with the constraint attributes.

内容的提问来源于stack exchange,提问作者lloydyu24

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.04.30 17:37:45