0

Before marking the question as duplicate, I already checked the other answers and nothing worked. The application works on Android >=5.0, but fails on Android 4.4.2 and probably on other Android 4.x devices.

This is the line where the crash is happening:

 @Override
    public RecyclerViewHolder onCreateViewHolder(ViewGroup parent, int viewType)
    {
       View itemView = LayoutInflater.from(context).inflate(itemLayout, parent, false);
       ..........................

build.gradle

android {
    compileSdkVersion 23
    buildToolsVersion ANDROID_BUILD_TOOLS

defaultConfig {
    minSdkVersion 16
    targetSdkVersion ANDROID_TARGET_SDK
.......................
}


dependencies {
 compile 'com.android.support:appcompat-v7:23.1.0'
 compile 'com.android.support:design:23.1.0'
 compile 'com.android.support:recyclerview-v7:23.1.0'
 compile 'com.github.bumptech.glide:glide:3.5.2'
 .......................
}

Stacktrace

android.view.InflateException: Binary XML file line #1: Error inflating class <unknown>
                                                                        at android.view.LayoutInflater.createView(LayoutInflater.java:620)
                                                                        at com.android.internal.policy.impl.PhoneLayoutInflater.onCreateView(PhoneLayoutInflater.java:56)
                                                                        at android.view.LayoutInflater.onCreateView(LayoutInflater.java:669)
                                                                        at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:694)
                                                                        at android.view.LayoutInflater.inflate(LayoutInflater.java:469)
                                                                        at android.view.LayoutInflater.inflate(LayoutInflater.java:397)
                                                                        at com.dyve.counting.FileExplorer.RecyclerAdapter.onCreateViewHolder(RecyclerAdapter.java:54)
                                                                        at com.dyve.counting.FileExplorer.RecyclerAdapter.onCreateViewHolder(RecyclerAdapter.java:18)
                                                                        at android.support.v7.widget.RecyclerView$Adapter.createViewHolder(RecyclerView.java:5223)
                                                                        at android.support.v7.widget.RecyclerView$Recycler.getViewForPosition(RecyclerView.java:4449)
                                                                        at android.support.v7.widget.RecyclerView$Recycler.getViewForPosition(RecyclerView.java:4359)
                                                                        at android.support.v7.widget.LinearLayoutManager$LayoutState.next(LinearLayoutManager.java:1961)
                                                                        at android.support.v7.widget.GridLayoutManager.layoutChunk(GridLayoutManager.java:438)
                                                                        at android.support.v7.widget.LinearLayoutManager.fill(LinearLayoutManager.java:1333)
                                                                        at android.support.v7.widget.LinearLayoutManager.onLayoutChildren(LinearLayoutManager.java:562)
                                                                        at android.support.v7.widget.GridLayoutManager.onLayoutChildren(GridLayoutManager.java:171)
                                                                        at android.support.v7.widget.RecyclerView.dispatchLayout(RecyclerView.java:2900)
                                                                        at android.support.v7.widget.RecyclerView.onLayout(RecyclerView.java:3071)
                                                                        at android.view.View.layout(View.java:14858)
                                                                        at android.view.ViewGroup.layout(ViewGroup.java:4646)
                                                                        at android.support.design.widget.CoordinatorLayout.layoutChild(CoordinatorLayout.java:1037)
                                                                        at android.support.design.widget.CoordinatorLayout.onLayoutChild(CoordinatorLayout.java:747)
                                                                        at android.support.design.widget.ViewOffsetBehavior.onLayoutChild(ViewOffsetBehavior.java:42)
                                                                        at android.support.design.widget.AppBarLayout$ScrollingViewBehavior.onLayoutChild(AppBarLayout.java:1133)
                                                                        at android.support.design.widget.CoordinatorLayout.onLayout(CoordinatorLayout.java:760)
                                                                        at android.view.View.layout(View.java:14858)
                                                                        at android.view.ViewGroup.layout(ViewGroup.java:4646)
                                                                        at android.support.v4.widget.DrawerLayout.onLayout(DrawerLayout.java:1043)
                                                                        at android.view.View.layout(View.java:14858)
                                                                        at android.view.ViewGroup.layout(ViewGroup.java:4646)
                                                                        at android.widget.FrameLayout.layoutChildren(FrameLayout.java:453)
                                                                        at android.widget.FrameLayout.onLayout(FrameLayout.java:388)
                                                                        at android.view.View.layout(View.java:14858)
                                                                        at android.view.ViewGroup.layout(ViewGroup.java:4646)
                                                                        at android.widget.LinearLayout.setChildFrame(LinearLayout.java:1673)
                                                                        at android.widget.LinearLayout.layoutVertical(LinearLayout.java:1527)
                                                                        at android.widget.LinearLayout.onLayout(LinearLayout.java:1436)
                                                                        at android.view.View.layout(View.java:14858)
                                                                        at android.view.ViewGroup.layout(ViewGroup.java:4646)
                                                                        at android.widget.FrameLayout.layoutChildren(FrameLayout.java:453)
                                                                        at android.widget.FrameLayout.onLayout(FrameLayout.java:388)
                                                                        at android.view.View.layout(View.java:14858)
                                                                        at android.view.ViewGroup.layout(ViewGroup.java:4646)
                                                                        at android.widget.LinearLayout.setChildFrame(LinearLayout.java:1673)
                                                                        at android.widget.LinearLayout.layoutVertical(LinearLayout.java:1527)
                                                                        at android.widget.LinearLayout.onLayout(LinearLayout.java:1436)
                                                                        at android.view.View.layout(View.java:14858)
                                                                        at android.view.ViewGroup.layout(ViewGroup.java:4646)
                                                                        at android.widget.FrameLayout.layoutChildren(FrameLayout.java:453)
                                                                        at android.widget.FrameLayout.onLayout(FrameLayout.java:388)
                                                                        at android.view.View.layout(View.java:14858)
                                                                        at android.view.ViewGroup.layout(ViewGroup.java:4646)
                                                                        at android.view.ViewRootImpl.performLayout(ViewRootImpl.java:2035)
                                                                        at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:1792)
                                                                        at android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:1047)

