简体   繁体   English

错误膨胀类 android.support.v7.widget.RecyclerView

[英]Error inflating class android.support.v7.widget.RecyclerView

I'm trying to use RecyclerView in my existing project, builds without errors but getting no class found error for the RecyclerView while inflating.我正在尝试在我现有的项目中使用 RecyclerView,构建时没有错误,但在膨胀时没有发现 RecyclerView 的类错误。 Cannot see what I'm doing wrong.看不到我做错了什么。 Thanks for helping!感谢您的帮助!

//activity_main.xml //activity_main.xml

<LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_height="match_parent"
    android:layout_width="match_parent"
    android:orientation="vertical">

    <android.support.v7.widget.RecyclerView
        android:id="@+id/recyclerView"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"/>
</LinearLayout>

//MainActivity.onCreate
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        recyclerView = (RecyclerView) findViewById(R.id.recyclerView);
        ItemData itemsData[] = { new ItemData("Help",R.drawable.visa),
                new ItemData("Delete",R.drawable.sample),
                new ItemData("Cloud",R.drawable.sample),
                new ItemData("Favorite",R.drawable.sample),
                new ItemData("Like",R.drawable.sample),
                new ItemData("Rating",R.drawable.sample)};

        // 2. set layoutManger
        recyclerView.setLayoutManager(new LinearLayoutManager(this));
        // 3. create an adapter
        MyAdapter mAdapter = new MyAdapter(itemsData);
        // 4. set adapter
        recyclerView.setAdapter(mAdapter);
        // 5. set item animator to DefaultAnimator
        //recyclerView.setItemAnimator(new DefaultItemAnimator());
        recyclerView.setHasFixedSize(true);
    }

//build.gradle //build.gradle

apply plugin: 'com.android.application'

android {
    compileSdkVersion 20
    buildToolsVersion '19.1.0'

    defaultConfig {
        applicationId "com.domain.project"
        minSdkVersion 19
        targetSdkVersion 20
        versionCode 1
        versionName "1.0"
    }

    buildTypes {
        release {
            runProguard false
            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
        }
    }
}

dependencies {
    compile fileTree(include: ['*.jar'], dir: 'libs')
    compile 'com.android.support:support-v4:+'
    compile 'com.android.support:support-v13:+'
    compile project(':facebook-3.15')
    compile project(':parse-1.5.1')
    compile project(':viewpagerindicator-2.4.1')
    compile 'com.github.manuelpeinado.fadingactionbar:fadingactionbar:3.1.2'
    compile 'com.android.support:cardview-v7:+'
    compile 'com.android.support:recyclerview-v7:+'
    compile 'com.google.android.gms:play-services:+'
}

configurations {
    // to avoid double inclusion of support libraries
    all*.exclude group: 'com.android.support', module: 'support-v4'
}

//LOGCAT //逻辑猫

