简体   繁体   English

膨胀CardView时出错

[英]Error when inflating CardView

I am using a RecyclerView to hold a list of CardViews. 我正在使用RecyclerView来保存CardViews列表。 On my device running Lollipop 5.0.2, the app runs fine and the list displays the data correctly. 在运行Lollipop 5.0.2的设备上,该应用程序运行良好,并且列表正确显示了数据。 however, on other pre-lollipop devices such as one running 4.4.2, I get the following error: 但是,在其他运行前棒棒糖的设备(例如运行4.4.2的设备)上,出现以下错误:

android.view.InflateException: Binary XML file line #2: Error inflating class android.support.v7.widget.CardView

I've searched up answers on StackOverflow, and most of them say that the problem is with my dependencies, but my build.gradle has all the latest dependencies that I need. 我已经在StackOverflow上搜索了答案,并且大多数人都说问题出在我的依赖项上,但是我的build.gradle具有我需要的所有最新依赖项。

dependencies {
 compile fileTree(dir: 'libs', include: ['*.jar'])
 compile 'com.android.support:appcompat-v7:21.0.3'
 compile 'com.android.support:recyclerview-v7:21.0.3'
 compile 'com.android.support:cardview-v7:21.0.3'
 compile 'com.google.android.gms:play-services:6.5.87'
}

And this is the XML layout that is being referenced to in the LogCat: 这是LogCat中引用的XML布局:

<?xml version="1.0" encoding="utf-8"?>
<android.support.v7.widget.CardView
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:background="@drawable/ripple_background"
    xmlns:card_view="http://schemas.android.com/apk/res-auto"
    card_view:cardUseCompatPadding="true"
    card_view:cardElevation="2dp"
    android:layout_marginLeft="2dp"
    android:id="@+id/news_item_layout"
    android:layout_marginRight="2dp"
    card_view:cardCornerRadius="1dp"
    android:layout_width="match_parent"
    android:layout_height="match_parent">
    ....
</android.support.v7.widget.CardView>

