2

I am facing this InflateException when I try to inflate CardView/RecyclerView on an android device with version 4.4 please note that i am not facing this issue in any device with version more than 4.4 or Kitkat.

Please is the stacktrace of the exception

02-02 01:59:10.526 2288-2288/app.learnkannada.com.learnkannadakannadakali E/AndroidRuntime: FATAL EXCEPTION: main
                                                                                        Process: app.learnkannada.com.learnkannadakannadakali, PID: 2288
                                                                                        android.view.InflateException: Binary XML file line #0: Error inflating class android.support.v7.widget.CardView
                                                                                            at android.view.LayoutInflater.createView(LayoutInflater.java:620)
                                                                                            at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:696)
                                                                                            at android.view.LayoutInflater.rInflate(LayoutInflater.java:755)
                                                                                            at android.view.LayoutInflater.inflate(LayoutInflater.java:492)
                                                                                            at android.view.LayoutInflater.inflate(LayoutInflater.java:397)
                                                                                            at adapter.ListViewAdapter.onCreateViewHolder(ListViewAdapter.java:68)
                                                                                            at adapter.ListViewAdapter.onCreateViewHolder(ListViewAdapter.java:28)
                                                                                            at android.support.v7.widget.RecyclerView$Adapter.createViewHolder(RecyclerView.java:6365)
                                                                                            at android.support.v7.widget.RecyclerView$Recycler.tryGetViewHolderForPositionByDeadline(RecyclerView.java:5563)
                                                                                            at android.support.v7.widget.RecyclerView$Recycler.getViewForPosition(RecyclerView.java:5448)
                                                                                            at android.support.v7.widget.RecyclerView$Recycler.getViewForPosition(RecyclerView.java:5444)
                                                                                            at android.support.v7.widget.LinearLayoutManager$LayoutState.next(LinearLayoutManager.java:2224)
                                                                                            at android.support.v7.widget.LinearLayoutManager.layoutChunk(LinearLayoutManager.java:1551)
                                                                                            at android.support.v7.widget.LinearLayoutManager.fill(LinearLayoutManager.java:1511)
                                                                                            at android.support.v7.widget.LinearLayoutManager.onLayoutChildren(LinearLayoutManager.java:595)
                                                                                            at android.support.v7.widget.RecyclerView.dispatchLayoutStep2(RecyclerView.java:3600)
                                                                                            at android.support.v7.widget.RecyclerView.onMeasure(RecyclerView.java:3036)
                                                                                            at android.view.View.measure(View.java:16497)
                                                                                            at android.widget.RelativeLayout.measureChild(RelativeLayout.java:689)
                                                                                            at android.widget.RelativeLayout.onMeasure(RelativeLayout.java:473)
                                                                                            at android.view.View.measure(View.java:16497)
                                                                                            at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5125)
                                                                                            at android.widget.FrameLayout.onMeasure(FrameLayout.java:310)
                                                                                            at android.support.v7.widget.ContentFrameLayout.onMeasure(ContentFrameLayout.java:139)
                                                                                            at android.view.View.measure(View.java:16497)
                                                                                            at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5125)
                                                                                            at android.support.v7.widget.ActionBarOverlayLayout.onMeasure(ActionBarOverlayLayout.java:393)
                                                                                            at android.view.View.measure(View.java:16497)
                                                                                            at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5125)
                                                                                            at android.widget.FrameLayout.onMeasure(FrameLayout.java:310)
                                                                                            at android.view.View.measure(View.java:16497)
                                                                                            at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5125)
                                                                                            at android.widget.LinearLayout.measureChildBeforeLayout(LinearLayout.java:1404)
                                                                                            at android.widget.LinearLayout.measureVertical(LinearLayout.java:695)
                                                                                            at android.widget.LinearLayout.onMeasure(LinearLayout.java:588)
                                                                                            at android.view.View.measure(View.java:16497)
                                                                                            at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5125)
                                                                                            at android.widget.FrameLayout.onMeasure(FrameLayout.java:310)
                                                                                            at com.android.internal.policy.impl.PhoneWindow$DecorView.onMeasure(PhoneWindow.java:2291)
                                                                                            at android.view.View.measure(View.java:16497)
                                                                                            at android.view.ViewRootImpl.performMeasure(ViewRootImpl.java:1912)
                                                                                            at android.view.ViewRootImpl.measureHierarchy(ViewRootImpl.java:1109)
                                                                                            at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:1291)
                                                                                            at android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:996)
                                                                                            at android.view.ViewRootImpl$TraversalRunnable.run(ViewRootImpl.java:5600)
                                                                                            at android.view.Choreographer$CallbackRecord.run(Choreographer.java:761)
                                                                                            at android.view.Choreographer.doCallbacks(Choreographer.java:574)
                                                                                            at android.view.Choreographer.doFrame(Choreographer.java:544)
                                                                                            at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:747)
                                                                                            at android.os.Handler.handleCallback(Handler.java:733)
                                                                                            at android.os.Handler.dispatchMessage(Handler.java:95)
                                                                                            at android.os.Looper.loop(Looper.java:136)
                                                                                            at android.app.ActivityThread.main(ActivityThread.java:5001)
                                                                                            at java.lang.reflect.Method.invokeNative(Native Method)
                                                                                            at java.lang

