Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Error when adding CardView to layout

Tags:

People also ask

How do I add a CardView library?

To add a dependency on CardView, you must add the Google Maven repository to your project. Read Google's Maven repository for more information. For more information about dependencies, see Add build dependencies.

Is CardView deprecated?

CardView : it is provided by the old support libraries and it is deprecated.

Can we use CardView without RecyclerView?

Is it possible to implement a click listener on each button without using a RecyclerView? Yes, You can use cardview as menu item but you should use layout instead of menu. You need to implement individual click for them.

How do I customize my CardView?

Customized CardView First, add a CardView dependency to the application-level build. gradle file. Then create a drawable background for the cards. For that, create a new drawable resource file inside the drawable folder.


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

Here is my 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:

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!

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