08-24 17:49:27.626  27544-27544/com.domain.project E/AndroidRuntime﹕ FATAL EXCEPTION: main
    Process: com.domain.project, PID: 27544
    java.lang.RuntimeException: Unable to start activity ComponentInfo{com.domain.project/com.domain.project.MainActivity}: android.view.InflateException: Binary XML file line #7: Error inflating class android.support.v7.widget.RecyclerView
            at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2215)
            at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2264)
            at android.app.ActivityThread.access$800(ActivityThread.java:144)
            at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1205)
            at android.os.Handler.dispatchMessage(Handler.java:102)
            at android.os.Looper.loop(Looper.java:136)
            at android.app.ActivityThread.main(ActivityThread.java:5139)
            at java.lang.reflect.Method.invokeNative(Native Method)
            at java.lang.reflect.Method.invoke(Method.java:515)
            at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:796)
            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:612)
            at dalvik.system.NativeStart.main(Native Method)
     Caused by: android.view.InflateException: Binary XML file line #7: Error inflating class android.support.v7.widget.RecyclerView
            at android.view.LayoutInflater.createView(LayoutInflater.java:620)
            at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:696)
            at android.view.LayoutInflater.rInflate(LayoutInflater.java:755)
            at android.view.LayoutInflater.inflate(LayoutInflater.java:492)
            at android.view.LayoutInflater.inflate(LayoutInflater.java:397)
            at android.view.LayoutInflater.inflate(LayoutInflater.java:353)
            at com.android.internal.policy.impl.PhoneWindow.setContentView(PhoneWindow.java:343)
            at android.app.Activity.setContentView(Activity.java:1929)
            at com.domain.project.MainActivity.onCreate(MainActivity.java:35)
            at android.app.Activity.performCreate(Activity.java:5231)
            at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1087)
            at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2169)
            at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2264)
            at android.app.ActivityThread.access$800(ActivityThread.java:144)
            at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1205)
            at android.os.Handler.dispatchMessage(Handler.java:102)
            at android.os.Looper.loop(Looper.java:136)
            at android.app.ActivityThread.main(ActivityThread.java:5139)
            at java.lang.reflect.Method.invokeNative(Native Method)
            at java.lang.reflect.Method.invoke(Method.java:515)
            at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:796)
            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:612)
            at dalvik.system.NativeStart.main(Native Method)
     Caused by: java.lang.reflect.InvocationTargetException
            at java.lang.reflect.Constructor.constructNative(Native Method)
            at java.lang.reflect.Constructor.newInstance(Constructor.java:423)
            at android.view.LayoutInflater.createView(LayoutInflater.java:594)
            at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:696)
            at android.view.LayoutInflater.rInflate(LayoutInflater.java:755)
            at android.view.LayoutInflater.inflate(LayoutInflater.java:492)
            at android.view.LayoutInflater.inflate(LayoutInflater.java:397)
            at android.view.LayoutInflater.inflate(LayoutInflater.java:353)
            at com.android.internal.policy.impl.PhoneWindow.setContentView(PhoneWindow.java:343)
            at android.app.Activity.setContentView(Activity.java:1929)
            at com.domain.project.MainActivity.onCreate(MainActivity.java:35)
            at android.app.Activity.performCreate(Activity.java:5231)
            at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1087)
            at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2169)
            at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2264)
            at android.app.ActivityThread.access$800(ActivityThread.java:144)
            at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1205)
            at android.os.Handler.dispatchMessage(Handler.java:102)
            at android.os.Looper.loop(Looper.java:136)
            at android.app.ActivityThread.main(ActivityThread.java:5139)
            at java.lang.reflect.Method.invokeNative(Native Method)
            at java.lang.reflect.Method.invoke(Method.java:515)
            at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:796)
            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:612)
            at dalvik.system.NativeStart.main(Native Method)
     Caused by: java.lang.NoClassDefFoundError: android.support.v4.util.Pools$SimplePool
            at android.support.v7.widget.RecyclerView.<init>(RecyclerView.java:121)
            at android.support.v7.widget.RecyclerView.<init>(RecyclerView.java:213)
            at java.lang.reflect.Constructor.constructNative(Native Method)
            at java.lang.reflect.Constructor.newInstance(Constructor.java:423)
            at android.view.LayoutInflater.createView(LayoutInflater.java:594)
            at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:696)
            at android.view.LayoutInflater.rInflate(LayoutInflater.java:755)
            at android.view.LayoutInflater.inflate(LayoutInflater.java:492)
            at android.view.LayoutInflater.inflate(LayoutInflater.java:397)
            at android.view.LayoutInflater.inflate(LayoutInflater.java:353)
            at com.android.internal.policy.impl.PhoneWindow.setContentView(PhoneWindow.java:343)
            at android.app.Activity.setContentView(Activity.java:1929)
            at com.domain.project.MainActivity.onCreate(MainActivity.java:35)
            at android.app.Activity.performCreate(Activity.java:5231)
            at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1087)
            at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2169)
            at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2264)
            at android.app.ActivityThread.access$800(ActivityThread.java:144)
            at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1205)
            at android.os.Handler.dispatchMessage(Handler.java:102)
            at android.os.Looper.loop(Looper.java:136)
            at android.app.ActivityThread.main(ActivityThread.java:5139)
            at java.lang.reflect.Method.invokeNative(Native Method)
            at java.lang.reflect.Method.invoke(Method.java:515)
            at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:796)
            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:612)
            at dalvik.system.NativeStart.main(Native Method)

Or... in my case, I was including the androidx version of RecyclerView in my dependencies (build.gradle) but using the other in my XML... Doh.或者...在我的情况下,我在我的依赖项(build.gradle)中包含了 RecyclerView 的 androidx 版本,但在我的 XML 中使用了另一个... Doh。

Replaced已更换

android.support.v7.widget.RecyclerView

with

androidx.recyclerview.widget.RecyclerView

and it worked!它奏效了! :) :)

In the xml declaration of the RecyclerView: Replace在 RecyclerView 的 xml 声明中:替换

<android.support.v7.widget.RecyclerView

with

<androidx.recyclerview.widget.RecyclerView

