首页 > 解决方案 > 片段中的 RecycleView + CardView

问题描述

我正在尝试在我的 android studio 项目的片段中使用 recycleView + cardView。当我运行该应用程序时,它被强制关闭。有人可以告诉我如何解决这个问题吗?

日志猫

我的代码片段_home.xml

<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    xmlns:app="http://schemas.android.com/apk/res-auto">
    <LinearLayout
        android:id="@+id/layoutMenu"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="vertical"
        tools:ignore="MissingConstraints">
        <androidx.cardview.widget.CardView
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_marginStart="10sp"
            android:layout_marginEnd="10sp"
            android:layout_marginTop="20sp"
            android:layout_marginBottom="5sp"
            android:layout_gravity="center_horizontal"
            app:cardCornerRadius="8sp"
            app:layout_constraintBottom_toBottomOf="parent"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintHorizontal_bias="0.0"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toTopOf="parent"
            app:layout_constraintVertical_bias="0.0">
            <LinearLayout
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:orientation="vertical"
                android:paddingTop="20sp"
                android:paddingBottom="20sp"
                android:gravity="center"
               android:layout_gravity="center_vertical|center_horizontal">
                <ImageView
                    android:id="@+id/image_1"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:src="@drawable/ic_home"/>
                <TextView
                    android:id="@+id/text_1"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:text="@string/news"
                    android:textSize="10sp"
                    android:textStyle="bold"
                    android:textAlignment="center"/>
            </LinearLayout>
        </androidx.cardview.widget.CardView>
    </LinearLayout>
</androidx.constraintlayout.widget.ConstraintLayout>

请帮我解决这个问题。

标签: androidfragment

解决方案


确保在片段的 xml:“fragment_home”中有带有 id:“dataList”的 recyclerview。该错误告诉您 recyclerview 初始化有问题,因此它为 null 并且您尝试从 null 对象引用调用 setLayoutManager() 。


推荐阅读