返回文章列表文章索引阅读全文
STARBLOG / 阅读手记
Android开发

该死的应用闪退,终于解决error-while-inflating-CardView

前言 (先来一个微笑脸)这个问题已经困扰我好久了,之前各种搜索无果,得到的解决方案无非就是 AppCompact 和 CardView 包的版本不一样云云,然而我根本没有这个问题。(怕是不把我Gradle看在眼里?) 问题 就是这么个界面,用到了RecycleView和Cardview,但是在SDK 23以下都不行,官方已经写明这个组件是支持到SDK9还是10的,反正基本是个android手机就可

曦远 (DealiAxy)约 8 分钟
目录3 个章节

前言

(先来一个微笑脸)这个问题已经困扰我好久了,之前各种搜索无果,得到的解决方案无非就是 AppCompact 和 CardView 包的版本不一样云云,然而我根本没有这个问题。(怕是不把我Gradle看在眼里?)

问题

就是这么个界面,用到了RecycleView和Cardview,但是在SDK 23以下都不行,官方已经写明这个组件是支持到SDK9还是10的,反正基本是个android手机就可以,然而,它就是不行,在SDK24以上都没问题,我自己用的手机都没问题,就是在模拟器上和几个旧版本的测试机,都闪退了。 我真的想掐死这个Cardview。。

解决

今天晚上花了两个小时在查资料上,翻了Google的官方文档,下载了几个官方Samples,然而并没有发现什么有价值的解决方案,然后继续在Stack Overflow上搜索,Stack Overflow真的是程序员居家出行必备的好帮手,最后终于功夫不负有心人,找到了和我一样遇到相同问题的网友。

具有参考意义的是这两个回答:

https://stackoverflow.com/questions/38132148/error-while-inflating-class-android-support-v7-widget-cardview

Try this it will help you remove this android:foreground="?android:attr/selectableItemBackground"

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
xmlns:card_view="http://schemas.android.com/apk/res-auto"
android:paddingLeft="4dp"
android:paddingRight="4dp"
android:paddingBottom="2dp">

<android.support.v7.widget.CardView
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:id="@+id/cardview"
    card_view:cardBackgroundColor="@android:color/white"
    card_view:cardElevation="2dp"
    card_view:cardMaxElevation="2dp"
    card_view:cardUseCompatPadding="true">

    <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical">

....here i have a bunch of other layout elements
         </RelativeLayout>
    </android.support.v7.widget.CardView>
</LinearLayout>

这个回答让我把 android:foreground 删掉,我照着删掉了,但是还是不行,但是确实SDK 23以下用这个有兼容性问题。

然后是这个: https://stackoverflow.com/questions/28541920/error-when-inflating-cardview

From the official notes of 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.

When trying to use ripple-drawables, you get errors on pre-lollipop (that's why it says line 2, it's the xml drawable). Use a different folder (drawable-v21) for ripples and use selectors in your normal drawables if you want a difference for different states.

Official support is not coming soon, I believe, because lollipop has a dedicated render-thread that no other version has.

意思就是 android:stateListAnimator="@drawable/selector_elevation"这个不行,在旧版本的系统上不支持。。 然后还是新的支持coming soon?我不信。

所以最后,我把这两行代码都删掉了,感人的一幕出现了,终于可以了~~~ 删掉的代码,shit!

android:stateListAnimator="@drawable/selector_elevation"
android:foreground="?android:attr/selectableItemBackground"

_EOF_

该死的应用闪退,终于解决error-while-inflating-CardView

本文作者:
程序设计实验室
版权声明:
本博客所有文章除特别声明外,均采用 BY-NC-SA 许可协议。转载请注明出处!

相关文章

Android开发:在Linux系统上使用ADB连接Android手机

前言 由于 Linux 系统的安全性设置等原因,非 root 用户使用外部设备或者某些硬件接口会受到限制。Google 官方提供的 SDK 并不能直接工作,如找不到设备或者显示一堆问号以及 Permission Denied。 方法 记得以前有看过 Google 官方提供的解决方案,即将指定的 USB 设备读写权限赋予普通用户。 首先使用 lsusb 命令查看连接到计算机的 usb 设备,找到 A

2021-12-18 · Android开发 · 约 1 分钟

讨论

共 0 条

良言一句三冬暖,恶语伤人六月寒。

加载评论中…