android - 片段中的 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>
请帮我解决这个问题。
解决方案
确保在片段的 xml:“fragment_home”中有带有 id:“dataList”的 recyclerview。该错误告诉您 recyclerview 初始化有问题,因此它为 null 并且您尝试从 null 对象引用调用 setLayoutManager() 。
推荐阅读
- flutter - 如何在flutter中从网络图像创建pdf
- keras - 根据论文实现EEGNet卷积神经网络的问题
- php - 在更新 Laravel 时从空值创建默认对象
- permissions - 递归设置默认 ACL 与仅设置默认 ACL 有何意义
- wordpress - 如何在没有插件且不使用自定义字段的情况下在自定义帖子中添加文本字段
- javascript - 如何从中获取(视频)文件使用 javascript 并发送请求 XMLHttpRequest()
- python - 调试:已抓取 <200>...(引用者:无)
- azure - 有没有办法从 workflow_Id 获取逻辑应用名称?
- node.js - 如何将 discord-xp 从每条消息提供 XP 更改为每分钟提供 XP?
- google-sheets - 如何将结果拆分为两个单元格