给CardView充气时出错

时间:2015-02-16 12:57:01

标签: java android android-5.0-lollipop android-support-library android-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

我已经在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'
}

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

2 个答案:

答案 0 :(得分:2)

来自appcompat v21

的官方说明
  

前棒棒糖为什么没有涟漪?许多允许RippleDrawable顺利运行的是Android 5.0的新RenderThread。为了优化以前版本Android的性能,我们暂时离开了RippleDrawable。

当尝试使用波纹绘制时,你会在棒棒糖前发生错误(这就是为什么它表示第2行,它是xml drawable)。如果你想要不同状态的差异,请使用不同的文件夹(drawable-v21)来表示涟漪,并在普通的drawable中使用选择器。

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

答案 1 :(得分:0)

您可以尝试从cardview中删除以下内容并检查

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