尽管在 gradle 文件中实现了 CardView class "could not be found"
CardView class "could not be found" despite implementing it in the gradle file
出于某种原因,我的 CardView 找不到 class。我将它用作根布局,并在 gradle 文件中实现了它。我尝试了关于 SO 的所有提示,但没有任何效果:
<?xml version="1.0" encoding="utf-8"?>
<android.support.v7.widget.CardView
xmlns:card_view="http://schemas.android.com/apk/res-auto"
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/item_card_view"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="5dp"
android:layout_marginBottom="5dp"
android:layout_marginStart="10dp"
android:layout_marginEnd="10dp"
android:orientation="horizontal"
card_view:cardCornerRadius="4dp">
<TextView
android:id="@+id/username_display"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/username_display" />
<TextView
android:id="@+id/password_display"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/password_display" />
</android.support.v7.widget.CardView>
这是完整的 Gradle 文件:
android {
compileSdkVersion 28
defaultConfig {
applicationId "com.ozbek.cryptpass"
minSdkVersion 23
targetSdkVersion 28
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-alpha04'
implementation 'com.android.support:design:28.0.1'
implementation 'com.google.android.material:material:1.1.0-alpha05'
implementation 'androidx.constraintlayout:constraintlayout:1.1.3'
implementation 'androidx.recyclerview:recyclerview:1.1.0-alpha04'
implementation 'com.android.support:cardview-v7:28.0.0'
testImplementation 'junit:junit:4.13-beta-2'
androidTestImplementation 'androidx.test:runner:1.2.0-alpha04'
androidTestImplementation 'androidx.test.espresso:espresso-core:3.2.0-alpha4'
}
这是我遇到的错误:
The following classes could not be found:
- android.support.v7.widget.CardView (Fix Build Path, Edit XML, Create Class)
您的 cardView
没有显示,因为您正在设置它的 height="wrap_content"
而没有任何内容可显示,而唯一的 children 是空的,请尝试设置您的 cardView
高度到 60dp
看看它是否会显示一些东西
android:layout_height="60dp"
编辑
我在你的编辑中注意到你正在使用 androidx
所以你应该使用这个 :
implementation 'androidx.cardview:cardview:1.1.0'
并在您的 xml 文件中:
androidx.cardview.widget.CardView
如果您想使用旧的 类:将 android:enableJetifier=true
添加到您的 gradle.properties
并改用 appcompat
依赖项。
出于某种原因,我的 CardView 找不到 class。我将它用作根布局,并在 gradle 文件中实现了它。我尝试了关于 SO 的所有提示,但没有任何效果:
<?xml version="1.0" encoding="utf-8"?>
<android.support.v7.widget.CardView
xmlns:card_view="http://schemas.android.com/apk/res-auto"
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/item_card_view"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="5dp"
android:layout_marginBottom="5dp"
android:layout_marginStart="10dp"
android:layout_marginEnd="10dp"
android:orientation="horizontal"
card_view:cardCornerRadius="4dp">
<TextView
android:id="@+id/username_display"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/username_display" />
<TextView
android:id="@+id/password_display"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/password_display" />
</android.support.v7.widget.CardView>
这是完整的 Gradle 文件:
android {
compileSdkVersion 28
defaultConfig {
applicationId "com.ozbek.cryptpass"
minSdkVersion 23
targetSdkVersion 28
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-alpha04'
implementation 'com.android.support:design:28.0.1'
implementation 'com.google.android.material:material:1.1.0-alpha05'
implementation 'androidx.constraintlayout:constraintlayout:1.1.3'
implementation 'androidx.recyclerview:recyclerview:1.1.0-alpha04'
implementation 'com.android.support:cardview-v7:28.0.0'
testImplementation 'junit:junit:4.13-beta-2'
androidTestImplementation 'androidx.test:runner:1.2.0-alpha04'
androidTestImplementation 'androidx.test.espresso:espresso-core:3.2.0-alpha4'
}
这是我遇到的错误:
The following classes could not be found:
- android.support.v7.widget.CardView (Fix Build Path, Edit XML, Create Class)
您的 cardView
没有显示,因为您正在设置它的 height="wrap_content"
而没有任何内容可显示,而唯一的 children 是空的,请尝试设置您的 cardView
高度到 60dp
看看它是否会显示一些东西
android:layout_height="60dp"
编辑
我在你的编辑中注意到你正在使用 androidx
所以你应该使用这个 :
implementation 'androidx.cardview:cardview:1.1.0'
并在您的 xml 文件中:
androidx.cardview.widget.CardView
如果您想使用旧的 类:将 android:enableJetifier=true
添加到您的 gradle.properties
并改用 appcompat
依赖项。