Hope this works ^_^希望这有效^_^

Please check your support libs are updated to latest..请检查您的支持库是否更新到最新..

com.android.support:support-v4: and com.android.support:recyclerview-v7 com.android.support:support-v4: 和 com.android.support:recyclerview-v7

for the new API Version, on the build.gradle - project dependencies add :对于新的 API 版本,在 build.gradle - 项目依赖项中添加:

implementation 'androidx.recyclerview:recyclerview:1.0.0'

and in the layout, you should replace :在布局中,您应该替换:

android.support.v7.widget.RecyclerView

with

androidx.recyclerview.widget.RecyclerView

In your layout file:在您的布局文件中:

Use:用:

androidx.recyclerview.widget.RecyclerView androidx.recyclerview.widget.RecyclerView

Instead:反而:

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

It will work for you.它会为你工作。

If you do not want to use Androidx then in your gradle.properties file and do this:如果您不想使用 Androidx,请在您的gradle.properties文件中执行以下操作:

android.useAndroidX=false

android.enableJetifier=false

My problem was that the library was compiled using JDK1.6 instead of 1.7.我的问题是该库是使用 JDK1.6 而不是 1.7 编译的。

Found the solution here link在此处找到解决方案链接

Add Dependencies in gradle file.在 gradle 文件中添加依赖项。

dependencies{
    compile 'com.android.support:appcompat-v7:23.3.0'
    compile 'com.android.support:recyclerview-v7:23.3.0'
}

只需将以下内容添加到您的 proguard-rules.pro 中,

-keep public class android.support.v7.widget.** { *; }

I figured it out.我想到了。 Removing the following configuration in build.gradle makes the recyclerview work.删除 build.gradle 中的以下配置使 recyclerview 工作。 This leads me to another question: android studio: gradle dependency error这让我想到了另一个问题: android studio: gradle dependency error

-- ——

configurations {
    // to avoid double inclusion of support libraries
    all*.exclude group: 'com.android.support', module: 'support-v4'
}

Make sure you have added proper dependencies for recyclerView.确保您已为 recyclerView 添加了适当的依赖项。 Then check whether you have dependecy for cardView.然后检查你是否有 cardView 的依赖。

If yes, then如果是,那么

  • remove card view dependency移除卡片视图依赖

  • sync project同步项目

  • then again add card view dependency然后再次添加卡片视图依赖项

  • clean project清洁工程

  • rebuild project重建项目

It worked for me它对我有用

After trying everything on internet i solved recyclerview inflating error by observing small mistake.在互联网上尝试了一切之后,我通过观察小错误解决了recyclerview膨胀错误。 First, Did you check your android-support-v7-recyclerview.jar library present in libs folder?首先,您是否检查了 libs 文件夹中的android-support-v7-recyclerview.jar库? if it is then check if your manifest file that has target sdk version to 21如果是,则检查您的清单文件的目标 sdk 版本是否为21

<uses-sdk
        android:minSdkVersion="11" //or whatever you need
        android:targetSdkVersion="21" />

may be this can help for someone in future and not wasting days behind it as i did.也许这可以帮助将来的某个人,而不是像我一样浪费时间。

Check out my answer here Error inflating class RecyclerView .在这里查看我的答案Error inflating class RecyclerView In short, you need to import recyclerview and appcompat dependencies and sync before you are trying to add RecyclerView to your XML.简而言之,在尝试将 RecyclerView 添加到 XML之前,您需要导入 recyclerview 和 appcompat 依赖项并进行同步。

--- Quoted from Android website. --- 引自 Android 网站。

Dependencies依赖关系

To use these features in versions of Android earlier than 5.0 (API level 21), include the Android v7 Support Library in your project as a Gradle dependency:要在 5.0(API 级别 21)之前的 Android 版本中使用这些功能,请将 Android v7 支持库作为 Gradle 依赖项包含在您的项目中:

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

For make it work under Eclipse in my case I added RecyclerView as Library in the workspace.为了使它在 Eclipse 下工作,我在工作区中添加了 RecyclerView 作为库。 And then added to my project under ANDROID LIBRARY.然后在ANDROID LIBRARY下添加到我的项目中。 Finally.最后。 Three hours lost.损失了三个小时。

I had a similar same error message, and tried many of the suggested steps from the answers on this site.我有一个类似的错误消息,并尝试了本网站答案中的许多建议步骤。

