使用Android卡视图会导致应用崩溃

时间:2019-12-27 16:32:24

标签: java android cardview

我尝试将名片视图集成到我的布局中,但是应用程序崩溃并出现以下错误:

java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.android/com.example.android.MainActivity}: android.view.InflateException: Binary XML file line #35: Binary XML file line #43: Error inflating class android.support.v7.widget.CardView

这是我正在使用的布局xml:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:orientation="vertical"
    android:background="@drawable/backdrop_rounded_front"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:elevation="2dp"
    xmlns:card_view="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    >


    <TextView
        android:id="@+id/category"
        android:text="Category: All"
        android:layout_gravity="left"
        android:layout_width="match_parent"
        android:textSize="12sp"
        android:paddingTop="16dp"
        android:paddingBottom="16dp"
        android:layout_height="wrap_content"
        android:textColor="@color/colorPrimary"
        app:textAllCaps="true"
        android:paddingLeft="16dp"
        android:paddingRight="16dp"
        />

    <TextView
        android:id="@+id/question"
        android:text="Empty question"
        android:layout_gravity="center"
        android:layout_width="match_parent"
        android:textSize="24sp"
        android:layout_height="wrap_content"
        android:paddingBottom="16dp"
        android:paddingLeft="16dp"
        android:paddingRight="16dp"
        />


    <!-- A CardView that contains a TextView -->
    <android.support.v7.widget.CardView
        xmlns:card_view="http://schemas.android.com/apk/res-auto"
        android:id="@+id/card_view"
        android:layout_gravity="center"
        android:layout_width="200dp"
        android:layout_height="200dp"
        card_view:cardCornerRadius="4dp">

        <TextView
            android:id="@+id/info_text"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:text="Empty "
            />
    </android.support.v7.widget.CardView>

    <ListView
        android:id="@+id/simpleListView"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:background="#E4F4FF"
        android:divider="@null"
        />

</LinearLayout>

我添加了cardview的依赖关系,但是该依赖关系用红色下划线标出。作为建议,它说迁移到android x。如果我这样做,什么也没发生。

dependencies {
    implementation fileTree(dir: 'libs', include: ['*.jar'])
    implementation 'androidx.appcompat:appcompat:1.1.0'
    implementation 'androidx.constraintlayout:constraintlayout:1.1.3'
    testImplementation 'junit:junit:4.12'
    implementation 'com.android.support:cardview-v7:28.0.0'
    androidTestImplementation 'androidx.test:runner:1.2.0'
    androidTestImplementation 'androidx.test.espresso:espresso-core:3.2.0'
    implementation 'com.github.roiacult:BackdropLayout:1.2'
    implementation 'com.google.android.material:material:1.0.0'
}

我正在使用Android Studio 3.5 编译SDK版本:29 生成工具版本:29.0.1 最低SDK版本:19 使用Java代码

谢谢! (:

2 个答案:

答案 0 :(得分:1)

您已迁移到androidx,但继续对CardView使用支持库。在依赖项中更改此导入

implementation 'androidx.cardview:cardview:1.0.0'

还更改XML文件中的根目录

<androidx.cardview.widget.CardView
        android:id="@+id/card_view"
        android:layout_gravity="center"
        android:layout_width="200dp"
        android:layout_height="200dp"
        app:cardCornerRadius="4dp">

        <TextView
            android:id="@+id/info_text"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:text="Empty "
            />
</androidx.cardview.widget.CardView>

答案 1 :(得分:1)

请勿将AndroidX库与support库混合使用。 在您的模块级别build.gradle中删除

implementation 'com.android.support:cardview-v7:28.0.0'

在您的布局中尝试使用

androidx.cardview.widget.CardView

代替

android.support.v7.widget.CardView