[DynamicColors] App crash when opening context menu with onLongClickListener
See original GitHub issueDescription: After upgrading to M3 and enabling dynamic colors, the app crashes when opening a context menu on Android 12. This behavior happens only when I explicitly set an onLongClickListener to trigger the context menu. The app doesn’t crash when dynamic colors are disabled or not available (older Android version).
Here are the error log messages I get with the stack trace:
E/ThemeUtils: View class androidx.appcompat.widget.AppCompatImageView is an AppCompat widget that can only be used with a Theme.AppCompat theme (or descendant).
E/ThemeUtils: View class com.google.android.material.textview.MaterialTextView is an AppCompat widget that can only be used with a Theme.AppCompat theme (or descendant).
E/ThemeUtils: View class com.google.android.material.button.MaterialButton is an AppCompat widget that can only be used with a Theme.AppCompat theme (or descendant).
D/AndroidRuntime: Shutting down VM
E/AndroidRuntime: FATAL EXCEPTION: main
Process: org.example.android12_context_menu_issue, PID: 11851
android.view.InflateException: Binary XML file line #80 in android:layout/alert_dialog_material: Binary XML file line #37 in android:layout/alert_dialog_button_bar_material: Error inflating class Button
Caused by: android.view.InflateException: Binary XML file line #37 in android:layout/alert_dialog_button_bar_material: Error inflating class Button
Caused by: java.lang.IllegalArgumentException: The style on this component requires your app theme to be Theme.MaterialComponents (or a descendant).
at com.google.android.material.internal.ThemeEnforcement.checkTheme(ThemeEnforcement.java:241)
at com.google.android.material.internal.ThemeEnforcement.checkMaterialTheme(ThemeEnforcement.java:215)
at com.google.android.material.internal.ThemeEnforcement.checkCompatibleTheme(ThemeEnforcement.java:143)
at com.google.android.material.internal.ThemeEnforcement.obtainStyledAttributes(ThemeEnforcement.java:75)
at com.google.android.material.button.MaterialButton.<init>(MaterialButton.java:226)
at com.google.android.material.button.MaterialButton.<init>(MaterialButton.java:217)
at com.google.android.material.theme.MaterialComponentsViewInflater.createButton(MaterialComponentsViewInflater.java:43)
at androidx.appcompat.app.AppCompatViewInflater.createView(AppCompatViewInflater.java:129)
at androidx.appcompat.app.AppCompatDelegateImpl.createView(AppCompatDelegateImpl.java:1566)
at androidx.appcompat.app.AppCompatDelegateImpl.onCreateView(AppCompatDelegateImpl.java:1617)
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.inflate(LayoutInflater.java:686)
at android.view.LayoutInflater.inflate(LayoutInflater.java:538)
at android.view.LayoutInflater.inflate(LayoutInflater.java:485)
at com.android.internal.policy.PhoneWindow.setContentView(PhoneWindow.java:461)
at com.android.internal.app.AlertController.installContent(AlertController.java:269)
at android.app.AlertDialog.onCreate(AlertDialog.java:439)
at android.app.Dialog.dispatchOnCreate(Dialog.java:424)
at android.app.Dialog.show(Dialog.java:318)
at com.android.internal.view.menu.MenuDialogHelper.show(MenuDialogHelper.java:89)
at com.android.internal.view.menu.ContextMenuBuilder.showDialog(ContextMenuBuilder.java:90)
at com.android.internal.policy.DecorView.showContextMenuForChildInternal(DecorView.java:859)
at com.android.internal.policy.DecorView.showContextMenuForChild(DecorView.java:829)
at android.view.ViewGroup.showContextMenuForChild(ViewGroup.java:978)
at android.view.ViewGroup.showContextMenuForChild(ViewGroup.java:978)
at android.view.ViewGroup.showContextMenuForChild(ViewGroup.java:978)
at android.view.ViewGroup.showContextMenuForChild(ViewGroup.java:978)
at android.view.ViewGroup.showContextMenuForChild(ViewGroup.java:978)
at android.view.View.showContextMenu(View.java:7596)
at android.widget.TextView.showContextMenu(TextView.java:11259)
at org.example.android12_context_menu_issue.MainActivity.onCreate$lambda-0(MainActivity.kt:18)
at org.example.android12_context_menu_issue.MainActivity.$r8$lambda$KmXOWaTXF_Np4mpPc2lqYGLll8U(Unknown Source:0)
at org.example.android12_context_menu_issue.MainActivity$$ExternalSyntheticLambda2.onLongClick(Unknown Source:0)
E/AndroidRuntime: at android.view.View.performLongClickInternal(View.java:7521)
at android.view.View.performLongClick(View.java:7479)
at android.widget.TextView.performLongClick(TextView.java:12633)
at android.view.View.performLongClick(View.java:7497)
at android.view.View$CheckForLongPress.run(View.java:28635)
at android.os.Handler.handleCallback(Handler.java:938)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loopOnce(Looper.java:201)
at android.os.Looper.loop(Looper.java:288)
at android.app.ActivityThread.main(ActivityThread.java:7839)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:548)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1003)
Expected behavior: The context menu is displayed and the app doesn’t crash.
Source code:
App.kt
class App : Application() {
override fun onCreate() {
super.onCreate()
DynamicColors.applyToActivitiesIfAvailable(this)
}
}
MainActivity.kt
class MainActivity : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
val textView: TextView = findViewById(R.id.text_view)
textView.setOnLongClickListener { v ->
v.showContextMenu()
}
textView.setOnCreateContextMenuListener { menu, _, _ ->
menu.add(Menu.NONE, R.id.action_show_snackbar, Menu.NONE, "Show snackbar")
.setOnMenuItemClickListener {
Snackbar.make(textView, "Hello World!", Snackbar.LENGTH_LONG).show()
true
}
}
}
}
activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">
<TextView
android:id="@+id/text_view"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Hello World!"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent"/>
</androidx.constraintlayout.widget.ConstraintLayout>
themes.xml (built with https://material-foundation.github.io/material-theme-builder/)
<resources>
<style name="AppTheme" parent="Theme.Material3.Light.NoActionBar">
<item name="colorPrimary">@color/md_theme_light_primary</item>
<item name="colorOnPrimary">@color/md_theme_light_onPrimary</item>
<item name="colorPrimaryContainer">@color/md_theme_light_primaryContainer</item>
<item name="colorOnPrimaryContainer">@color/md_theme_light_onPrimaryContainer</item>
<item name="colorSecondary">@color/md_theme_light_secondary</item>
<item name="colorOnSecondary">@color/md_theme_light_onSecondary</item>
<item name="colorSecondaryContainer">@color/md_theme_light_secondaryContainer</item>
<item name="colorOnSecondaryContainer">@color/md_theme_light_onSecondaryContainer</item>
<item name="colorTertiary">@color/md_theme_light_tertiary</item>
<item name="colorOnTertiary">@color/md_theme_light_onTertiary</item>
<item name="colorTertiaryContainer">@color/md_theme_light_tertiaryContainer</item>
<item name="colorOnTertiaryContainer">@color/md_theme_light_onTertiaryContainer</item>
<item name="colorError">@color/md_theme_light_error</item>
<item name="colorErrorContainer">@color/md_theme_light_errorContainer</item>
<item name="colorOnError">@color/md_theme_light_onError</item>
<item name="colorOnErrorContainer">@color/md_theme_light_onErrorContainer</item>
<item name="android:colorBackground">@color/md_theme_light_background</item>
<item name="colorOnBackground">@color/md_theme_light_onBackground</item>
<item name="colorSurface">@color/md_theme_light_surface</item>
<item name="colorOnSurface">@color/md_theme_light_onSurface</item>
<item name="colorSurfaceVariant">@color/md_theme_light_surfaceVariant</item>
<item name="colorOnSurfaceVariant">@color/md_theme_light_onSurfaceVariant</item>
<item name="colorOutline">@color/md_theme_light_outline</item>
<item name="colorOnSurfaceInverse">@color/md_theme_light_inverseOnSurface</item>
<item name="colorSurfaceInverse">@color/md_theme_light_inverseSurface</item>
<item name="colorPrimaryInverse">@color/md_theme_light_primaryInverse</item>
</style>
</resources>
Android API version: 31
Material Library version: 1.5.0, 1.6.0-alpha02 Device: Pixel 4a (physical)
Issue Analytics
- State:
- Created 2 years ago
- Comments:6 (3 by maintainers)
Top Results From Across the Web
App crashes on context menu selected - Stack Overflow
The exception says it loud and clear: The result of item.getMenuInfo() cannot be casted to ExpandableListView.
Read more >Opening context menu crashes any app. Con…
Question: Q: Opening context menu crashes any app. Contextual Menu Items folder missing. Hi all,. Specs: 13" Macbook Pro Retina (Late 2013).
Read more >ListView ContextActions cause app to crash on Android #6647
On Android, When opening context menu of a ListView with a long press gesture, the app crashes. This behavior is observed with screenreader ......
Read more >Menus - Android Developers
On Android 3.0 and higher, the options menu is considered to always be open when menu items are presented in the app bar....
Read more >How to identify the cause of a crash in the context menu
When I right-click on a file in an open-dialog it makes the underlying application crash "once-in-a-while". This happens in many ...
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
@InternationalKoder I’m wondering if this crash may be fixed by https://github.com/material-components/material-components-android/commit/b84f35f1211028e7c456ce1ca91685a938f0ffcc and https://github.com/material-components/material-components-android/commit/f6c8fa5aefc84a1543eca3056a538120de0b4297 which are not released yet.
Would you be able to try with our latest snapshot?
https://github.com/material-components/material-components-android/blob/master/docs/using-snapshot-version.md
It works great, the issue is fixed on version 1.6.0-SNAPSHOT. Thank you!