简体   繁体   English

将 CardView 添加到布局时出错

[英]Error when adding CardView to layout

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.我尝试忽略错误并在 avd 中运行应用程序,虽然一切都编译正常,但布局中没有出现 cardview。 Please help!请帮忙!

Ps I'm using the new Android Studio beta 0.8.0 Ps 我正在使用新的 Android Studio beta 0.8.0

I was able to get it to work on the emulator by changing my build.gradle to this:通过将我的 build.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!确保您已从 SDK Manager 安装了支持存储库! Hope this helps希望这可以帮助

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' 是更好的做法,但不是必需的。

尝试将此行添加到您的RelativeLayout

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

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>

I was able to get this to work by adding the following to my build.gradle: compile 'com.android.support:cardview-v7:21.+'我能够通过将以下内容添加到我的 build.gradle 来使其工作:compile 'com.android.support:cardview-v7:21.+'

Hope this helps.希望这可以帮助。

This is a bug in Android Studio, see https://code.google.com/p/android/issues/detail?id=79071这是 Android Studio 中的一个错误,请参阅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."该补丁的评论之一是:“这导致卡片视图渲染失败。CardView 声明了 CardView.Dark 形式的样式,因此生成的 R 类无法加载。”

Next version should have this fixed according to the bug report (the patch is on branch studio-1.0-dev).下一个版本应该根据错误报告修复这个问题(补丁在分支 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).他们说将项目构建一次作为解决方法,但这并不能解决我的问题(即使我有完全相同的错误和堆栈跟踪)。

Try adding this to your build.gradle file尝试将其添加到您的 build.gradle 文件中

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

Go to app, right click on: Open module -> Settings -> Dependencies.转到应用程序,右键单击:打开模块 -> 设置 -> 依赖项。

Go to "Declared dependencies" and you will find the "+" sign under the title.转到“声明的依赖项”,您会在标题下找到“+”号。

Click on "Library dependencies".单击“库依赖项”。

Search for "Material" and go to "version section".搜索“材料”并转到“版本部分”。

Apply 1.0.0 and click "ok".应用 1.0.0 并单击“确定”。

And then search for card view you will get it on and the class然后搜索卡片视图,您将获得它和课程

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 关于在线性布局中以编程方式添加 cardview - About adding cardview programatically inside linear layout 膨胀CardView时出错 - Error when inflating CardView 错误:如何将Cardview插入回收站片段布局 - Error: How to insert cardview into recycler fragment layout 添加依赖项CardView和RecylerView时发生Gradle错误 - Gradle error while adding the dependencies CardView and RecylerView 将Xamarin文本字段添加到布局时出错 - Error when adding Xamarin Text Field to Layout Robolectric:将按钮添加到layout.xml时,膨胀布局错误 - Robolectric: Error Inflating Layout when adding button to the layout.xml 在片段中使用cardView时,布局充气机会发生错误吗? - While using a cardView in a fragment , an error occurs at layout inflater? 仅当布局中存在 SearchView 时,CardView 上的 java.lang.ClassCastException - java.lang.ClassCastException on CardView only when SearchView is present in layout CardView 设计在布局数据内移动时发生变化 - CardView design changes when moved inside layout data 如何把imageView带到cardview前面? 当两者都是相对布局的孩子时 - How to bring imageView in front of cardview ? When both are of Relative Layout childs
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM