升级到com.google.android.material:material:1.1.0-alphaXX

时间:2019-05-23 14:55:38

标签: android material-design android-theme build-dependencies gradle-dependencies

问题摘要

在我的单一活动应用程序中,我使用了一个自定义的NavigationDrawerFragment,其中<​​strong> androidx.drawerlayout.widget.DrawerLayout 包含 com.google.android.material.navigation.NavigationView 侧面菜单可在片段之间导航。

最近,我从v7支持库迁移到 AndroidX ,今天尝试从 com.google.android.material:material:1.0.0 升级到 com .google.android.material:material:1.1.0-alpha06 ,但是我的应用在开始制作时崩溃:

  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

调查

降级为材料:1.1.0-alpha05 -alpha04 -alpha03 -alpha02 ,和 -alpha01 会在加载到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

降级为 material:1.0.0 不会对我的物理测试设备 Android 5.1.1 Samsung Galaxy J3

产生任何错误

发现类似案件

StackOverflow 上最相关的问题没有可接受的答案:

  1. Failed to resolve attribute at index 13 at android.content.res.TypedArray.getDrawable()
  2. ANDROID : Failed to resolve attribute at index 13

其他会产生另一个错误消息(由...引起),例如NullPointerException,不同的属性索引 number 或提及 v7 android支持库我不使用的,即(精简列表):

  1. Android XML: RuntimeException: Failed to resolve attribute at index 6
  2. CoordinatorLayout - Failed to resolve attribute at index 1 at android.content.res.TypedArray.getDrawable

他们都没有提到新的材料库。

常见问题-所有theze错误都与android Theme固有属性(例如?attr或预定义的颜色以及style.xml中的值)关联

我的代码

NavigationDrawerFragment.java

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;
  }
}

navigation_drawer_fragment.xml

<?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>

styles.xml

<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>

build.gradle(Module:app)

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'

需要帮助

我希望我的应用程序在更新后能够成功构建,但是主题出现了问题。

我想知道是否有一种方法可以解决此问题,而不必深入研究预定义的主题属性和手动编辑样式值。

感谢任何帮助或建议-)

1 个答案:

答案 0 :(得分:0)

我发现一种可能的方法是编辑我的styles.xml文件,用一些Theme.AppCompat...条目替换每个Theme.MaterialComponents...条目,尽管它不是很优雅(并且破坏了某些应用程序的样式) :

  • <style name="AppTheme" parent="Theme.AppCompat.Light.NoActionBar">
    替换为<style name="AppTheme" parent="Theme.MaterialComponents.Light.NoActionBar">

  • <style name="AppTheme.CardViewStyle" parent="Widget.AppCompat.CardView">
    替换为<style name="AppTheme.CardViewStyle" parent="Widget.MaterialComponents.CardView">

  • <style name="AppTheme.NavigationDrawerStyle" parent="ThemeOverlay.AppCompat.Light">
    替换为<style name="AppTheme.NavigationDrawerStyle" parent="ThemeOverlay.MaterialComponents.Light">

我也替换了其他一些样式条目,尽管其中一些没有确切的对应样式:

  • <style name="RoundedButtonStyle" parent="Widget.AppCompat.Button.Colored">
    替换为<style name="RoundedButtonStyle" parent="Widget.MaterialComponents.Button.OutlinedButton">

  • <style name="AboutApplicationDialog" parent="@style/Theme.AppCompat.Light.Dialog">
    替换为<style name="AboutApplicationDialog" parent="@style/Theme.MaterialComponents.Light.Dialog">

  • <style name="AppTheme.EditTextStyle" parent="Widget.AppCompat.EditText">
    替换为<style name="AppTheme.EditTextStyle" parent="Widget.MaterialComponents.TextInputLayout.OutlinedBox">

如果您找到其他方法,请回答)