【问题标题】:Error inflating class android.support.v7.widget.CardView, recyclerView and SwipeRefreshLayout [duplicate]膨胀类 android.support.v7.widget.CardView、recyclerView 和 SwipeRefreshLayout 时出错 [重复]
【发布时间】:2019-08-18 13:15:10
【问题描述】:

我正在做安卓项目

现在在我的布局中出现了这个错误:

The following classes could not be found:
- android.support.v4.widget.SwipeRefreshLayout (Fix Build Path, Edit XML, Create Class)
- android.support.v7.widget.CardView (Fix Build Path, Edit XML, Create Class)
- android.support.v7.widget.RecyclerView (Fix Build Path, Edit XML, Create Class)

我在 stackOverFlow 和其他中尝试了许多 selotions,但我未能解决我的问题。

我在 logcat 中也有这个致命错误:

2019-08-18 15:36:08.069 13405-13405/com.example.souqsenae E/AndroidRuntime: 致命异常: main 进程:com.example.souqsenae,PID:13405 java.lang.RuntimeException:无法启动活动 ComponentInfo{com.example.souqsenae/com.example.souqsenae.MainActivity}:android.view.InflateException:二进制 XML 文件第 20 行:二进制 XML 文件第 20 行:膨胀类错误android.support.v7.widget.CardView 在 android.app.ActivityThread.performLaunchActivity(ActivityThread.java:3364) 在 android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3548) 在 android.app.servertransaction.LaunchActivityItem.execute(LaunchActivityItem.java:86) 在 android.app.servertransaction.TransactionExecutor.executeCallbacks(TransactionExecutor.java:108) 在 android.app.servertransaction.TransactionExecutor.execute(TransactionExecutor.java:68) 在 android.app.ActivityThread$H.handleMessage(ActivityThread.java:2155) 在 android.os.Handler.dispatchMessage(Handler.java:109) 在 android.os.Looper.loop(Looper.java:207) 在 android.app.ActivityThread.main(ActivityThread.java:7539) 在 java.lang.reflect.Method.invoke(本机方法) 在 com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:524) 在 com.android.internal.os.ZygoteInit.main(ZygoteInit.java:958) 引起:android.view.InflateException:二进制 XML 文件第 20 行:二进制 XML 文件第 20 行:错误膨胀类 android.support.v7.widget.CardView 引起:android.view.InflateException:二进制 XML 文件第 20 行:错误膨胀类 android.support.v7.widget.CardView 引起:java.lang.ClassNotFoundException:在路径上找不到类“android.support.v7.widget.CardView”:DexPathList[[zip file“/data/app/com.example.souqsenae-0cR1gMxdpqx9a6GmZb9xEw==/base .apk"],nativeLibraryDirectories=[/data/app/com.example.souqsenae-0cR1gMxdpqx9a6GmZb9xEw==/lib/arm64, /system/lib64, /product/lib64]]

这是我的布局:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout android:layout_width="match_parent"
    android:layout_height="wrap_content"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:android="http://schemas.android.com/apk/res/android">
    <LinearLayout
        android:layout_width = "match_parent"
        android:layout_height = "70dp"
        android:elevation="24dp"
        android:padding="8dp"
        android:background="@drawable/white_action_bar"
        android:id="@+id/first"
        android:orientation="horizontal"
        android:weightSum="1">
          <androidx.appcompat.widget.AppCompatEditText
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:drawableEnd="@drawable/ic_my_location_black_24dp"
            android:drawableStart="@drawable/ic_search_black_24dp"
            android:drawableTint="@color/gray"
            android:hint="@string/search"
            android:textColorHint="@color/gray"
            android:background="@drawable/stroke_gray"
            android:padding="8dp"
            android:layout_margin="2dp"/>
            </LinearLayout>
            <LinearLayout
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:background="@drawable/white_action_bar"
                android:layout_below="@id/first"
                android:orientation="horizontal"
                android:layout_marginLeft="24dp"
                android:layout_marginRight="24dp"
                android:weightSum="1"
                android:id="@+id/taskBar">

                <Button
                    android:layout_width="0dp"
                    android:layout_height="wrap_content"
                    android:text="@string/new_"
                    android:background="@android:color/transparent"
                    android:textColor="@color/colorAccent"
                    android:layout_weight="0.45"
                    android:id="@+id/btnAvailable"/>
                <TextView
                    android:layout_width="0dp"
                    android:layout_height="wrap_content"
                    android:layout_weight="0.1"
                    android:gravity="center"
                    android:text="@string/small_divider"
                    android:textColor="@color/colorPrimaryDark"/>

                <Button
                    android:layout_width="0dp"
                    android:layout_height="wrap_content"
                    android:text="@string/used"
                    android:background="@android:color/transparent"
                    android:textColor="@color/colorPrimaryDark"
                    android:layout_weight="0.45"
                    android:id="@+id/btnMyJob"/>

            </LinearLayout>
            <LinearLayout
                android:orientation="vertical"
                android:elevation="24dp"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:clickable="true"
                android:focusable="true"
                android:id="@+id/layout_task_item"
                android:layout_below="@id/taskBar">

                <android.support.v7.widget.CardView
                    android:layout_width="match_parent"
                    android:layout_height="100dp"
                    android:id="@+id/view2"
                    app:cardCornerRadius="15dp"
                    android:innerRadius="0dp"
                    android:shape="rectangle"
                    android:thicknessRatio="1.9"
                    android:layout_margin="8dp"
                    >

                    <ImageView
                        android:layout_height="match_parent"
                        android:layout_width="wrap_content"
                        android:id="@+id/imageView1"
                        android:src="@drawable/ic_image_black_24dp"

                        android:layout_alignParentTop="true"
                        android:layout_centerHorizontal="true">
                    </ImageView>

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

                <LinearLayout
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content">
                    <LinearLayout
                        android:layout_width="match_parent"
                        android:layout_height="wrap_content"
                        android:orientation="vertical">
                        <LinearLayout
                            android:layout_width="match_parent"
                            android:layout_height="wrap_content"
                            android:orientation="vertical"
                            >
                            <TextView
                                android:layout_width="match_parent"
                                android:layout_height="wrap_content"
                                android:text="@string/app_name"
                                android:padding="8dp"
                                android:textSize="20sp"
                                android:textColor="@color/colorPrimaryDark"
                                android:id="@+id/tvClientName"
                                android:textStyle="bold" />
                            <TextView
                                android:layout_width="match_parent"
                                android:layout_height="wrap_content"
                                android:text="@string/app_name"
                                android:padding="8dp"
                                android:textSize="12sp"
                                android:textColor="@color/colorPrimaryDark"
                                android:id="@+id/tvTaskType"
                                android:textStyle="italic"/>
                            <TextView
                                android:layout_width="match_parent"
                                android:layout_height="wrap_content"
                                android:text="@string/app_name"
                                android:padding="8dp"
                                android:textSize="12sp"
                                android:textColor="@color/black"
                                android:id="@+id/tvTaskDesc"
                                android:textStyle="italic"
                                android:visibility="gone"/>

                        </LinearLayout>

                    </LinearLayout>
                </LinearLayout>


            </LinearLayout>
            <RelativeLayout
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_below="@+id/layout_task_item"
                android:gravity="center"
                android:id="@+id/listContainer"
                android:layout_marginBottom="60dp">

                <TextView
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:id="@+id/no_data"
                    android:text="@string/no_data"
                    android:textSize="24sp"
                    android:textAlignment="center"
                    android:gravity="center"
                    android:textColor="@color/colorPrimary" />
                <ProgressBar
                    android:foregroundGravity="center"
                    android:id="@+id/progress"
                    android:layout_below="@+id/no_data"
                    android:layout_width="40dp"
                    android:layout_height="40dp"
                    android:background="@android:color/transparent"
                    android:layout_centerInParent="true"/>




                <android.support.v4.widget.SwipeRefreshLayout
                    android:id="@+id/swipe_container"
                    android:layout_width="match_parent"
                    android:layout_height="match_parent">
                    <android.support.v7.widget.RecyclerView
                        android:layout_width="match_parent"
                        android:layout_height="wrap_content"
                        android:id="@+id/listTasks"
                        app:layoutManager="android.support.v7.widget.LinearLayoutManager" />
                </android.support.v4.widget.SwipeRefreshLayout>
            </RelativeLayout>
        <LinearLayout
            android:layout_width = "match_parent"
            android:layout_height = "50dp"
            android:padding="2dp"
            android:id="@+id/end"
            android:orientation="horizontal"
            android:background="@drawable/white_menu_bar"
            android:weightSum="1"
            android:gravity="center_horizontal"
            android:layout_alignParentBottom="true">
           <Button
               android:layout_width="0dp"
               android:layout_height="wrap_content"
               android:layout_weight="0.2"
               android:background="@android:color/transparent"
               android:text="@string/home"
               android:textColor="@color/colorPrimaryDark"/>

            <Button
                android:layout_width="0dp"
                android:layout_height="wrap_content"
                android:layout_weight="0.3"
                android:background="@android:color/transparent"
                android:text="@string/my_order"
                android:textColor="@color/colorPrimaryDark"/>
            <Button
                android:layout_width="0dp"
                android:layout_height="wrap_content"
                android:layout_weight="0.3"
                android:background="@android:color/transparent"
                android:text="@string/notifications"
                android:textColor="@color/colorPrimaryDark"/>
            <Button
                android:layout_width="0dp"
                android:layout_height="wrap_content"
                android:layout_weight="0.2"
                android:background="@android:color/transparent"
                android:drawableTop="@drawable/ic_menu_black_24dp"
                android:layout_marginTop="12dp"
                android:drawableTint="@color/colorPrimaryDark"/>



        </LinearLayout>
</RelativeLayout>

这是我的样式文件:

<resources>

<!-- Base application theme. -->
<style name="AppTheme" parent="Base.Theme.AppCompat.Light.DarkActionBar">
    <!-- Customize your theme here. -->
    <item name="colorPrimary">@color/colorPrimary</item>
    <item name="colorPrimaryDark">@color/colorPrimaryDark</item>
    <item name="colorAccent">@color/colorAccent</item>
</style>

我添加了 'Base' 词作为我发现但也不起作用的一个 seotion..

依赖关系:

   implementation fileTree(dir: 'libs', include: ['*.jar'])
implementation 'androidx.appcompat:appcompat:1.1.0-rc01'
testImplementation 'junit:junit:4.13-beta-3'

implementation 'androidx.constraintlayout:constraintlayout:2.0.0-beta2'
testImplementation 'junit:junit:4.13-beta-3'
androidTestImplementation 'androidx.test:runner:1.3.0-alpha02'
androidTestImplementation 'androidx.test.espresso:espresso-core:3.3.0-alpha02'

implementation 'com.android.support:appcompat-v7:29.0.0'
implementation 'com.android.support:design:29.0.0'
implementation 'com.android.support:cardview-v7:29.0.0'

implementation 'com.android.support:support-v4:29.0.0'

有什么帮助吗?

【问题讨论】:

    标签: android


    【解决方案1】:

    首先

    现在您应该知道,随着 Android 9.0(API 级别 28)的发布,有一个名为 AndroidX 的新版本支持库,它是 Jetpack 的一部分

    支持库版本 28.0.0 是 android.support packaging 下的最后一个功能版本,鼓励开发人员迁移到 AndroidX。没有像 ...support:v29.0.0 这样的东西,所以我怀疑您的项目是否会成功构建。

    其次

    你不能在同一个项目中同时使用supportandroidx 库,除非你使用Jetpack 工具,否则肯定会有冲突。使用 Artifacts mapping 将您的支持库迁移到 androidx

    *最后**

    Androidx 布局文件中的引用都应该是 androidx 工件,而不是其他。

    因此标签如下:

      <android.support.v7.widget.CardView
                    android:layout_width="match_parent
    

    应该是:

        <androidx.cardview.widget.CardView
                    android:layout_width="match_parent
    

    【讨论】:

      【解决方案2】:

      试试这个:

      androidx.swiperefreshlayout.widget.SwipeRefreshLayout
      
      androidx.cardview.widget.CardView
      
      androidx.recyclerview.widget.RecyclerView 
      

      而不是

      android.support.v4.widget.SwipeRefreshLayout
      android.support.v7.widget.CardView
      android.support.v7.widget.RecyclerView
      

      【讨论】:

        【解决方案3】:

        您同时使用了支持库和 androidx 库。要么使用支持库,要么使用 androidx

        使用 androidx 库:

         androidx.swiperefreshlayout.widget.SwipeRefreshLayout
        
         androidx.cardview.widget.CardView
        
         androidx.recyclerview.widget.RecyclerView 
        

        而不是

        android.support.v4.widget.SwipeRefreshLayout
        android.support.v7.widget.CardView
        android.support.v7.widget.RecyclerView
        

        并在成绩文件中添加androidx卡片视图、滑动刷新布局和recyclerview的依赖项。

        另外,请参阅迁移到 AndroidX https://developer.android.com/jetpack/androidx/migrate

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 2015-01-06
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2018-09-05
          相关资源
          最近更新 更多