1

The app launches and runs well on Android 10 and upward. However, on Android 9 and lower, it keeps throwing this runtime exception that has kept me searching for days. Here is the stack trace:

2022-03-14 13:42:15.724 14046-14046/com.iqjoy.jojo... E/AndroidRuntime: FATAL EXCEPTION: main
    Process: com.iqjoy.jojo..., PID: 14046
    java.lang.RuntimeException: Unable to start activity ComponentInfo{com.iqjoy.jojo.../com.iqjoy.jojo....NavActivity}: android.view.InflateException: Binary XML file line #19: Binary XML file line #36: Error inflating class android.view.View
        at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2913)
        at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3048)
        at android.app.servertransaction.LaunchActivityItem.execute(LaunchActivityItem.java:78)
        at android.app.servertransaction.TransactionExecutor.executeCallbacks(TransactionExecutor.java:108)
        at android.app.servertransaction.TransactionExecutor.execute(TransactionExecutor.java:68)
        at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1808)
        at android.os.Handler.dispatchMessage(Handler.java:106)
        at android.os.Looper.loop(Looper.java:193)
        at android.app.ActivityThread.main(ActivityThread.java:6669)
        at java.lang.reflect.Method.invoke(Native Method)
        at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:493)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:858)
     Caused by: android.view.InflateException: Binary XML file line #19: Binary XML file line #36: Error inflating class android.view.View
     Caused by: android.view.InflateException: Binary XML file line #36: Error inflating class android.view.View
     Caused by: java.lang.reflect.InvocationTargetException
        at java.lang.reflect.Constructor.newInstance0(Native Method)
        at java.lang.reflect.Constructor.newInstance(Constructor.java:343)
        at android.view.LayoutInflater.createView(LayoutInflater.java:647)
        at android.view.LayoutInflater.onCreateView(LayoutInflater.java:703)
        at com.android.internal.policy.PhoneLayoutInflater.onCreateView(PhoneLayoutInflater.java:68)
        at android.view.LayoutInflater.onCreateView(LayoutInflater.java:720)
        at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:788)
        at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:730)
        at android.view.LayoutInflater.rInflate(LayoutInflater.java:863)
        at android.view.LayoutInflater.parseInclude(LayoutInflater.java:963)
        at android.view.LayoutInflater.rInflate(LayoutInflater.java:859)
        at android.view.LayoutInflater.rInflateChildren(LayoutInflater.java:824)
        at android.view.LayoutInflater.rInflate(LayoutInflater.java:866)
        at android.view.LayoutInflater.rInflateChildren(LayoutInflater.java:824)
        at android.view.LayoutInflater.inflate(LayoutInflater.java:515)
        at android.view.LayoutInflater.inflate(LayoutInflater.java:423)
        at android.view.LayoutInflater.inflate(LayoutInflater.java:374)
        at androidx.appcompat.app.AppCompatDelegateImpl.setContentView(AppCompatDelegateImpl.java:706)
        at androidx.appcompat.app.AppCompatActivity.setContentView(AppCompatActivity.java:195)
        at com.iqjoy.jojo....NavActivity.onCreate(NavActivity.java:119)
        at android.app.Activity.performCreate(Activity.java:7136)
        at android.app.Activity.performCreate(Activity.java:7127)
        at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1271)
        at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2893)
        at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3048)
        at android.app.servertransaction.LaunchActivityItem.execute(LaunchActivityItem.java:78)
        at android.app.servertransaction.TransactionExecutor.executeCallbacks(TransactionExecutor.java:108)
        at android.app.servertransaction.TransactionExecutor.execute(TransactionExecutor.java:68)
        at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1808)
        at android.os.Handler.dispatchMessage(Handler.java:106)
        at android.os.Looper.loop(Looper.java:193)
        at android.app.ActivityThread.main(ActivityThread.java:6669)
        at java.lang.reflect.Method.invoke(Native Method)
        at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:493)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:858)
2022-03-14 13:42:15.726 14046-14046/com.iqjoy.jojo... E/AndroidRuntime: Caused by: android.content.res.Resources$NotFoundException: Drawable android:color/text_color_primary with resource ID #0x106015f
    Caused by: android.content.res.Resources$NotFoundException: File res/color/text_color_primary.xml from drawable resource ID #0x106015f
        at android.content.res.ResourcesImpl.loadDrawableForCookie(ResourcesImpl.java:847)
        at android.content.res.ResourcesImpl.loadDrawable(ResourcesImpl.java:631)
        at android.content.res.Resources.loadDrawable(Resources.java:897)
        at android.content.res.TypedArray.getDrawableForDensity(TypedArray.java:955)
        at android.content.res.TypedArray.getDrawable(TypedArray.java:930)
        at android.view.View.<init>(View.java:5010)
        at android.view.View.<init>(View.java:4911)
        at android.view.View.<init>(View.java:4890)
        at java.lang.reflect.Constructor.newInstance0(Native Method)
        at java.lang.reflect.Constructor.newInstance(Constructor.java:343)
        at android.view.LayoutInflater.createView(LayoutInflater.java:647)
        at android.view.LayoutInflater.onCreateView(LayoutInflater.java:703)
        at com.android.internal.policy.PhoneLayoutInflater.onCreateView(PhoneLayoutInflater.java:68)
        at android.view.LayoutInflater.onCreateView(LayoutInflater.java:720)
        at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:788)
        at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:730)
        at android.view.LayoutInflater.rInflate(LayoutInflater.java:863)
        at android.view.LayoutInflater.parseInclude(LayoutInflater.java:963)
        at android.view.LayoutInflater.rInflate(LayoutInflater.java:859)
        at android.view.LayoutInflater.rInflateChildren(LayoutInflater.java:824)
        at android.view.LayoutInflater.rInflate(LayoutInflater.java:866)
        at android.view.LayoutInflater.rInflateChildren(LayoutInflater.java:824)
        at android.view.LayoutInflater.inflate(LayoutInflater.java:515)
        at android.view.LayoutInflater.inflate(LayoutInflater.java:423)
        at android.view.LayoutInflater.inflate(LayoutInflater.java:374)
        at androidx.appcompat.app.AppCompatDelegateImpl.setContentView(AppCompatDelegateImpl.java:706)
        at androidx.appcompat.app.AppCompatActivity.setContentView(AppCompatActivity.java:195)
        at com.iqjoy.jojo....NavActivity.onCreate(NavActivity.java:119)
        at android.app.Activity.performCreate(Activity.java:7136)
        at android.app.Activity.performCreate(Activity.java:7127)
        at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1271)
        at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2893)
        at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3048)
        at android.app.servertransaction.LaunchActivityItem.execute(LaunchActivityItem.java:78)
        at android.app.servertransaction.TransactionExecutor.executeCallbacks(TransactionExecutor.java:108)
        at android.app.servertransaction.TransactionExecutor.execute(TransactionExecutor.java:68)
        at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1808)
        at android.os.Handler.dispatchMessage(Handler.java:106)
        at android.os.Looper.loop(Looper.java:193)
        at android.app.ActivityThread.main(ActivityThread.java:6669)
        at java.lang.reflect.Method.invoke(Native Method)
        at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:493)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:858)
     Caused by: org.xmlpull.v1.XmlPullParserException: Binary XML file line #18: <item> tag requires a 'drawable' attribute or child tag defining a drawable
        at android.graphics.drawable.StateListDrawable.inflateChildElements(StateListDrawable.java:190)
        at android.graphics.drawable.StateListDrawable.inflate(StateListDrawable.java:122)
        at android.graphics.drawable.DrawableInflater.inflateFromXmlForDensity(DrawableInflater.java:142)
        at android.graphics.drawable.Drawable.createFromXmlInnerForDensity(Drawable.java:1332)
        at android.graphics.drawable.Drawable.createFromXmlForDensity(Drawable.java:1291)
        at android.content.res.ResourcesImpl.loadDrawableForCookie(ResourcesImpl.java:833)

I believe there must be a resource which Android >=10 ignores or automatically finds hence the exception is not thrown but lower versions shut down.

What I have done

