CardView 在运行时使我的 AS 应用程序崩溃,我该如何正确使用 CardView?

CardView colapsses my AS app at runtime, how can I use CardView appropriately?

我有一个简单的应用程序,它在我 运行 时崩溃了。当我删除 CardView 时,适当地查看应用程序 运行s。

How can I add the CardView view appropriately?

我怀疑 CardView 视图的正确 declecation 应该是不同的:android.support.v7.widget.CardView.

这里是activity.main.xml

<?xml version="1.0" encoding="utf-8"?>


<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="vertical">

    <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.support.v7.widget.CardView>

</LinearLayout>

这里是 build.gradle(模块:app):

apply plugin: 'com.android.application'

android {
    compileSdkVersion 29
    buildToolsVersion "29.0.2"
    defaultConfig {
        applicationId "android.example.blah1111"
        minSdkVersion 15
        targetSdkVersion 29
        versionCode 1
        versionName "1.0"
        testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
    }
    buildTypes {
        release {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
        }
    }
}

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'
    androidTestImplementation 'androidx.test:runner:1.2.0'
    androidTestImplementation 'androidx.test.espresso:espresso-core:3.2.0'
    implementation 'com.android.support:cardview-v7:29.0.0'
}

已编辑

您有 appcompat 依赖项,其中包含 CardView 的正确版本。因此,只需将您的 XML 中的引用从 android.com 更改为 androidx。不要忘记摆脱旧的未使用的依赖项。 也看看这篇文章https://developer.android.com/jetpack/androidx/migrate