【问题标题】:Android Studio CardView error: could not be instantiatedAndroid Studio CardView 错误:无法实例化
【发布时间】:2016-10-26 13:50:17
【问题描述】:

每当我将 CardView 放入我的 activity_main.xml 时,我都会收到此错误。

以下类无法实例化:

android.support.v7.widget.CardView(打开类,显示异常,清除缓存)

我已将此添加到 build.gradle 中:

    compile 'com.android.support:design:23.2.1'
    //CardView
    compile 'com.android.support:cardview-v7:25.0.0'
    //RecyclerView
    compile 'com.android.support:recyclerview-v7:25.0.0'

这是我的 activity_main.xml

<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:id="@+id/ScrollView01"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent" >



    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:app="http://schemas.android.com/apk/res-auto"
        xmlns:tools="http://schemas.android.com/tools"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        tools:context="com.example.ivanp.meteostanica.TvrdjavaFragment"
        android:orientation="vertical"
        android:weightSum="1"
        >

        <ImageView
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            app:srcCompat="@drawable/bubanj"
            android:id="@+id/imageView8"
            android:adjustViewBounds="true" />


        <android.support.v7.widget.CardView
            xmlns:android="http://schemas.android.com/apk/res/android"
            xmlns:card_view="http://schemas.android.com/apk/res-auto"
            android:foreground="?android:attr/selectableItemBackground"
            android:clickable="true"
            android:layout_margin="40dp"
            android:id="@+id/card_view"
            android:layout_width="fill_parent"
            android:layout_height="250dp"
            card_view:cardCornerRadius="2dp">

            <TextView
                android:text="TextView"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:id="@+id/textView" />

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

    </LinearLayout>
</ScrollView>

【问题讨论】:

    标签: android android-layout android-studio android-cardview


    【解决方案1】:

    尝试使用相同版本的 appcompat-v7。

    compile 'com.android.support:cardview-v7:23.2.1'
    compile 'com.android.support:recyclerview-v7:23.2.1'
    

    【讨论】:

      【解决方案2】:

      添加appcompat版本。

      com.android.support:appcompat-v7:25.0.0
      

      如果您还没有添加设计支持,也可以添加。

      com.android.support:design:25.0.0
      

      【讨论】:

        【解决方案3】:

        我发现了问题。主题不兼容。我使用默认主题启动了一个新项目,并将其添加到 build.gradle。

            compile 'com.android.support:design:23.2.1'
            compile 'com.android.support:cardview-v7:23.2.1' //CardView
            compile 'com.android.support:recyclerview-v7:23.2.1'//RecyclerView
        

        之后我只是在设计模式下添加了 CardView,它工作正常,没有错误。

        【讨论】:

          【解决方案4】:

          如果缺少样式,请将 style="@style/CardView" 添加到您的 CardView。

          【讨论】:

            猜你喜欢
            • 2014-07-27
            • 1970-01-01
            • 1970-01-01
            • 2018-03-05
            • 2021-08-27
            • 1970-01-01
            • 2015-09-11
            • 1970-01-01
            • 1970-01-01
            相关资源
            最近更新 更多