0

I know it's a very common question and I have looked around to found a solution but nothing have helped me so far.

I am getting this exception only WITH API level 19 (my minSDKVersion is 19 and compile and targetSDK version is 25)

I have tried with emulators running on following API level and everything works fine.

API level 25 - works
API level 23 - works
API level 22 - works
API level 21 - works
API level 19 - crashes with following exception

Below is the exception:

android.view.InflateException: Binary XML file line #9: Error inflating class android.support.v7.widget.CardView
at android.view.LayoutInflater.createView(LayoutInflater.java:621)
at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:697)
at android.view.LayoutInflater.rInflate(LayoutInflater.java:756)
at android.view.LayoutInflater.inflate(LayoutInflater.java:492)
at android.view.LayoutInflater.inflate(LayoutInflater.java:397)
at com.nitinsharma.materiallistview.CardRecycleViewAdapter.onCreateViewHolder(CardRecycleViewAdapter.java:21)
at com.nitinsharma.materiallistview.CardRecycleViewAdapter.onCreateViewHolder(CardRecycleViewAdapter.java:15)
at android.support.v7.widget.RecyclerView$Adapter.createViewHolder(RecyclerView.java:6290)
at android.support.v7.widget.RecyclerView$Recycler.tryGetViewHolderForPositionByDeadline(RecyclerView.java:5478)
at android.support.v7.widget.RecyclerView$Recycler.getViewForPosition(RecyclerView.java:5363)
at android.support.v7.widget.RecyclerView$Recycler.getViewForPosition(RecyclerView.java:5359)
at android.support.v7.widget.LinearLayoutManager$LayoutState.next(LinearLayoutManager.java:2141)
at android.support.v7.widget.LinearLayoutManager.layoutChunk(LinearLayoutManager.java:1525)
at android.support.v7.widget.LinearLayoutManager.fill(LinearLayoutManager.java:1488)
at android.support.v7.widget.LinearLayoutManager.onLayoutChildren(LinearLayoutManager.java:585)
at android.support.v7.widget.RecyclerView.dispatchLayoutStep2(RecyclerView.java:3506)
at android.support.v7.widget.RecyclerView.dispatchLayout(RecyclerView.java:3254)
at android.support.v7.widget.RecyclerView.onLayout(RecyclerView.java:3767)
at android.view.View.layout(View.java:14817)
at android.view.ViewGroup.layout(ViewGroup.java:4631)
at android.widget.RelativeLayout.onLayout(RelativeLayout.java:1055)
at android.view.View.layout(View.java:14817)
at android.view.ViewGroup.layout(ViewGroup.java:4631)
at android.widget.FrameLayout.layoutChildren(FrameLayout.java:453)
at android.widget.FrameLayout.onLayout(FrameLayout.java:388)
at android.view.View.layout(View.java:14817)
at android.view.ViewGroup.layout(ViewGroup.java:4631)
at android.support.v7.widget.ActionBarOverlayLayout.onLayout(ActionBarOverlayLayout.java:437)
at android.view.View.layout(View.java:14817)
at android.view.ViewGroup.layout(ViewGroup.java:4631)
at android.widget.FrameLayout.layoutChildren(FrameLayout.java:453)
at android.widget.FrameLayout.onLayout(FrameLayout.java:388)
at android.view.View.layout(View.java:14817)
at android.view.ViewGroup.layout(ViewGroup.java:4631)
at android.widget.LinearLayout.setChildFrame(LinearLayout.java:1671)
at android.widget.LinearLayout.layoutVertical(LinearLayout.java:1525)
at android.widget.LinearLayout.onLayout(LinearLayout.java:1434)
at android.view.View.layout(View.java:14817)
at android.view.ViewGroup.layout(ViewGroup.java:4631)
at android.widget.FrameLayout.layoutChildren(FrameLayout.java:453)
at android.widget.FrameLayout.onLayout(FrameLayout.java:388)
at android.view.View.layout(View.java:14817)
at android.view.ViewGroup.layout(ViewGroup.java:4631)
at android.view.ViewRootImpl.performLayout(ViewRootImpl.java:1987)
at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:1744)
at android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:1000)
at android.view.ViewRootImpl$TraversalRunnable.run(ViewRootImpl.java:5670)
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:5017)
    at java.lang.reflect.Method.invokeNati

Below is my Gradle:

apply plugin: 'com.android.application'

android {
    compileSdkVersion 25
    buildToolsVersion "25.0.1"
    defaultConfig {
        applicationId "com.nitinsharma.materiallistview"
        minSdkVersion 19
        targetSdkVersion 25
        versionCode 1
        versionName "1.0"
        testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
    }
    buildTypes {
        release {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
        }
    }

    dependencies {
        compile fileTree(dir: 'libs', include: ['*.jar'])
        androidTestCompile('com.android.support.test.espresso:espresso-core:2.2.2', {
            exclude group: 'com.android.support', module: 'support-annotations'
        })
        testCompile 'junit:junit:4.12'


        compile 'com.android.support:cardview-v7:25.+'
        compile 'com.android.support:appcompat-v7:25.+'
        compile 'com.android.support:recyclerview-v7:25.+'
        compile 'com.android.support:cardview-v7:25.+'
        compile 'com.android.support:gridlayout-v7:25.+'
        compile 'com.android.support:support-v4:25.+'
        compile 'com.android.support:design:25.+'
    }

}

and finally my card_view.xml file:

<?xml version="1.0" encoding="utf-8"?>

<RelativeLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"

    xmlns:android="http://schemas.android.com/apk/res/android">

<android.support.v7.widget.CardView
    xmlns:card_view="http://schemas.android.com/apk/res-auto"
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/bottomcardview"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    card_view:cardBackgroundColor="@android:color/transparent"
    card_view:cardElevation="-1dp"
    >


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

    <android.support.v7.widget.CardView
        android:id="@+id/topcardview"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        card_view:cardBackgroundColor="@android:color/white"
        card_view:cardUseCompatPadding="true"
        card_view:cardElevation="6dp"
        card_view:cardCornerRadius="4dp"
        android:transitionName="content"

        >

        <ImageView
            android:id="@+id/cardImage"
            android:layout_width="100dp"
            android:layout_height="match_parent"
            android:layout_gravity="left"
            android:transitionName="image"
            />
        <ImageView
            android:id="@+id/arrow"
            android:layout_width="50dp"
            android:layout_height="match_parent"
            android:layout_gravity="right|bottom"/>
        <TextView
            android:id="@+id/cardtext"
            android:layout_gravity="top|left"
            android:layout_marginLeft="110dp"
            android:textColor="#000"
            android:textSize="22sp"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:shadowColor="@android:color/black"
            android:fontFamily="sans-serif"
            android:elevation="4dp"
            android:textStyle="normal|bold"
            android:textAllCaps="false" />

        <TextView
            android:id="@+id/cardtextsubs"
            android:text="Hello World"
            android:layout_marginLeft="110dp"
            android:layout_marginRight="50dp"
            android:layout_gravity="bottom"
            android:textColor="#000"
            android:textSize="13sp"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:shadowColor="@android:color/black"
            android:fontFamily="sans-serif"
            android:elevation="4dp"
            android:textStyle="normal|italic"
            android:textAllCaps="false" />


        <View
            android:id="@+id/dividerLeft"
            android:layout_marginLeft="100dp"
            android:layout_width="2dp"
            android:layout_height="match_parent"
            android:elevation="8dp"
            android:background="?android:attr/listDivider"/>


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


 </RelativeLayout>

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

</RelativeLayout>

The line in my code where I am getting the error:

public class CardRecycleViewAdapter extends RecyclerView.Adapter<CardViewHolder> {


    @Override
    public CardViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        Log.d("TAG","inside oncreateviewholder");
        **View v = LayoutInflater.from(parent.getContext()).inflate(R.layout.card_view,parent,false);**
        CardViewHolder viewHolder = new CardViewHolder(v,parent);
        return  viewHolder;
    }

My jar files:

enter image description here

My MainActivity:

public class MainActivity extends AppCompatActivity {

    private RecyclerView mRecycleView;
    private LinearLayoutManager mRecycleViewLayout;
    private RecyclerView.Adapter mRecycleViewAdapter;
    protected static int sizex=0;
    protected static int sizey=0;
    protected static int recycleViewHeight=0;
    protected static RecyclerView sharedRecyclerView;
    protected  static Context mContext;



    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);


        mRecycleViewLayout = new LinearLayoutManager(this);
        mRecycleViewAdapter = new CardRecycleViewAdapter();
        mRecycleView = (RecyclerView) findViewById(R.id.recycleViewRight);
        mRecycleView.setHasFixedSize(true);
        mRecycleView.setLayoutManager(mRecycleViewLayout);
        mRecycleView.setAdapter(mRecycleViewAdapter);

....
}
5
  • Your logcat has no Caused by. Look for that reason Commented Jan 23, 2017 at 2:59
  • Hey, that's all I get in the stack trace when the app crashes. There is no caused by. Commented Jan 23, 2017 at 3:03
  • Well, xmlns:card_view and xmlns:android should be on the top level element only, and typically the xmlns:card_view is referred to as xmlns:app because it's not specific to only the cardview Commented Jan 23, 2017 at 3:07
  • Thanks. Tried making both the xmlns changes. No luck. By the way does the gradle look good to you? These version of support library and compileSDK version should work with API level 19 right? They should be good till API 7 if I am not wrong? Commented Jan 23, 2017 at 3:18
  • I know the xmlns doesn't fix anything. I'm just going off line#9 from your exception message. And I generally don't use a plus for version numbers as long as I can avoid it Commented Jan 23, 2017 at 3:21

1 Answer 1

2

That is because card_view:cardElevation="-1dp" value must >=0:

  1. In CardView method initialize(Context context, AttributeSet attrs, int defStyleAttr), it invokes IMPL.initialize(mCardViewDelegate, context, backgroundColor, radius, elevation, maxElevation);
  2. The IMPL like this:

    static {
    if (Build.VERSION.SDK_INT >= 21) {
        IMPL = new CardViewApi21();
    } else if (Build.VERSION.SDK_INT >= 17) {
        IMPL = new CardViewJellybeanMr1();
    } else {
        IMPL = new CardViewGingerbread();
    }
    IMPL.initStatic();
    

    }

  3. Because of API level is below 21, so CardViewJellybeanMr1 is created;
  4. In CardViewJellybeanMr1#initialize it invokes createBackground-->new RoundRectDrawableWithShadow-->setShadowSize, here it says:

    if (shadowSize < 0f) {
        throw new IllegalArgumentException("Invalid shadow size " + shadowSize +
                ". Must be >= 0");
    }
    

The shadowSize is the same as cardElevation

Sign up to request clarification or add additional context in comments.

2 Comments

Great observation. Thank you so much! :) It fixed my issue.
It's my pleasure.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.