https://kotlinlang.org logo
#android
Title
# android
j

Jaime Guerrero

01/08/2021, 12:44 PM
Hello everyone!! I’m testing a fragment and I have a problem. I initialize it doing this:
Copy code
@Test
fun testFragment() {
    launchFragmentInContainer<CountryFragment>(themeResId = R.style.AppTheme)
    ...
}
My style is:
Copy code
<style name="AppTheme" parent="Theme.MaterialComponents.DayNight.DarkActionBar">
...
</style>
onCreateOptionsMenu(menu: Menu, inflater: MenuInflater)
in the fragment is not called and the actionbar is hide. If I use
R.style.ThemeOverlay_MaterialComponents_Dark_ActionBar
for instance ,
onCreateOptionsMenu
is called and the actionbar is shown but I get this error:
Copy code
android.view.InflateException: Binary XML file line #12 in com.jaimegc.covid19tracker:layout/item_total: Binary XML file line #12 in com.jaimegc.covid19tracker:layout/item_total: Error inflating class com.google.android.material.card.MaterialCardView
Caused by: android.view.InflateException: Binary XML file line #12 in com.jaimegc.covid19tracker:layout/item_total: Error inflating class com.google.android.material.card.MaterialCardView
Caused by: java.lang.reflect.InvocationTargetException
...
Caused by: java.lang.IllegalArgumentException: The style on this component requires your app theme to be Theme.MaterialComponents (or a descendant).
And using
R.style.Theme_MaterialComponents
I have the same problem as in the first case. What would be the way to initialize the fragment with its actionbar? I could create a toolbar but I would like to avoid it if there are alternatives. Thanks!
stackoverflow 2
2 Views