In my single-activity application I use a custom NavigationDrawerFragment with androidx.drawerlayout.widget.DrawerLayout containing com.google.android.material.navigation.NavigationView as a side menu to navigate between fragments.
Recently I migrated from v7 support library to AndroidX and today tried to update from com.google.android.material:material:1.0.0 to com.google.android.material:material:1.1.0-alpha06 but my app crashes on start producing:
android.view.InflateException: Binary XML file line #37:
Error inflating class com.google.android.material.navigation.NavigationView
Caused by: java.lang.reflect.InvocationTargetException
Caused by: java.lang.RuntimeException: Failed to resolve attribute at index 13
Downgrading to material:1.1.0-alpha05, -alpha04, -alpha03, -alpha02, and -alpha01 gives another but similar error messages inside a default fragment, loaded in DrawerLayout:
android.view.InflateException: Binary XML file line #2:
Error inflating class androidx.cardview.widget.CardView
Caused by: java.lang.reflect.InvocationTargetException
Caused by: java.lang.RuntimeException: Failed to resolve attribute at index 2
Downgrading to material:1.0.0 gives no error on my physical testing device Android 5.1.1 Samsung Galaxy J3
The most relevant questions on StackOverflow have no accepted answers:
Other produce another error messages (Caused by...) like NullPointerException, different attribute index number, or mention v7 android support library which I do not use, i.e. (reduced list):
And none of them mention new material library.
What is common - all theze errors are linked with android Theme
intrinsic attributes, like ?attr
or predefined colors, and values in style.xml
public class NavigationDrawerFragment extends Fragment
{
private DrawerLayout drawerLayout;
private Toolbar toolbar;
private NavigationView navigationView;
private View toolbarView;
@Nullable @Override
public final View
onCreateView
(@Nullable final LayoutInflater inflater,
@Nullable final ViewGroup container,
@Nullable final Bundle savedInstanceState)
{
View result = null;
getActivity().setTheme
(R.style.AppTheme_NavigationDrawerStyle);
if((inflater != null) && (container != null))
{
final View view = inflater.inflate
(R.layout.navigation_drawer_fragment, container, false);
if(view != null)
{
toolbar = view.findViewById(R.id.toolbar);
drawerLayout = view.findViewById(R.id.drawerLayout);
navigationView = view.findViewById(R.id.navigationView);
if(toolbar != null)
{
toolbarView =
getLayoutInflater().inflate
(R.layout.toolbar_view_layout, toolbar);
if(toolbarView != null)
{
final String title = getString(R.string.app_name);
toolbar.setTitle(title);
((AppCompatActivity) getActivity())
.setSupportActionBar(toolbar);
((AppCompatActivity) getActivity())
.getSupportActionBar().setTitle(title);
}
}
}
result = view;
}
return result;
}
}
<?xml version="1.0" encoding="utf-8"?>
<androidx.drawerlayout.widget.DrawerLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/drawerLayout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fitsSystemWindows="true"
tools:openDrawer="start"
tools:context=".view.activity.MainActivity">
<androidx.appcompat.widget.LinearLayoutCompat
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<include
layout="@layout/toolbar"
android:layout_width="match_parent"
android:layout_height="wrap_content"/>
<!-- Layout contents of main body (drawer will slide over this) -->
<FrameLayout
android:id="@+id/contentFrame"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:isScrollContainer="true">
</FrameLayout>
</androidx.appcompat.widget.LinearLayoutCompat>
<!-- Container for contents of drawer (header and items) -->
<com.google.android.material.navigation.NavigationView
android:id="@+id/navigationView"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_gravity="start"
android:fitsSystemWindows="true"
android:theme="@style/AppTheme.NavigationDrawerStyle"
app:headerLayout="@layout/menu_layout_navigation_drawer_header"
app:menu="@menu/menu_navigation_view_items"/>
</androidx.drawerlayout.widget.DrawerLayout>
<resources>
<!-- Base application theme-->
<style name="AppTheme" parent="Theme.AppCompat.Light.NoActionBar">
<item name="colorPrimary">@color/colorPrimary</item>
<item name="colorPrimaryDark">@color/colorPrimaryDark</item>
<item name="colorAccent">@color/colorAccent</item>
<item name="colorControlActivated">@color/colorAccent</item>
<item name="colorControlHighlight">@color/colorAccent</item>
<item name="android:textColorHighlight">@color/colorAccent</item>
<item name="cardViewStyle">@style/AppTheme.CardViewStyle</item>
<item name="editTextStyle">@style/AppTheme.EditTextStyle</item>
<item name="navigationViewStyle">
@style/Widget.AppCompat.NavigationView
</item>
</style>
<!-- / Base application theme-->
<!-- CardView -->
<style name="AppTheme.CardViewStyle" parent="Widget.AppCompat.CardView">
<item name="cardPreventCornerOverlap">true</item>
<item name="cardUseCompatPadding">true</item>
</style>
<!-- / CardView -->
<!-- Navigation Drawer -->
<style name="AppTheme.NavigationDrawerStyle" parent="ThemeOverlay.AppCompat.Light">
<item name="android:textSize">@dimen/navigation_drawer_menu_fontsize</item>
<item name="android:listPreferredItemHeightSmall">
@dimen/navigation_drawer_menu_item_height_medium
</item>
<item name="listPreferredItemHeightSmall">
@dimen/navigation_drawer_menu_item_height_medium
</item>
<item name="listPreferredItemPaddingLeft">
@dimen/navigation_drawer_menu_item_padding_left
</item>
<item name="fontFamily">@string/roboto_medium</item>
</style>
<!-- / Navigation Drawer -->
</resources>
apply plugin: 'com.android.application'
android
{
compileSdkVersion 28
defaultConfig
{
applicationId = "net.cargo"
minSdkVersion 14
targetSdkVersion 28
versionCode = 3
versionName = "1.0.2"
testInstrumentationRunner = "androidx.test.runner.AndroidJUnitRunner"
vectorDrawables {
useSupportLibrary = true
}
multiDexEnabled = true
}
compileOptions {
sourceCompatibility JavaVersion.VERSION_1_8
targetCompatibility JavaVersion.VERSION_1_8
}
flavorDimensions "product", "mock"
productFlavors {
}
buildToolsVersion '29.0.0 rc2'
// different resource directories
sourceSets {
main {
res.srcDirs = ['src/main/res']
}
}
buildTypes {
debug {
ext.enableCrashlytics = true
shrinkResources false
minifyEnabled false
zipAlignEnabled true
}
}
}
repositories {
mavenCentral()
}
configurations {
cleanedAnnotations
}
dependencies
{
implementation fileTree(include: ['*.jar'], dir: 'libs')
// support material design
implementation 'com.google.android.material:material:1.1.0-alpha06'
// androidx support library
implementation 'androidx.legacy:legacy-support-v4:1.0.0'
implementation 'androidx.appcompat:appcompat:1.1.0-alpha05'
implementation 'androidx.recyclerview:recyclerview:1.1.0-alpha05'
implementation 'androidx.cardview:cardview:1.0.0'
implementation 'androidx.vectordrawable:vectordrawable:1.1.0-beta01'
implementation 'androidx.constraintlayout:constraintlayout:2.0.0-beta1'
implementation 'androidx.multidex:multidex:2.0.1'
// google architecture components
implementation 'androidx.lifecycle:lifecycle-extensions:2.2.0-alpha01'
implementation 'androidx.lifecycle:lifecycle-common-java8:2.2.0-alpha01'
implementation 'androidx.lifecycle:lifecycle-reactivestreams:2.2.0-alpha01'
implementation 'androidx.room:room-runtime:2.1.0-beta01'
implementation 'androidx.room:room-rxjava2:2.1.0-beta01'
// annotation processors
annotationProcessor 'androidx.lifecycle:lifecycle-compiler:2.2.0-alpha01'
annotationProcessor 'androidx.room:room-compiler:2.1.0-beta01'
I expected my app build successfully after update, but something broke in theme.
I wonder if there is a way to fix this issue.
I found one possibility to build the app by editing my styles.xml
file, replacing every Theme.AppCompat...
entry with some Theme.MaterialComponents...
entry, although it's not very elegant (and breaks some app styling):
<style name="AppTheme" parent="Theme.AppCompat.Light.NoActionBar">
replaced with<style name="AppTheme" parent="Theme.MaterialComponents.Light.NoActionBar">
<style name="AppTheme.CardViewStyle" parent="Widget.AppCompat.CardView">
replaced with <style name="AppTheme.CardViewStyle" parent="Widget.MaterialComponents.CardView">
<style name="AppTheme.NavigationDrawerStyle" parent="ThemeOverlay.AppCompat.Light">
replaced with <style name="AppTheme.NavigationDrawerStyle" parent="ThemeOverlay.MaterialComponents.Light">
I have also replaced some other style entries, though some of them do not have an exact corresponding style:
<style name="RoundedButtonStyle" parent="Widget.AppCompat.Button.Colored">
replaced with <style name="RoundedButtonStyle" parent="Widget.MaterialComponents.Button.OutlinedButton">
<style name="AboutApplicationDialog" parent="@style/Theme.AppCompat.Light.Dialog">
replaced with <style name="AboutApplicationDialog" parent="@style/Theme.MaterialComponents.Light.Dialog">
<style name="AppTheme.EditTextStyle" parent="Widget.AppCompat.EditText">
replaced with <style name="AppTheme.EditTextStyle" parent="Widget.MaterialComponents.TextInputLayout.OutlinedBox">
If you find some other way, please answer )