向布局中添加CardView时发生错误

时间:2022-09-25 18:39:19

I wanted to try out the new toys that google gave us and I've run into some trouble.

我想试用谷歌给我们的新玩具,但我遇到了一些麻烦。

Here is my build.gradle:

这是我build.gradle:

apply plugin: 'com.android.application'

android {
    compileSdkVersion 'android-L'
    buildToolsVersion "20.0.0"

    defaultConfig {
        applicationId "com.tod.android.lpreviewtest"
        minSdkVersion 'L'
        targetSdkVersion 'L'
        versionCode 1
        versionName "1.0"
    }
    buildTypes {
        release {
            runProguard false
            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
        }
    }
}

dependencies {
    compile fileTree(dir: 'libs', include: ['*.jar'])
    compile 'com.android.support:cardview-v7:+'
}

Now my layout:

现在我的布局:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    android:paddingBottom="@dimen/activity_vertical_margin"
    tools:context=".MyActivity">
    <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>
</RelativeLayout>

When I switch to screen preview in android studio, I get a render error:

当我在android studio中切换到屏幕预览时,我得到一个渲染错误:

Rendering Problems The following classes could not be instantiated:
- android.support.v7.widget.CardView (Open Class, Show Exception)

Exception Details java.lang.ClassFormatError: Illegal field name "CardView.Dark"         
in class android/support/v7/cardview/R$style  at
java.lang.ClassLoader.defineClass1(ClassLoader.java:-2)   at
java.lang.ClassLoader.defineClass(ClassLoader.java:792)   at 
java.lang.ClassLoader.defineClass(ClassLoader.java:635)   at 
java.lang.ClassLoader.loadClass(ClassLoader.java:424)   at 
java.lang.ClassLoader.loadClass(ClassLoader.java:411)   at 
java.lang.ClassLoader.loadClass(ClassLoader.java:357)   at 
android.support.v7.widget.CardView.initialize(CardView.java:69)   at 
android.support.v7.widget.CardView.<init>(CardView.java:60)   at 
java.lang.reflect.Constructor.newInstance(Constructor.java:526)   at 
android.view.LayoutInflater.rInflate_Original(LayoutInflater.java:802)   at 
android.view.LayoutInflater_Delegate.rInflate(LayoutInflater_Delegate.java:64)   at 
android.view.LayoutInflater.rInflate(LayoutInflater.java:778)   at 
android.view.LayoutInflater.inflate(LayoutInflater.java:500)   at 
android.view.LayoutInflater.inflate(LayoutInflater.java:381)

I tried ignoring the error and running the app in an avd and while everything compiles fine, no cardview appears in the layout. Please help!

我尝试忽略这个错误,在avd中运行这个应用程序,当一切都很好地编译时,在布局中没有出现cardview。请帮助!

P.s. I'm using the new Android Studio beta 0.8.0

附注:我正在使用新的Android Studio beta 0.8.0

6 个解决方案

#1


2  

This is a bug in Android Studio, see https://code.google.com/p/android/issues/detail?id=79071

这是Android Studio中的一个bug,参见https://code.google.com/p/android/issues/detail?id=79071

One of the comments of the patch reads: "This was causing card view rendering to fail. CardView declared styles of the form CardView.Dark due to which the generated R class failed to load."

该补丁的一条评论写道:“这导致了card view呈现失败。CardView声明了CardView窗体的样式。由于生成的R类无法加载而导致颜色变暗。

Next version should have this fixed according to the bug report (the patch is on branch studio-1.0-dev). They say to build the project once as a workaround, but this does not fix it for me (even though I have exactly the same error and stack trace).

下一个版本应该根据bug报告将其修复(补丁在branch studio-1.0-dev上)。他们说要构建一个项目作为一个解决方案,但是这并不能为我修复它(尽管我有完全相同的错误和堆栈跟踪)。

#2


30  

I was able to get it to work on the emulator by changing my build.gradle to this:

通过更改构建,我能够让它在模拟器上工作。gradle:

dependencies {
    compile fileTree(dir: 'libs', include: ['*.jar'])
    compile 'com.android.support:cardview-v7:+'
}

Make sure you have the support repository installed from the SDK Manager! Hope this helps

确保从SDK管理器中安装了支持存储库!希望这有助于