And here is the full LogCat report: 这是完整的LogCat报告:

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.rInflate(LayoutInflater.java:746)
        at android.view.LayoutInflater.inflate(LayoutInflater.java:489)
        at android.view.LayoutInflater.inflate(LayoutInflater.java:396)
        at com.macronews.macro.adapter.TopicsCursorAdapter.onCreateViewHolder(TopicsCursorAdapter.java:85)
        at com.macronews.macro.adapter.TopicsCursorAdapter.onCreateViewHolder(TopicsCursorAdapter.java:41)
        at android.support.v7.widget.RecyclerView$Adapter.createViewHolder(RecyclerView.java:4121)
        at android.support.v7.widget.RecyclerView$Recycler.getViewForPosition(RecyclerView.java:3431)
        at android.support.v7.widget.RecyclerView$Recycler.getViewForPosition(RecyclerView.java:3340)
        at android.support.v7.widget.LayoutState.next(LayoutState.java:83)
        at android.support.v7.widget.StaggeredGridLayoutManager.fill(StaggeredGridLayoutManager.java:1337)
        at android.support.v7.widget.StaggeredGridLayoutManager.onLayoutChildren(StaggeredGridLayoutManager.java:584)
        at android.support.v7.widget.RecyclerView.dispatchLayout(RecyclerView.java:1988)
        at android.support.v7.widget.RecyclerView.onLayout(RecyclerView.java:2237)
        at android.view.View.layout(View.java:13754)
        at android.view.ViewGroup.layout(ViewGroup.java:4364)
        at android.widget.FrameLayout.onLayout(FrameLayout.java:448)
        at android.view.View.layout(View.java:13754)
        at android.view.ViewGroup.layout(ViewGroup.java:4364)
        at android.widget.FrameLayout.onLayout(FrameLayout.java:448)
        at android.view.View.layout(View.java:13754)
        at android.view.ViewGroup.layout(ViewGroup.java:4364)
        at android.widget.FrameLayout.onLayout(FrameLayout.java:448)
        at android.view.View.layout(View.java:13754)
        at android.view.ViewGroup.layout(ViewGroup.java:4364)
        at android.widget.RelativeLayout.onLayout(RelativeLayout.java:948)
        at android.view.View.layout(View.java:13754)
        at android.view.ViewGroup.layout(ViewGroup.java:4364)
        at android.widget.FrameLayout.onLayout(FrameLayout.java:448)
        at android.view.View.layout(View.java:13754)
        at android.view.ViewGroup.layout(ViewGroup.java:4364)
        at android.widget.LinearLayout.setChildFrame(LinearLayout.java:1649)
        at android.widget.LinearLayout.layoutVertical(LinearLayout.java:1507)
        at android.widget.LinearLayout.onLayout(LinearLayout.java:1420)
        at android.view.View.layout(View.java:13754)
        at android.view.ViewGroup.layout(ViewGroup.java:4364)
        at android.widget.FrameLayout.onLayout(FrameLayout.java:448)
        at android.view.View.layout(View.java:13754)
        at android.view.ViewGroup.layout(ViewGroup.java:4364)
        at android.widget.LinearLayout.setChildFrame(LinearLayout.java:1649)
        at android.widget.LinearLayout.layoutVertical(LinearLayout.java:1507)
        at android.widget.LinearLayout.onLayout(LinearLayout.java:1420)
        at android.view.View.layout(View.java:13754)
        at android.view.ViewGroup.layout(ViewGroup.java:4364)
        at android.widget.FrameLayout.onLayout(FrameLayout.java:448)
        at android.view.View.layout(View.java:13754)
        at android.view.ViewGroup.layout(ViewGroup.java:4364)
        at android.view.ViewRootImpl.performLayout(ViewRootImpl.java:1868)
        at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:1689)
        at android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:1000)
        at android.view.ViewRootImpl$TraversalRunnable.run(ViewRootImpl.java:4214)
        at android.view.Choreographer$CallbackRecord.run(Choreographer.java:725)
        at android.view.Choreographer.doCallbacks(Choreographer.java:555)
        at android.view.Choreographer.doFrame(Choreographer.java:525)
        at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:711)
        at android.os.Handler.handleCallback(Handler.java:615)
        at android.os.Handler.dispatchMessage(Handler.java:92)
        at android.os.Looper.loop(Looper.java:137)
        at android.app.ActivityThread.main(ActivityThread.java:4745)
        at java.lang.

EDIT The ripple background drawable is this: 编辑可绘制的涟漪背景是这样的:

<?xml version="1.0" encoding="utf-8"?>
    <ripple
        xmlns:android="http://schemas.android.com/apk/res/android"
        android:color="@color/accent_color"/>

From the official notes of appcompat v21 来自appcompat v21的官方说明

Why are there no ripples on pre-Lollipop? 为什么棒棒糖前没有涟漪? A lot of what allows RippleDrawable to run smoothly is Android 5.0's new RenderThread. Android 5.0的新RenderThread是RippleDrawable可以平稳运行的许多原因。 To optimize for performance on previous versions of Android, we've left RippleDrawable out for now. 为了优化Android早期版本的性能,我们暂时不使用RippleDrawable。

When trying to use ripple-drawables, you get errors on pre-lollipop (that's why it says line 2, it's the xml drawable). 当尝试使用涟漪图可绘制对象时,会在pre-lollipop上出错(这就是为什么它说第2行,它是xml可绘制对象)的原因。 Use a different folder (drawable-v21) for ripples and use selectors in your normal drawables if you want a difference for different states. 如果希望在不同状态下有所不同,请使用其他文件夹(drawable-v21)产生波纹,并在普通可绘制对象中使用选择器。

Official support is not coming soon, I believe, because lollipop has a dedicated render-thread that no other version has. 我相信官方支持不会很快到来,因为lollipop具有专用的渲染线程,而其他版本没有。

Can you try removing following from cardview and check 您可以尝试从Cardview中删除以下内容并检查吗

card_view:cardUseCompatPadding="true"
    card_view:cardElevation="2dp"
  card_view:cardCornerRadius="1dp"

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM