充气 CardView 时出错

Error when inflating CardView

我正在使用 RecyclerView 来保存 CardView 列表。在我的设备 运行 Lollipop 5.0.2 上,应用程序运行良好并且列表正确显示数据。然而,在其他棒棒糖之前的设备上,例如 运行 4.4.2,我收到以下错误:

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

我在 Whosebug 上搜索了答案,大多数人说问题出在我的依赖项上,但我的 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'
}

这是 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>

这里是完整的 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.

编辑 涟漪背景图是这样的:

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

能否尝试从 cardview 中删除关注并检查

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

来自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. To optimize for performance on previous versions of Android, we've left RippleDrawable out for now.

当尝试使用 ripple-drawables 时,你会在 pre-lollipop 上遇到错误(这就是为什么它说第 2 行是 xml 可绘制对象)。如果您想要不同状态的差异,请为涟漪使用不同的文件夹 (drawable-v21) 并在普通可绘制对象中使用选择器。

官方支持不会很快到来,我相信,因为棒棒糖有一个其他版本没有的专用渲染线程。