EDIT: as someone suggested using 'com.android.support:cardview-v7:23.1' is a better practice but it not required.

编辑:有人建议使用com.android.support:cardview-v7:23.1是一个更好的实践,但不是必需的。

#3


10  

try adding this line to your RelativeLayout

尝试将这一行添加到您的相对论布局中

xmlns:card_view="http://schemas.android.com/apk/res-auto"

#4


4  

It solved me by adding xmlns:card_view="http://schemas.android.com/apk/res-auto"

它通过添加xmlns:card_view="http://schemas.android.com/apk/res-auto"来解决我的问题

        <LinearLayout
            android:orientation="vertical"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            xmlns:card_view="http://schemas.android.com/apk/res-auto"> --->> ******
        ....
        ...
        <android.support.v7.widget.CardView>
    ..
...
.....

        </android.support.v7.widget.CardView>
        </LinearLayout>

#5


3  

I was able to get this to work by adding the following to my build.gradle: compile 'com.android.support:cardview-v7:21.+'

通过在我的构建中添加以下内容,我能够让这个工作起来。gradle:编译“com.android.support:cardview-v7:21 +”。

Hope this helps.

希望这个有帮助。

#6


0  

Try adding this to your build.gradle file

尝试将它添加到您的构建中。gradle文件

dependencies {
...
compile 'com.android.support:cardview-v7:21.0.+'
compile 'com.android.support:recyclerview-v7:21.0.+'
...
}

#1


2  

This is a bug in Android Studio, see https://code.google.com/p/android/issues/detail?id=79071

这是Android Studio中的一个bug,参见https://code.google.com/p/android/issues/detail?id=79071

One of the comments of the patch reads: "This was causing card view rendering to fail. CardView declared styles of the form CardView.Dark due to which the generated R class failed to load."

该补丁的一条评论写道:“这导致了card view呈现失败。CardView声明了CardView窗体的样式。由于生成的R类无法加载而导致颜色变暗。

Next version should have this fixed according to the bug report (the patch is on branch studio-1.0-dev). They say to build the project once as a workaround, but this does not fix it for me (even though I have exactly the same error and stack trace).

下一个版本应该根据bug报告将其修复(补丁在branch studio-1.0-dev上)。他们说要构建一个项目作为一个解决方案,但是这并不能为我修复它(尽管我有完全相同的错误和堆栈跟踪)。

#2


30  

I was able to get it to work on the emulator by changing my build.gradle to this:

通过更改构建,我能够让它在模拟器上工作。gradle:

dependencies {
    compile fileTree(dir: 'libs', include: ['*.jar'])
    compile 'com.android.support:cardview-v7:+'
}

Make sure you have the support repository installed from the SDK Manager! Hope this helps

确保从SDK管理器中安装了支持存储库!希望这有助于

EDIT: as someone suggested using 'com.android.support:cardview-v7:23.1' is a better practice but it not required.

编辑:有人建议使用com.android.support:cardview-v7:23.1是一个更好的实践,但不是必需的。

#3


10  

try adding this line to your RelativeLayout

尝试将这一行添加到您的相对论布局中

xmlns:card_view="http://schemas.android.com/apk/res-auto"

#4


4  

It solved me by adding xmlns:card_view="http://schemas.android.com/apk/res-auto"

它通过添加xmlns:card_view="http://schemas.android.com/apk/res-auto"来解决我的问题

        <LinearLayout
            android:orientation="vertical"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            xmlns:card_view="http://schemas.android.com/apk/res-auto"> --->> ******
        ....
        ...
        <android.support.v7.widget.CardView>
    ..
...
.....

        </android.support.v7.widget.CardView>
        </LinearLayout>

#5


3  

I was able to get this to work by adding the following to my build.gradle: compile 'com.android.support:cardview-v7:21.+'

通过在我的构建中添加以下内容,我能够让这个工作起来。gradle:编译“com.android.support:cardview-v7:21 +”。

Hope this helps.

希望这个有帮助。

#6


0  

Try adding this to your build.gradle file

尝试将它添加到您的构建中。gradle文件

dependencies {
...
compile 'com.android.support:cardview-v7:21.0.+'
compile 'com.android.support:recyclerview-v7:21.0.+'
...
}