二进制 XML 文件第 2 行:在 android<21 in android studio 中膨胀类 android.support.v7.widget.CardView 时出错

Posted

技术标签:

【中文标题】二进制 XML 文件第 2 行:在 android<21 in android studio 中膨胀类 android.support.v7.widget.CardView 时出错【英文标题】:Binary XML file line #2: Error inflating class android.support.v7.widget.CardView in android<21 in android studio 【发布时间】:2018-05-03 19:16:41 【问题描述】:

以下代码在大于 21(棒棒糖)的 android 中运行良好,

@Override
public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) 

    View v = LayoutInflater.from(parent.getContext()).inflate(R.layout.card_course_not_bought, parent, false);

    return new ViewHolder(v);

但在 android

我用android studio,导入CardView的库

11-20 13:39:43.418 5851-5851 E/AndroidRuntime: FATAL EXCEPTION: main
                                           android.view.InflateException: Binary XML file line #2: Error inflating class android.support.v7.widget.CardView
                                               at android.view.LayoutInflater.createView(LayoutInflater.java:613)
                                               at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:687)
                                               at android.view.LayoutInflater.inflate(LayoutInflater.java:466)
                                               at android.view.LayoutInflater.inflate(LayoutInflater.java:396)
                                               at ir.dpsoft.lms.RecyclerViewAdapter.RecyclerNotBoughtCourses.onCreateViewHolder(RecyclerNotBoughtCourses.java:55)
                                               at android.support.v7.widget.RecyclerView$Adapter.createViewHolder(RecyclerView.java:6488)
                                               at android.support.v7.widget.RecyclerView$Recycler.tryGetViewHolderForPositionByDeadline(RecyclerView.java:5675)
                                               at android.support.v7.widget.RecyclerView$Recycler.getViewForPosition(RecyclerView.java:5558)
                                               at android.support.v7.widget.RecyclerView$Recycler.getViewForPosition(RecyclerView.java:5554)
                                               at android.support.v7.widget.LinearLayoutManager$LayoutState.next(LinearLayoutManager.java:2229)
                                               at android.support.v7.widget.LinearLayoutManager.layoutChunk(LinearLayoutManager.java:1556)
                                               at android.support.v7.widget.LinearLayoutManager.fill(LinearLayoutManager.java:1516)
                                               at android.support.v7.widget.LinearLayoutManager.onLayoutChildren(LinearLayoutManager.java:583)
                                               at android.support.v7.widget.RecyclerView.dispatchLayoutStep2(RecyclerView.java:3694)
                                               at android.support.v7.widget.RecyclerView.onMeasure(RecyclerView.java:3110)
                                               at android.view.View.measure(View.java:15518)
                                               at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:4825)
                                               at android.widget.LinearLayout.measureChildBeforeLayout(LinearLayout.java:1404)
                                               at android.widget.LinearLayout.measureVertical(LinearLayout.java:695)
                                               at android.widget.LinearLayout.onMeasure(LinearLayout.java:588)
                                               at android.view.View.measure(View.java:15518)
                                               at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:4825)
                                               at android.widget.LinearLayout.measureChildBeforeLayout(LinearLayout.java:1404)
                                               at android.widget.LinearLayout.measureVertical(LinearLayout.java:695)
                                               at android.widget.LinearLayout.onMeasure(LinearLayout.java:588)
                                               at android.view.View.measure(View.java:15518)
                                               at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:4825)
                                               at android.widget.LinearLayout.measureChildBeforeLayout(LinearLayout.java:1404)
                                               at android.widget.LinearLayout.measureVertical(LinearLayout.java:695)
                                               at android.widget.LinearLayout.onMeasure(LinearLayout.java:588)
                                               at android.view.View.measure(View.java:15518)
                                               at android.widget.ScrollView.measureChildWithMargins(ScrollView.java:1217)
                                               at android.widget.FrameLayout.onMeasure(FrameLayout.java:310)
                                               at android.widget.ScrollView.onMeasure(ScrollView.java:321)
                                               at android.view.View.measure(View.java:15518)
                                               at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:4825)
                                               at android.widget.LinearLayout.measureChildBeforeLayout(LinearLayout.java:1404)
                                               at android.widget.LinearLayout.measureVertical(LinearLayout.java:695)
                                               at android.widget.LinearLayout.onMeasure(LinearLayout.java:588)
                                               at android.view.View.measure(View.java:15518)
                                               at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:4825)
                                               at android.widget.FrameLayout.onMeasure(FrameLayout.java:310)
                                               at android.view.View.measure(View.java:15518)
                                               at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:4825)
                                               at android.widget.LinearLayout.measureChildBeforeLayout(LinearLayout.java:1404)
                                               at android.widget.LinearLayout.measureVertical(LinearLayout.java:695)
                                               at android.widget.LinearLayout.onMeasure(LinearLayout.java:588)
                                               at android.view.View.measure(View.java:15518)
                                               at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:4825)
                                               at android.widget.LinearLayout.measureChildBeforeLayout(LinearLayout.java:1404)
                                               at android.widget.LinearLayout.measureVertical(LinearLayout.java:695)
                                               at android.widget.LinearLayout.onMeasure(LinearLayout.java:588)
                                               at android.view.View.measure(View.java:15518)
                                               at android.support.v4.widget.DrawerLayout.onMeasure(DrawerLayout.java:1060)

【问题讨论】:

显示你的 xml 文件 这是完整的 logcat 吗?通常有更多关于Caused by的信息 向我们展示你的 graddle 文件 【参考方案1】:

将这些依赖项添加到您的 gradle 文件中:

    compile 'com.android.support:appcompat-v7:27.0.0'
    compile 'com.android.support:cardview-v7:27.0.0'

有关早期版本的兼容性,请参阅此文档:

https://developer.android.com/training/material/compatibility.html

【讨论】:

我正在阅读有关兼容性的信息:developer.android.com/training/material/compatibility.html 在哪里?我没有看到他的 gradle 文件,可能他没有包含 appcompat-v7 如果不包含这些库,应用程序将无法运行并具有此堆栈跟踪。 我认为该应用程序只能与 cardview-v7 一起运行,但如果他想要向后兼容该应用程序需要 appcompat-v7 Appcompat-v7 是 CardView 库的传递依赖

以上是关于二进制 XML 文件第 2 行:在 android<21 in android studio 中膨胀类 android.support.v7.widget.CardView 时出错的主要内容,如果未能解决你的问题,请参考以下文章

二进制 XML 文件第 2 行:在 android<21 in android studio 中膨胀类 android.support.v7.widget.CardView 时出错

android.view.InflateException:二进制 XML 文件第 15 行:二进制 XML 文件第 19 行:膨胀类片段时出错

android.view.inflateexception 二进制 xml 文件第 1 行错误膨胀类 android.widget.relativeLayout

android.view.inflateexception 二进制 xml 文件第 7 行错误膨胀类片段

android.view.InflateException 膨胀类片段时出错,二进制 XML 文件第 1 行?

Android.view.InflateException:二进制 XML 文件第 25 行:膨胀类 ImageButton、Resource NotFoundException、XmlPullPar