is it that RecyclerView/CardView are not supported in Kitkat? or any other reason? Please help.

Below is the code where exception occurred

 LayoutInflater inflater = LayoutInflater.from(parent.getContext());
            v = inflater.inflate(R.layout.row_layout_course, parent, false);

Below is the xml I am trying to inflate.

please find below is the xml code.

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:orientation="vertical"
    android:layout_height="wrap_content">

    <android.support.v7.widget.CardView
        android:foreground="?android:attr/selectableItemBackgroundBorderless"
        app:cardCornerRadius="10dp"
        android:layout_width="match_parent"
        android:layout_margin="@dimen/cardview_compat_inset_shadow"
        android:layout_height="wrap_content"
        tools:targetApi="lollipop">

        <LinearLayout
            android:orientation="horizontal"
            android:layout_width="match_parent"
            android:gravity="center_vertical"
            android:padding="10dp"
            android:layout_height="70dp">

            <LinearLayout
                android:orientation="vertical"
                android:layout_weight="0.9"
                android:layout_width="wrap_content"
                android:paddingLeft="7dp"
                android:layout_height="wrap_content">

                <TextView
                    android:id="@+id/textID"
                    android:text="Test Text"
                    android:textStyle="bold"
                    android:textSize="20sp"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content" />

                <TextView
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:text="number"
                    android:id="@+id/sizeID"/>

            </LinearLayout>

            <ImageView
                android:layout_width="30dp"
                android:layout_height="30dp"
                android:layout_gravity="center_vertical"
                />

        </LinearLayout>

    </android.support.v7.widget.CardView>

</RelativeLayout>
Varun A M
  • 1,103
  • 2
  • 14
  • 29

2 Answers2

2

Try this Remove tools:targetApi="lollipop" from your CardView

And Also Use this

android:foreground="?android:attr/selectableItemBackground"

Instead of this

 android:foreground="?android:attr/selectableItemBackgroundBorderless

CODE

<android.support.v7.widget.CardView
        android:foreground="?android:attr/selectableItemBackground"
        app:cardCornerRadius="10dp"
        android:layout_width="match_parent"
        android:layout_margin="@dimen/cardview_compat_inset_shadow"
        android:layout_height="wrap_content"
        >
AskNilesh
  • 67,701
  • 16
  • 123
  • 163
  • Thank you so much. This helped me but I cannot get selectableItemBackgroundBorderless as it requires me to put targetApi. Now, how can I handle this programmatically so that I still use this effect for android versions above 4.4? – Varun A M Feb 02 '18 at 07:23
  • 1
    Set it inside the direct child of the `CardView` – Debdeep Feb 02 '18 at 07:31
  • How does removing `tools` from the xml help? AFAIK tools' attributes are removed when you create an APK? – Abbas Feb 02 '18 at 07:32
  • @VarunAM this will help you my friend https://stackoverflow.com/questions/37987732/programatically-set-selectableitembackground-on-android-view – AskNilesh Feb 02 '18 at 07:58
  • 1
    @NileshRathod Thank you so much :) – Varun A M Feb 02 '18 at 09:10
  • @VarunAM happy to you – AskNilesh Feb 02 '18 at 09:28
  • 1
    @NileshRathod I have accepted it as answer now :-) Thanks for accepting the edit. If you feel this was a valid question, please upvote question also! – Varun A M Feb 02 '18 at 09:40
0

Make sure to include a dependency for CardView. To add dependency, add following line in your module gradle's depedency part

dependencies {
    compile 'com.android.support:cardview-v7:X.X.X'
}

(replace X with your version)

Make sure you are using vector images in ImageView with app:srcCompat. If you are using vector images as top/bottom/right/left drawable or checkbox button, below 21 Vector images are not directly supported in drawable.

For more info on vector support, you can go through How to use vector drawables in Android API lower 21?

bhumik
  • 231
  • 2
  • 11
  • Hi, I have already added the dependency. this issue is seen only on android device with version below lollipop. The above answer by nilesh has helped me. Please suggest if there is a way to give foreground effect programmatically. – Varun A M Feb 02 '18 at 07:41