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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/25477860/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-20 09:28:21  来源:igfitidea点击:

Error inflating class android.support.v7.widget.RecyclerView

androidandroid-recyclerview

提问by nomongo

I'm trying to use RecyclerView in my existing project, builds without errors but getting no class found error for the RecyclerView while inflating. Cannot see what I'm doing wrong. Thanks for helping!

我正在尝试在我现有的项目中使用 RecyclerView,构建时没有错误,但在膨胀时没有发现 RecyclerView 的类错误。看不到我做错了什么。感谢您的帮助!

//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.access0(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.access0(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.access0(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.access0(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)

回答by codingjeremy

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! :)

它奏效了!:)

回答by Eman Sallam

In the xml declaration of the RecyclerView: Replace

在 RecyclerView 的 xml 声明中:替换

<android.support.v7.widget.RecyclerView

with

<androidx.recyclerview.widget.RecyclerView

Hope this works ^_^

希望这有效^_^

回答by sreekumar

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

回答by masbayu

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

回答by Muazzam abbas

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.propertiesfile and do this:

如果您不想使用 Androidx,则在您的gradle.properties文件中执行以下操作:

android.useAndroidX=false

android.enableJetifier=false

回答by Raj Karekar

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'
}

回答by Melbourne Lopes

Just add following into your proguard-rules.pro,

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

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

回答by guy_m

My problem was that the library was compiled using JDK1.6 instead of 1.7.

我的问题是该库是使用 JDK1.6 而不是 1.7 编译的。

Found the solution here link

在此处找到解决方案链接

回答by nomongo

I figured it out. Removing the following configuration in build.gradle makes the recyclerview work. This leads me to another question: android studio: gradle dependency error

我想到了。删除 build.gradle 中的以下配置使 recyclerview 工作。这让我想到了另一个问题:android studio: gradle dependency error

--

——

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

回答by VeeyaaR

Make sure you have added proper dependencies for recyclerView. Then check whether you have dependecy for cardView.

确保您已为 recyclerView 添加了适当的依赖项。然后检查你是否有 cardView 的依赖。

If yes, then

如果是,那么

  • remove card view dependency

  • sync project

  • then again add card view dependency

  • clean project

  • rebuild project

  • 移除卡片视图依赖

  • 同步项目

  • 然后再次添加卡片视图依赖项

  • 清洁工程

  • 重建项目

It worked for me

它对我有用