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

Damn app crashes, finally solved error-while-inflating-CardView

Preface (First, a smiley face) This problem has been bothering me for a long time. I searched all over with no results. The solutions I got were nothing more than that the AppCompact and CardView package versions were different, and so on, but I didn't have that problem at all. (Afraid they don't take my Gradle seriously?) Problem It's this kind of interface, using RecycleView and Cardview, but it doesn't work on SDK 23 and below. Official docs already state this component is supported down to SDK 9 or 10, anyway basically any android phone can

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

Preface

(First, a smiley face) This problem has been bothering me for a long time. I searched all over with no results. The solutions I got were nothing more than that the AppCompact and CardView package versions were different, and so on, but I didn't have that problem at all. (Afraid they don't take my Gradle seriously?)

Problem

It's this kind of interface, using RecycleView and Cardview, but it doesn't work on SDK 23 and below. Official docs already state this component is supported down to SDK 9 or 10, anyway basically any android phone should work, yet it just doesn't. On SDK 24 and above there's no problem, my own phone has no problem, it's just on the emulator and a few old-version test devices, they all crash. I really want to strangle this Cardview..

Solution

Tonight I spent two hours looking things up, went through Google's official docs, downloaded a few official Samples, but didn't find any valuable solutions. Then I continued searching on Stack Overflow. Stack Overflow is really a programmer's essential helper for home and travel. Finally, hard work paid off, and I found fellow users who encountered the same problem as me.

These two answers were of reference value:

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>

This answer told me to delete android:foreground. I deleted it as instructed, but it still didn't work. However, using this on SDK 23 and below indeed has compatibility issues.

Then this one: https://stackoverflow.com/questions/28541920/error-when-inflating-cardview

From the official notes of appcompat v21

He told me to take a look at the official docs:

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.

Meaning that android:stateListAnimator="@drawable/selector_elevation" doesn't work, it's not supported on older versions of the system.. And then new support is still coming soon? I don't believe it.

So in the end, I deleted both of these lines of code, and a touching scene appeared, it finally worked~~~ The deleted code, shit!

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

_EOF_

Damn app crashes, finally solved 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 条

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

加载评论中…