the XML file:

<?xml version="1.0" encoding="utf-8"?>
<android.support.v4.widget.DrawerLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="@+id/drawer_layout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fitsSystemWindows="true" >

<android.support.design.widget.CoordinatorLayout
    android:id="@+id/coordinator_layout"
    android:layout_width="match_parent"
    android:layout_height="match_parent" >

    <android.support.design.widget.AppBarLayout
        android:id="@+id/app_bar_layout"
        android:layout_width="match_parent"
        android:layout_height="@dimen/app_bar_height"
        android:theme="@style/ThemeOverlay.AppCompat.ActionBar" >

        <android.support.design.widget.CollapsingToolbarLayout
            android:id="@+id/collapsing_toolbar_layout"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:background="@color/primary_color"
            app:expandedTitleMarginStart="@dimen/title_margin"
            app:layout_scrollFlags="scroll|exitUntilCollapsed" >

            <android.support.v7.widget.Toolbar
                android:id="@+id/toolbar"
                android:layout_width="match_parent"
                android:layout_height="?actionBarSize"
                app:contentInsetStart="@dimen/title_margin"
                app:layout_collapseMode="pin"
                android:background="@color/primary_color"/>

        </android.support.design.widget.CollapsingToolbarLayout>

    </android.support.design.widget.AppBarLayout>

    <android.support.v7.widget.RecyclerView
        android:id="@+id/recycler_view"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:clipToPadding="false"
        android:paddingBottom="48dp"
        app:layout_behavior="@string/appbar_scrolling_view_behavior" />

</android.support.design.widget.CoordinatorLayout>

The first line of the xml file is: <android.support.v4.widget.DrawerLayout

I can't seem to figure out why this is happening and it's driving me nuts. I would greatly appreciate any help.

  • What is itemLayout? – Code-Apprentice Sep 30 '16 at 08:35
  • @Code-Apprentice It's an Integer. I edited the question with the whole function. –  Sep 30 '16 at 08:38
  • Why is it an Integer instead of an int? Where do you set is value? – Code-Apprentice Sep 30 '16 at 08:40
  • In initRecyclerView() i set his value. recyclerAdapter.setItemLayout(R.layout.list_item_0); recyclerAdapter.setSpanCount(getResources().getInteger(R.integer.span_count0)); –  Sep 30 '16 at 08:41
  • @Code-Apprentice Here you can find the entire code if you want to take a look. Thank you. Link: https://github.com/calintat/Explorer –  Sep 30 '16 at 08:46
  • Please edit your question to show the relevant code. – Code-Apprentice Sep 30 '16 at 08:52
  • can you post your xml? – AbhayBohra Sep 30 '16 at 08:52
  • I edited and added the xml also. Thanks –  Sep 30 '16 at 08:54
  • I had this issue before, sorry but i don't remember how i got it solved, buy give `clean & build` a try – Mohammad Ersan Sep 30 '16 at 09:16
  • It crashes only on Android 4.x. On 5.x and 6.x it works like a charm and I can't seem to figure out why, it's so damn frustrating. Thanks anyway @MoshErsan. –  Sep 30 '16 at 09:18
  • @ZackBogdan can you post the whole stacktrace? all stack trace – Mohammad Ersan Sep 30 '16 at 09:58
  • @MoshErsan but that's the whole stacktrace –  Sep 30 '16 at 10:01
  • if you check this question here http://stackoverflow.com/questions/20680451/inflateexception-binary-xml-file-line-1-error-inflating-class-unknown-cause , you can notice the stacktrace is extended more than yours – Mohammad Ersan Sep 30 '16 at 10:17
  • Yeah, unfortunately that's my whole stack. You can see in other thread, that his stack contains some bitmap decoding involved, but that's not the case here. I tried to change the configuration of the project, the compileSdkVersion and minSdkVersion, but nothing works, i can't get it to work on Android 4.X... –  Sep 30 '16 at 10:37

1 Answers1

1

After spending 2 silly days figuring out what was the problem, i figured out the cause. : using attributes as reference to color in xml drawable. On API<21 you can't use attrs to color in xml drawable. So only way is to use reference to color resourse (@color/YOURS_COLOR) or use #RGB format.So if you want to use xml drawable with theme depended colors you should create a drawable for each theme;

Solution: In case someone encounters his exception, you should catch it and have a look at the cause of it in the variables window because that's where the error message is more explicit.

I changed the background for a Relative Layout from @drawable/selectable_background.xml to a different color and everything works fine now. enter image description here