4

in my Xml file i have cardView

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="wrap_content"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_height="wrap_content"
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:foreground="?android:attr/selectableItemBackground"
    android:id="@+id/channel_cardview"
    app:cardBackgroundColor="@android:color/white"
    app:cardElevation="2dp"
    app:cardMaxElevation="2dp"
    app:cardUseCompatPadding="true">

   .... some other elements here 
  </RelativeLayout>

when i run my app on SDK 23 it worked fine but when am running this on SDK 19 or SDK 16 then its not working and throwing this error :

E/AndroidRuntime: FATAL EXCEPTION: main
android.view.InflateException: Binary XML file line #10: Error inflating class <unknown>
    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 pb.pocketboard.ChannelFragment$ChannelsAdapter.onCreateViewHolder(ChannelFragment.java:578)
    at pb.pocketboard.ChannelFragment$ChannelsAdapter.onCreateViewHolder(ChannelFragment.java:537)
    at android.support.v7.widget.RecyclerView$Adapter.createViewHolder(RecyclerView.java:5482)
    at android.support.v7.widget.RecyclerView$Recycler.getViewForPosition(RecyclerView.java:4707)

error is pointing to Line no. 10 which is of cardViewCLass, maybe system is not recognising the Cardview Class so i searched for related issues and there the problem was not adding the dependencies but i already added it

my Gradle :

dependencies {

compile fileTree(include: ['*.jar'], dir: 'libs')
testCompile 'junit:junit:4.12'
compile 'com.android.support:appcompat-v7:23.4.0'
compile 'com.android.support:design:23.4.0'
compile 'com.mikhaellopez:circularimageview:2.1.1'
compile 'com.isseiaoki:simplecropview:1.0.16'
compile 'com.android.support:recyclerview-v7:23.4.0'
compile 'com.android.support:cardview-v7:23.4.0'
compile 'com.android.support:support-v4:23.4.0'
compile('com.github.afollestad.material-dialogs:core:0.8.5.3@aar') {
    transitive = true
}
compile files('libs/commons-io-2.4.jar')
}

my java Class where i'm inflating the layout

    @Override
public ChannelsViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {

    View rowView = inflater.inflate(R.layout.custom_channel_row, parent, false); // error is here 
    ChannelsViewHolder holder = new ChannelsViewHolder(rowView , this);

    return holder;
    }

any clue why is this happening ??

remy boys
  • 2,928
  • 5
  • 36
  • 65
  • I think it should be something related to memory usage... http://stackoverflow.com/a/23202156/2442831 and http://stackoverflow.com/questions/20680451/inflateexception-binary-xml-file-line-1-error-inflating-class-unknown-cause – Lino Jul 02 '16 at 07:14
  • @Lino i checked that question before but here am not allocating any memory – remy boys Jul 02 '16 at 07:15
  • can you please attach your build.gradle (Module:app) file in the case when your application is throwing error. – Amit Upadhyay Jul 02 '16 at 07:43
  • the whole gradle file ? @AmitUpadhyay cause i already added the dependency section – remy boys Jul 02 '16 at 08:19

2 Answers2

2

CardView is added in the lollipop version. The same problem will occur when you will try to use RecyclerView ( or any other View that has been added in API 20 (v5.0) or later). Although they are added in support library.

Now once you give a thought that why have they been added to support library?

Its because developers can use them in older versions of android mobiles not because the developer can use them in older versions of SDK. Basically the developer is always going to develop the applications on the updated SDK.

I think this might me the reason :)

Amit Upadhyay
  • 7,179
  • 4
  • 43
  • 57
  • can you make it a little bit clearer ? you mean RecyclerView , CardView is not compatible with SDK < 20 ? if yes then check this out http://stackoverflow.com/questions/29982535/android-supportcardview-recyclerviewlibrary-in-older-versions-with-target-kit – remy boys Jul 02 '16 at 08:12
  • 1
    In your dependencies your have `com.android.support:cardview-v7:23.4.0` if you are changing the compile SDK version from 23 to 19 then try to change the v7 support to `com.android.support:cardview-v7:19.4.0` – Amit Upadhyay Jul 02 '16 at 08:27
  • have you tried changing your dependencies `com.android.support:cardview-v7:23.4.0` to `com.android.support:cardview-v7:19.4.0` while compiling it with SDK 19?? – Amit Upadhyay Jul 02 '16 at 08:39
-1

go to project structure and click on add library dependency and choose com.android.support:cardview-v7:23.2.1 press ok it will solve your problem. Click on below links to undersatnd

https://i.stack.imgur.com/AhYIN.png

https://i.stack.imgur.com/jDCGN.png

Naveen.G
  • 1
  • 2