【问题标题】:RecyclerView ClassNotFoundRecyclerView ClassNotFound
【发布时间】:2021-12-16 01:44:54
【问题描述】:

我尝试将 RecyclerView 和 CardView 添加到我的项目中

dependencies {
compile 'com.android.support:appcompat-v7:21.0.0'
compile 'com.android.support:support-v13:21.0.0'
compile 'com.android.support:cardview-v7:21.0.0'
compile 'com.android.support:recyclerview-v7:21.0.0'
compile 'com.viewpagerindicator:library:2.4.1@aar'
compile project(':facebook')
}

它可以编译,但在设备上运行时出现以下异常

Caused by: java.lang.ClassNotFoundException: Didn't find class "android.view.RecyclerView" on path: DexPathList[[zip file "/data/app/xxxx.apk"],nativeLibraryDirectories=[/data/app-lib/xxxx, /vendor/lib, /system/lib]]
        at dalvik.system.BaseDexClassLoader.findClass(BaseDexClassLoader.java:56)
        at java.lang.ClassLoader.loadClass(ClassLoader.java:497)
        at java.lang.ClassLoader.loadClass(ClassLoader.java:457)
        at android.view.LayoutInflater.createView(LayoutInflater.java:559)
        at android.view.LayoutInflater.onCreateView(LayoutInflater.java:652)

【问题讨论】:

    标签: android


    【解决方案1】:

    您的布局有问题。改变

    <RecyclerView 
       ...
    

    <android.support.v7.widget.RecyclerView
       ...
    

    如果您以编程方式创建 RecyclerView - 请确保您有正确的导入:

    import android.support.v7.widget.RecyclerView;
    

    【讨论】:

    • 是的,看起来我正在使用最新的 buildtools,它确实有 android.view.RecyclerView,所以 android studio 中没有错误,但我在 4.4.4 设备上运行它。 ..
    • 在我的布局中,我写的一样,仍然面临同样的问题。在 CS 类中,我可以找到 android.support.v7.widget 但找不到 android.support.v7.widget.RecyclerView
    【解决方案2】:

    我做了这篇文章中所说的所有内容,但没有任何效果。

    什么对我有用:

    1.在你的 build.gradle 中添加这个:

    compile 'com.android.support:support-v4:21.0.3'
    compile 'com.android.support:appcompat-v7:21.0.0'
    compile 'com.android.support:recyclerview-v7:+'
    compile 'com.android.support:cardview-v7:+'
    

    2.添加RecyclerView作为标准View并注明类:

        <view
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        class="android.support.v7.widget.RecyclerView"
        android:id="@+id/my_recycler_view"
        />
    

    3.然后添加导入:

    import android.support.v7.widget.RecyclerView;
    

    希望这会有所帮助!

    【讨论】:

    • 我忘记了 recyclerview 是它自己的编译行。不需要做其他事情。
    【解决方案3】:

    如果您正在寻找 2019 年的解决方案,您可以尝试将 android.support.v7.widget.RecyclerView 更改为 androidx.recyclerview.widget.RecyclerView。这个对我有用。希望对您有所帮助!

    【讨论】:

      【解决方案4】:

      你可以使用 androidx recyclerview 小部件,这行得通!

       <androidx.recyclerview.widget.RecyclerView
          android:layout_width="match_parent"
          android:layout_height="wrap_content"
          android:layout_marginLeft="10dp"
          android:layout_marginRight="10dp"
          android:scrollbars="vertical"
          android:id="@+id/listExp"
          />
      

      并导入代码,

      import androidx.recyclerview.widget.RecyclerView;
      

      然后就可以使用了,

       private RecyclerView recyclerViewExp;
      

      【讨论】:

        【解决方案5】:

        在 Eclipse 中,您需要从您直接或间接导入的其他库中清除其他(旧)版本的 android-support-v4.jar。

        以我为例;我的项目(TurkRenkleri)包括 MobilePlayGround,其中包括 appcompat 库(具有旧版本的支持 v4 jar)。所以我没有得到下面给出的错误,而是得到了 RecyclerView ClassNotFound。

        在做了this 之后,我终于得到了下面的错误,并且理解了问题因此解决方案(删除旧罐子)。

        [2015-04-29 00:59:53 - TurkRenkleri] Found 2 versions of android-support-v4.jar in the dependency list,
         [2015-04-29 00:59:53 - TurkRenkleri] but not all the versions are identical (check is based on SHA-1 only at this time).
         [2015-04-29 00:59:53 - TurkRenkleri] All versions of the libraries must be the same at this time.
         [2015-04-29 00:59:53 - TurkRenkleri] Versions found are:
         [2015-04-29 00:59:53 - TurkRenkleri] Path: E:\calisma alani\oyunlar\android-support-v7-appcompat\libs\android-support-v4.jar
         [2015-04-29 00:59:53 - TurkRenkleri]   Length: 621451
         [2015-04-29 00:59:53 - TurkRenkleri]   SHA-1: 5896b0a4e377ac4242eb2bc785220c1c4fc052f4
         [2015-04-29 00:59:53 - TurkRenkleri] Path: E:\calisma alani\oyunlar\RecyclerView\libs\android-support-v4.jar
         [2015-04-29 00:59:53 - TurkRenkleri]   Length: 1157388
         [2015-04-29 00:59:53 - TurkRenkleri]   SHA-1: 605c447c20ca216b5556af9f215af5d4bba1b117
         [2015-04-29 00:59:53 - TurkRenkleri] Jar mismatch! Fix your dependencies
        

        【讨论】:

          【解决方案6】:

          确保正在编译 'com.android.support:recyclerview-v7:22.2.0'

          【讨论】:

            【解决方案7】:

            这对我有用。将此添加到应用程序 gradle

            compile 'com.android.support:design:23.1.1'
            

            然后在你的布局中

            <android.support.v7.widget.RecyclerView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:id="@+id/recyclerView"
                android:layout_centerVertical="true"
                android:layout_alignParentLeft="true"
                android:layout_alignParentStart="true" />
            

            在你的活动中

            import android.support.v7.widget.RecyclerView;
            

            在你的 onCreate 中

            RecyclerView recyclerView;
            

            【讨论】:

              【解决方案8】:

              这对我有用:

              compile 'com.android.support:support-v4:21.0.3'
              compile 'com.android.support:appcompat-v7:21.0.0'
              compile 'com.android.support:recyclerview-v7:+'
              compile 'com.android.support:cardview-v7:+'
              

              【讨论】:

              【解决方案9】:

              这是一个建筑问题...

              在少数情况下是的,该代码以前可以正常工作,但突然之间 当我同步并构建一个时停止工作(应用程序启动时崩溃) 旧版本的代码。

              解决方法是关闭并重新启动 Eclipse 并清理项目 并清理所有依赖的库项目。然后它开始工作 再次正确。

              这是 Eclipse 中的某种构建问题,当刷新 项目文件。

              更新:特别是,如果您不小心修改了 “.classpath”文件(恢复到旧版本),Eclipse/Android SDK 可能会感到困惑并且无法正确构建项目。当你重新启动 Eclipse 并清理项目,Eclipse 会重新修改“.classpath” 文件,并正确构建。

              【讨论】:

              猜你喜欢
              • 2016-01-04
              • 1970-01-01
              • 1970-01-01
              • 1970-01-01
              • 1970-01-01
              • 2012-06-30
              • 2011-12-07
              • 2023-03-20
              • 1970-01-01
              相关资源
              最近更新 更多