InvocationTargetException 和 "Failed to resolve attribute at index 13" 升级到 com.google.android.material:material:1.1.0-alphaXX

InvocationTargetException and "Failed to resolve attribute at index 13" on upgrade to com.google.android.material:material:1.1.0-alphaXX

问题总结

在我的单一 activity 应用程序中,我使用自定义 NavigationDrawerFragment 与 androidx.drawerlayout.widget.DrawerLayout 包含 com.google.android.material.navigation.NavigationView 作为在片段之间导航的侧边菜单。

最近我从 v7 支持库迁移到 AndroidX,今天尝试从 com.google.android.material 更新:material:1.0.0com.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

调查

降级到 material: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 三星 Galaxy J3

查找相似案例

Whosebug 上最相关的问题没有被接受的答案:

  1. 无法在 android.content.res.TypedArray.getDrawable() 处解析索引 13 处的属性
  2. ANDROID : Failed to resolve attribute at index 13

其他产生另一个错误信息(Caused by...)比如NullPointerException,不同的属性索引number,或者提到v7 android 支持我不使用的库,即(简化列表):

其中 none 提到了新的 material 库。

常见的是 - 所有 theze 错误都与 android Theme 内在属性相关联,例如 ?attr 或预定义的颜色,以及 style.xml[=25= 中的值]

我的代码

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(模块:应用程序)

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'

需要帮助

我预计我的应用程序在更新后会成功构建,但主题出现问题。

我想知道是否有办法解决这个问题。

我发现了一种构建应用程序的可能性,方法是编辑我的 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">

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