java.lang.RuntimeException: Unable to start activity ComponentInfo{com.abc.def/com.abc.def.MainActivity}: android.view.InflateException: Binary XML file line #8: java.lang.RuntimeException:无法启动活动 ComponentInfo{com.abc.def/com.abc.def.MainActivity}:android.view.InflateException:二进制 XML 文件第 8 行:

Finally realized that for some reasons the tag in my layout was:终于意识到由于某些原因,我的布局中的标签是:

android.support.v7.internal.widget.RecyclerView 

instead of代替

android.support.v7.widget.RecyclerView. 

I am not sure how 'internal' crept in between v7 and widget, but once I removed that, the error went away.我不确定 v7 和小部件之间的“内部”是如何出现的,但是一旦我删除了它,错误就消失了。 Not saying other people will make the same mistake, but just to let people know that a typo somewhere in your layout xml could be the cause of this error message.不是说其他​​人会犯同样的错误,只是让人们知道布局 xml 中某处的拼写错误可能是导致此错误消息的原因。

Check your layout.xml file...Cant stop my laughing... I made a stupid mistake...检查你的layout.xml文件...忍不住笑了...我犯了一个愚蠢的错误...

I put我放

<android.support.v7.wodget.RecyclerView

instead of代替

<android.support.v7.widget.RecyclerView

look at the small 'o', it wasted me 30 mins or more to check other files... [looking forward my answer can save your time]看那个小'o',检查其他文件浪费了我30分钟或更长时间...... [期待我的回答可以节省你的时间]

Add below line in Gradle file:在 Gradle 文件中添加以下行:

dependencies {
    //recyclerview
    implementation 'androidx.recyclerview:recyclerview:1.0.0'
}

Add below code in your activity layout.xml:在您的活动 layout.xml 中添加以下代码:

<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout 
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent">

<androidx.recyclerview.widget.RecyclerView
    android:id="@+id/recyclerview"
    android:layout_width="0dp"
    android:layout_height="0dp"
    app:layout_constraintBottom_toBottomOf="parent"
    app:layout_constraintEnd_toEndOf="parent"
    app:layout_constraintStart_toStartOf="parent"
    app:layout_constraintTop_toTopOf="parent" />

</androidx.constraintlayout.widget.ConstraintLayout>

Your Android studio auto imports the androidx recyclerview.您的 Android Studio 会自动导入 androidx recyclerview。

AndroidX is a major improvement to the original Android Support Library. AndroidX 是对原始 Android 支持库的重大改进。 Like the Support Library, AndroidX ships separately from the Android OS and provides backwards-compatibility across Android releases.与支持库一样,AndroidX 与 Android 操作系统分开发布,并提供跨 Android 版本的向后兼容性。

Here is the reference AndroidX: https://developer.android.com/jetpack/androidx这里是参考 AndroidX: https : //developer.android.com/jetpack/androidx

I too got same exception.我也有同样的例外。

simple solution is add layout_behaviour to recycler view:简单的解决方案是将layout_behaviour添加到回收站视图:

app:layout_behavior="@string/appbar_scrolling_view_behavior"

暂无
暂无

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

相关问题 蚂蚁,错误膨胀类android.support.v7.widget.RecyclerView - Ant, Error inflating class android.support.v7.widget.RecyclerView 错误膨胀类 android.support.v7.widget.RecyclerView(Eclipse) - Error inflating class android.support.v7.widget.RecyclerView(Eclipse) 收到错误“android.view.InflateException:膨胀 class android.support.v7.widget.RecyclerView 时出错” - recieving error “android.view.InflateException: Error inflating class android.support.v7.widget.RecyclerView” 将.aar内的类android.support.v7.widget.RecyclerView膨胀时出错 - Error inflating class android.support.v7.widget.RecyclerView inside an .aar 启动活动时错误膨胀类 android.support.v7.widget.RecyclerView - Error inflating class android.support.v7.widget.RecyclerView when starting activity 无法解决错误膨胀类android.support.v7.widget.RecyclerView - Not able to resolve Error inflating class android.support.v7.widget.RecyclerView 没有找到类“android.support.v7.widget.RecyclerView - Didn't find class "android.support.v7.widget.RecyclerView android.support.v7.widget.RecyclerView$ViewHolder 的 class 文件 - class file for android.support.v7.widget.RecyclerView$ViewHolder eclipse中无法启动android.support.v7.widget.Recyclerview错误 - android.support.v7.widget.Recyclerview could not be initiated error in eclipse RecyclerView没有LayoutManager android.support.v7.widget.RecyclerView - RecyclerView has no LayoutManager android.support.v7.widget.RecyclerView
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM