[TextView & AppBarLayout (maybe more)] Material3 crash with light theme
See original GitHub issueDescription:
Using a theme with Theme.Material3.DayNight.NoActionBar
as parent, my app crashes when running a light theme. The dark theme works fine. Here are parts of the stack trace regarding the AppBarLayout:
java.lang.RuntimeException: Unable to start activity ComponentInfo{nl.jolanrensen.widgetscreensaver/nl.jolanrensen.widgetscreensaver.SettingsActivity}: android.view.InflateException: Binary XML file line #16 in nl.jolanrensen.widgetscreensaver:layout/activity_settings: Binary XML file line #16 in nl.jolanrensen.widgetscreensaver:layout/activity_settings: Error inflating class com.google.android.material.appbar.AppBarLayout
...
Caused by: android.view.InflateException: Binary XML file line #16 in nl.jolanrensen.widgetscreensaver:layout/activity_settings: Binary XML file line #16 in nl.jolanrensen.widgetscreensaver:layout/activity_settings: Error inflating class com.google.android.material.appbar.AppBarLayout
Caused by: android.view.InflateException: Binary XML file line #16 in nl.jolanrensen.widgetscreensaver:layout/activity_settings: Error inflating class com.google.android.material.appbar.AppBarLayout
Caused by: java.lang.reflect.InvocationTargetException
...
E/AndroidRuntime: at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1003)
Caused by: java.lang.UnsupportedOperationException: Failed to resolve attribute at index 13: TypedValue{t=0x2/d=0x7f0300f1 a=10}
at android.content.res.TypedArray.getDrawableForDensity(TypedArray.java:998)
at android.content.res.TypedArray.getDrawable(TypedArray.java:982)
at android.view.View.<init>(View.java:5520)
at android.view.ViewGroup.<init>(ViewGroup.java:702)
at android.widget.LinearLayout.<init>(LinearLayout.java:254)
at android.widget.LinearLayout.<init>(LinearLayout.java:250)
at com.google.android.material.appbar.AppBarLayout.<init>(AppBarLayout.java:215)
at com.google.android.material.appbar.AppBarLayout.<init>(AppBarLayout.java:211)
If I take out the AppBarLayout from the project, the app simply crashes on the next TextView:
java.lang.RuntimeException: Unable to start activity ComponentInfo{nl.jolanrensen.widgetscreensaver/nl.jolanrensen.widgetscreensaver.SettingsActivity}: android.view.InflateException: Binary XML file line #65 in nl.jolanrensen.widgetscreensaver:layout/activity_settings: Binary XML file line #38 in nl.jolanrensen.widgetscreensaver:layout/content_settings: Error inflating class TextView
...
Caused by: android.view.InflateException: Binary XML file line #65 in nl.jolanrensen.widgetscreensaver:layout/activity_settings: Binary XML file line #38 in nl.jolanrensen.widgetscreensaver:layout/content_settings: Error inflating class TextView
Caused by: android.view.InflateException: Binary XML file line #38 in nl.jolanrensen.widgetscreensaver:layout/content_settings: Error inflating class TextView
Caused by: java.lang.UnsupportedOperationException: Failed to resolve attribute at index 6: TypedValue{t=0x2/d=0x101009b a=1}
at android.content.res.TypedArray.getColorStateList(TypedArray.java:598)
at android.widget.TextView.readTextAppearance(TextView.java:4062)
at android.widget.TextView.<init>(TextView.java:1088)
at android.widget.TextView.<init>(TextView.java:1026)
at androidx.appcompat.widget.AppCompatTextView.<init>(AppCompatTextView.java:108)
at com.google.android.material.textview.MaterialTextView.<init>(MaterialTextView.java:93)
at com.google.android.material.textview.MaterialTextView.<init>(MaterialTextView.java:88)
at com.google.android.material.textview.MaterialTextView.<init>(MaterialTextView.java:83)
at com.google.android.material.theme.MaterialComponentsViewInflater.createTextView(MaterialComponentsViewInflater.java:61)
at androidx.appcompat.app.AppCompatViewInflater.createView(AppCompatViewInflater.java:121)
at androidx.appcompat.app.AppCompatDelegateImpl.createView(AppCompatDelegateImpl.java:1570)
at androidx.appcompat.app.AppCompatDelegateImpl.onCreateView(AppCompatDelegateImpl.java:1621)
at android.view.LayoutInflater.tryCreateView(LayoutInflater.java:1065)
at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:1001)
at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:965)
at android.view.LayoutInflater.rInflate(LayoutInflater.java:1127)
at android.view.LayoutInflater.rInflateChildren(LayoutInflater.java:1088)
at android.view.LayoutInflater.rInflate(LayoutInflater.java:1130)
at android.view.LayoutInflater.rInflateChildren(LayoutInflater.java:1088)
at android.view.LayoutInflater.parseInclude(LayoutInflater.java:1267)
at android.view.LayoutInflater.rInflate(LayoutInflater.java:1123)
at android.view.LayoutInflater.rInflateChildren(LayoutInflater.java:1088)
at android.view.LayoutInflater.rInflate(LayoutInflater.java:1130)
at android.view.LayoutInflater.rInflateChildren(LayoutInflater.java:1088)
at android.view.LayoutInflater.rInflate(LayoutInflater.java:1130)
at android.view.LayoutInflater.rInflateChildren(LayoutInflater.java:1088)
at android.view.LayoutInflater.inflate(LayoutInflater.java:686)
It looks like there is an attribute somewhere, that only exists in the light theme, which both TextView and AppBarLayout use, that can’t be read or found somehow.
Expected behavior: There shouldn’t be a crash.
Source code: My app does use ViewBinding if that could be the cause of the issue. Other than that I use a simple AppBarLayout:
<com.google.android.material.appbar.AppBarLayout
android:id="@+id/appbar"
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent">
<com.google.android.material.appbar.MaterialToolbar
android:id="@+id/toolbar"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
android:transitionName="toolbar">
<include
android:id="@+id/activity_settings_toolbar_content"
layout="@layout/activity_settings_toolbar_content" />
</com.google.android.material.appbar.MaterialToolbar>
</com.google.android.material.appbar.AppBarLayout>
and TextView:
<TextView
android:id="@+id/orientation_text"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:text="@string/orientation"
android:textAppearance="@style/TextAppearance.AppCompat.Body1"
android:textColor="?attr/colorOnSurface"
android:textSize="14sp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toStartOf="@id/guideline5"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
My main theme is based on MaterialThemeBuilder, with the parent theme being replaced with Theme.Material3.DayNight.NoActionBar
Android API version: 31
Material Library version: 1.5.0-alpha03
Device: Pixel 4XL: Android 12 beta 5
Issue Analytics
- State:
- Created 2 years ago
- Comments:15 (7 by maintainers)
Top GitHub Comments
Glad to hear you found the root cause!!
Material 3 should work without too much extra efforts if you are already using Material themes. We deprecate certain color roles, like colorPrimaryDark, so if you are customizing it somewhere in your app, you may want to double check if the new look and feel still serves your need.
We don’t have an official migration guide yet. But we will probably have it when Material 3 is officially released. (The est. timeline is around Oct.)
Material library is more or less based on AppCompat, so in theory there won’t be compatibility issues between them. Let me know if this answers your concern. : )
I think I figured it out… In the values-v26 etc I didn’t inherit from Base.Theme.MyApp, but from another Material Components theme. Since those values influence the new ones, I essentially had two different parent themes, a Material Components and a Material3 theme. Thanks for your help! One last question: How’s the backwards compatibility for Material3 btw? I noticed for another app of mine that on android 7 colors and rendering was all over the place using a Material3 theme. Should I wait for AppCompat to support Material3 or is there already a guide on how to handle this?