膨胀类 RecyclerView 时出错

Posted

技术标签:

【中文标题】膨胀类 RecyclerView 时出错【英文标题】:Error inflating class RecyclerView 【发布时间】:2015-04-13 23:07:48 【问题描述】:

所以我的代码只是使用 RecyclerView 创建了一个 CardView 列表。在运行我的代码时,我不断收到一个奇怪的错误,声称我的 xml 中有错误。经过一段时间的修补后,我发现在我的布局文件中,如果我将<RecyclerView> 更改为<android.support.v7.widget.RecyclerView>,一切都会正常工作。为什么会这样? 我的活动。

import android.support.v7.app.ActionBarActivity;
import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.Menu;
import android.view.MenuItem;

import java.util.ArrayList;


public class CardListActivity extends ActionBarActivity 

    @Override
    protected void onCreate(Bundle savedInstanceState) 
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_card_list);
        RecyclerView recyclerView = (RecyclerView)findViewById(R.id.cardList);
        recyclerView.setHasFixedSize(true);

        LinearLayoutManager linearLayoutManager = new LinearLayoutManager(this);
        linearLayoutManager.setOrientation(LinearLayoutManager.VERTICAL);
        recyclerView.setLayoutManager(linearLayoutManager);

        ArrayList<String> list = new ArrayList<>();
        for(int i = 0; i < 20; i++) list.add("Item " + i);
        CardListAdapter cardListAdapter = new CardListAdapter(list);
        recyclerView.setAdapter(cardListAdapter);
    


我的适配器

    import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import java.util.List;

public class CardListAdapter extends RecyclerView.Adapter<CardListAdapter.CardListViewHolder> 

    private List<String> list;

    public CardListAdapter(List<String> list) 
        this.list = list;
    

    @Override
    public CardListViewHolder onCreateViewHolder(ViewGroup viewGroup, int i) 
        View v = LayoutInflater.from(viewGroup.getContext()).inflate(R.layout.card_layout,viewGroup,false);
        return new CardListViewHolder(v);
    

    @Override
    public void onBindViewHolder(CardListViewHolder cardListViewHolder, int i) 
        String s = list.get(i);
        cardListViewHolder.title.setText(s);
    

    @Override
    public int getItemCount() 
        return list.size();
    


    public static class CardListViewHolder extends RecyclerView.ViewHolder 
        TextView title;
        public CardListViewHolder(View itemView) 
            super(itemView);
            title = (TextView)itemView.findViewById(R.id.title);
        
    


请注意,我的布局文件将 &lt;RecyclerView&gt; 更改为 &lt;android.support.v7.widget.RecyclerView&gt; 可修复错误。

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools" android:layout_
android:layout_ android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
android:paddingBottom="@dimen/activity_vertical_margin" tools:context=".CardListActivity">

<RecyclerView
    android:layout_
    android:layout_
    android:id="@+id/cardList">

    </RecyclerView>

当我使用&lt;RecyclerView&gt; 运行时,我收到此错误,.

`Process: com.liquidink.lollipopmaterialui, PID: 7317
    java.lang.RuntimeException: Unable to start activity ComponentInfocom.liquidink.lollipopmaterialui/com.liquidink.lollipopmaterialui.CardListActivity: android.view.InflateException: Binary XML file line #8: Error inflating class RecyclerView
            at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2298)
            at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2360)
            at android.app.ActivityThread.access$800(ActivityThread.java:144)
            at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1278)
            at android.os.Handler.dispatchMessage(Handler.java:102)
            at android.os.Looper.loop(Looper.java:135)
            at android.app.ActivityThread.main(ActivityThread.java:5221)
            at java.lang.reflect.Method.invoke(Native Method)
            at java.lang.reflect.Method.invoke(Method.java:372)
            at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:899)
            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:694)
     Caused by: android.view.InflateException: Binary XML file line #8: Error inflating class RecyclerView
            at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:757)
            at android.view.LayoutInflater.rInflate(LayoutInflater.java:806)
            at android.view.LayoutInflater.inflate(LayoutInflater.java:504)
            at android.view.LayoutInflater.inflate(LayoutInflater.java:414)
            at android.view.LayoutInflater.inflate(LayoutInflater.java:365)
            at android.support.v7.app.ActionBarActivityDelegateBase.setContentView(ActionBarActivityDelegateBase.java:228)
            at android.support.v7.app.ActionBarActivity.setContentView(ActionBarActivity.java:102)
            at com.liquidink.lollipopmaterialui.CardListActivity.onCreate(CardListActivity.java:18)
            at android.app.Activity.performCreate(Activity.java:5933)
            at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1105)
            at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2251)
            at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2360)
            at android.app.ActivityThread.access$800(ActivityThread.java:144)
            at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1278)
            at android.os.Handler.dispatchMessage(Handler.java:102)
            at android.os.Looper.loop(Looper.java:135)
            at android.app.ActivityThread.main(ActivityThread.java:5221)
            at java.lang.reflect.Method.invoke(Native Method)
            at java.lang.reflect.Method.invoke(Method.java:372)
            at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:899)
            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:694)
     Caused by: java.lang.ClassNotFoundException: Didn't find class "android.view.RecyclerView" on path: DexPathList[[zip file "/data/app/com.liquidink.lollipopmaterialui-2/base.apk"],nativeLibraryDirectories=[/vendor/lib, /system/lib]]
            at dalvik.system.BaseDexClassLoader.findClass(BaseDexClassLoader.java:56)
            at java.lang.ClassLoader.loadClass(ClassLoader.java:511)
            at java.lang.ClassLoader.loadClass(ClassLoader.java:469)
            at android.view.LayoutInflater.createView(LayoutInflater.java:571)
            at android.view.LayoutInflater.onCreateView(LayoutInflater.java:665)
            at com.android.internal.policy.impl.PhoneLayoutInflater.onCreateView(PhoneLayoutInflater.java:65)
            at android.view.LayoutInflater.onCreateView(LayoutInflater.java:682)
            at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:741)
            at android.view.LayoutInflater.rInflate(LayoutInflater.java:806)
            at android.view.LayoutInflater.inflate(LayoutInflater.java:504)
            at android.view.LayoutInflater.inflate(LayoutInflater.java:414)
            at android.view.LayoutInflater.inflate(LayoutInflater.java:365)
            at android.support.v7.app.ActionBarActivityDelegateBase.setContentView(ActionBarActivityDelegateBase.java:228)
            at android.support.v7.app.ActionBarActivity.setContentView(ActionBarActivity.java:102)
            at com.liquidink.lollipopmaterialui.CardListActivity.onCreate(CardListActivity.java:18)
            at android.app.Activity.performCreate(Activity.java:5933)
            at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1105)
            at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2251)
            at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2360)
            at android.app.ActivityThread.access$800(ActivityThread.java:144)
            at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1278)
            at android.os.Handler.dispatchMessage(Handler.java:102)
            at android.os.Looper.loop(Looper.java:135)
            at android.app.ActivityThread.main(ActivityThread.java:5221)
            at java.lang.reflect.Method.invoke(Native Method)
            at java.lang.reflect.Method.invoke(Method.java:372)
            at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:899)
            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:694)
    Suppressed: java.lang.ClassNotFoundException: android.view.RecyclerView
            at java.lang.Class.classForName(Native Method)
            at java.lang.BootClassLoader.findClass(ClassLoader.java:781)
            at java.lang.BootClassLoader.loadClass(ClassLoader.java:841)
            at java.lang.ClassLoader.loadClass(ClassLoader.java:504)
            ... 26 more
     Caused by: java.lang.NoClassDefFoundError: Class not found using the boot class loader; no stack available

`