I have created the "res/color/text_color_primary.xml". The error remains. I have rebuilt and cleaned over and over. I have tried to locate the resource file where the problem is being generated, can't find it. I have tried several other ideas including this and this

EDITED

Here's a draft of the affected layout file:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:id = "@+id/parentContainer"
    android:background="@android:color/black"
    >

    <LinearLayout
        style="@style/Widget.Shrine.Backdrop"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:gravity="center_horizontal"
        android:orientation="vertical"
        android:layout_marginTop="80dp"
        android:paddingBottom="100dp"
        >
        <include layout="@layout/backdrop"></include>
    </LinearLayout>

    <com.google.android.material.appbar.AppBarLayout
        android:id="@+id/app_tool"
        android:layout_width="match_parent"
        android:elevation="10dp"
        android:layout_height="wrap_content">

        <androidx.appcompat.widget.Toolbar
            android:id="@+id/app_bar"
            style="@style/Widget.Shrine.Toolbar"
            android:layout_width="match_parent"
            android:layout_height="?attr/actionBarSize"
            app:navigationIcon="@drawable/ic_a_menu"
            app:popupTheme="@style/Theme.PopupOverlay.Menu"
            app:title="@string/app_name" />
    </com.google.android.material.appbar.AppBarLayout>


    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="vertical"
        >
    <fragment
        android:id="@+id/nav_host_fragment"
        android:name="androidx.navigation.fragment.NavHostFragment"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        app:defaultNavHost="true"
        app:navGraph="@navigation/mobile_navigation">
    </fragment>

    <com.google.android.material.bottomnavigation.BottomNavigationView
        android:id="@+id/nav_view"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true"
        android:layout_marginStart="0dp"
        android:layout_marginEnd="0dp"
        android:background="?android:attr/windowBackground"
        app:itemIconTint="@drawable/bottom_navigation_selector"
        app:itemTextColor="@drawable/bottom_navigation_selector"
        app:itemRippleColor="@color/colorPrimary"
        app:menu="@menu/bottom_nav_menu" />
    </LinearLayout>
</RelativeLayout>
Jojo
  • 33
  • 7
  • I know there may be a simple solution to this, but please do not down vote me. I am still learning. – Jojo Mar 14 '22 at 14:06

1 Answers1

1

Ok. So I finally found the problem by isolating each section of the layout file. The problem is actually from the Backdrop section, particularly, the layout that I included:

<include layout="@layout/backdrop"></include>

The backdrop.xml contains a View segment that takes a colour reference for the background:

<?xml version="1.0" encoding="utf-8"?>
<merge xmlns:android="http://schemas.android.com/apk/res/android">

    <com.google.android.material.button.MaterialButton
        style="@style/Widget.Shrine.Button.TextButton"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textColor="#F4D7D5"
        android:id="@+id/fullscreen_button"
        android:text="Livestream" />
    <View
    android:layout_width="56dp"
    android:layout_height="1dp"
    android:layout_margin="16dp"
    android:background="?android:attr/textColorPrimary" />
</merge>

The android:background textColorPrimary is not accessible in Android 9 and lower, check statement by @azizbekian at Why does ?android:textColorPrimary get different values with different API versions?:

This means, that the value specified in the current theme's android:textColorPrimary would be applied. So, if you have declared a TextView in xml and have applied android:textColor="?android:textColorPrimary" to it, then this attribute would be fetched from the theme of the current context with which this layout is being inflated.

As it seems, Android 9 and lower does not reference the attrs.xml. Here's the section of the xml:

   <!-- The most prominent text color.  -->
        <attr name="textColorPrimary" format="reference|color" />
        <!-- Secondary text color. -->
        <attr name="textColorSecondary" format="reference|color" />
        <!-- Tertiary text color. -->
        <attr name="textColorTertiary" format="reference|color" />

To solve the problem, I simply changed the View background to this:

        <View
        android:layout_width="56dp"
        android:layout_height="1dp"
        android:layout_margin="16dp"
        android:background="@color/textColorPrimary" />

Now app is running fine.

Jojo
  • 33
  • 7