片段中的 RecyclerView + CardView
RecycleView + CardView in Fragment
我正尝试在我的 android 工作室项目的片段中使用 recycleView + cardView。当我 运行 应用程序时,它被强制关闭。谁能告诉我如何解决这个问题?
我的代码
fragment_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>
请帮我解决这个问题。
确保您的片段 xml:“fragment_home”中有 ID 为“dataList”的 recyclerview。该错误告诉您您的 recyclerview 初始化有问题,因此它为空,您正试图从空对象引用调用 setLayoutManager()。
我正尝试在我的 android 工作室项目的片段中使用 recycleView + cardView。当我 运行 应用程序时,它被强制关闭。谁能告诉我如何解决这个问题?
我的代码 fragment_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>
请帮我解决这个问题。
确保您的片段 xml:“fragment_home”中有 ID 为“dataList”的 recyclerview。该错误告诉您您的 recyclerview 初始化有问题,因此它为空,您正试图从空对象引用调用 setLayoutManager()。