【问题讨论】:

【参考方案1】:

你应该这样在 XML 中添加 RecyclerView:

<android.support.v7.widget.RecyclerView
    android:layout_
    android:layout_
    android:id="@+id/cardList">

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

希望对你有帮助!

【讨论】:

您仍然需要将 implementation'com.android.support:recyclerview-v7:26.1.0' 作为 @KuRoSan ER 添加到您的 gradle 文件中。说【参考方案2】:

RecyclerView 不包含在基本 Android 框架中,只有基本 Android 框架中的小部件(如 ListViewGridView 等)可以在没有完整命名空间的布局中指定。 RecyclerViewrecyclerview-v7 支持库的一部分。

【讨论】:

【参考方案3】:

我今天遇到了这个问题。 并解决了。

第一步:保持你使用的支持库是相同的版本

compile 'com.android.support:appcompat-v7:23.1.1'
compile 'com.android.support:support-v4:23.1.1'
compile 'com.android.support:recyclerview-v7:23.1.1'

第二步:你应该将 recyclerView 添加到你的 proguard 文件中

-keep class android.support.v7.widget.** *;
// I`ve just keep all widgets

【讨论】:

其他解决方案均无效,将上述行添加到我的 pro-guard 文件中有效。可能值得一提的是,如果您的应用程序在调试版本中工作,而不是在具有回收器视图膨胀异常的发布中,那么上述将肯定是正确的解决方案,因为 proguard 用于发布版本而不是调试。谢谢【参考方案4】:

确保在将 RecyclerView 添加到 XML 之前包含

compile 'com.android.support:recyclerview-v7:22.2.0'
compile 'com.android.support:appcompat-v7:22.2.0'

如果您在添加这些依赖项之前在 XML 中创建了 RecyclerView,要使其正常工作,您应该删除(注释)您的回收站视图,在没有它的情况下构建项目,然后将其添加回来。否则它仍然会在二进制 XML 行#n 中显示 Inflate 异常。

【讨论】:

我正在使用 Eclipse 并面临这个问题。我已将我的问题发布在:***.com/questions/33375943/… 我尝试了您的建议,即首先在 XML 中包含库,然后是回收器视图小部件,但我面临同样的问题。你能帮忙吗【参考方案5】:
    检查 build.gradle 中的参考 在您的布局 xml 文件中,将您的 RecyclerView 组件的名称作为一个整体与它的包一起写入 android.support.v7.widget.RecyclerViewnot like RecyclerView

现在准备吃午饭了。好路线

【讨论】:

以上是关于膨胀类 RecyclerView 时出错的主要内容,如果未能解决你的问题,请参考以下文章

膨胀类 android.support.v7.widget.RecyclerView 时出错

膨胀类 android.support.v7.widget.RecyclerView(Eclipse) 时出错

RecyclerView 挤压子项宽度

膨胀类 ImageView 时出错 - android.view.InflateException:二进制 XML 文件第 10 行:膨胀类 ImageView 时出错

膨胀类 CollapsingToolbarLayout 时出错

膨胀内部类视图时出错