前言:

今天在使用卡片视图CardView,启动APP后出现程序停止运行,报错如下:

Binary XML file line #50: Error inflating class androidx.cardview.widget.CardView

报错显示在xml布局第50行

<androidx.cardview.widget.CardView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="15dp"
android:layout_marginTop="35dp"
android:layout_marginRight="15dp"
android:layout_marginBottom="15dp"
app:cardCornerRadius="4dp"
>
<TextView
android:id="@+id/tv_text"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="10dp"
android:text="我这里是一个卡片布局!" />
</androidx.cardview.widget.CardView>

解决方案:

必须要设置卡片的背景颜色,属性为:​​app:cardBackgroundColor="@color/white"​

<androidx.cardview.widget.CardView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="15dp"
android:layout_marginTop="35dp"
android:layout_marginRight="15dp"
android:layout_marginBottom="15dp"
app:cardCornerRadius="4dp"
app:cardBackgroundColor="@color/white"
>
<TextView
android:id="@+id/tv_text"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="10dp"
android:text="我这里是一个卡片布局!" />
</androidx.cardview.widget.CardView>

这样设置之后重新运行程序就可以了。