【问题标题】:InvocationTargetException and "Failed to resolve attribute at index 13" on upgrade to com.google.android.material:material:1.1.0-alphaXX升级到 com.google.android.material:material:1.1.0-alphaXX 时出现 InvocationTargetException 和“无法解析索引 13 处的属性”
【发布时间】:2019-10-10 04:33:19
【问题描述】:

问题总结

在我的单活动应用程序中,我使用带有 androidx.drawerlayout.widget.DrawerLayout 的自定义 NavigationDrawerFragment,其中包含 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

调查

降级到ma​​terial: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

降级到 ma​​terial: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

其他产生另一个错误消息(Caused by...) 比如 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

他们都没有提到新的 ma​​terial 库。

什么是常见的 - 所有这些错误都与 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(模块: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'

需要帮助

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

不知道有没有办法解决这个问题。

【问题讨论】:

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


    【解决方案1】:

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

    • &lt;style name="AppTheme" parent="Theme.AppCompat.Light.NoActionBar"&gt;
      替换为&lt;style name="AppTheme" parent="Theme.MaterialComponents.Light.NoActionBar"&gt;

    • &lt;style name="AppTheme.CardViewStyle" parent="Widget.AppCompat.CardView"&gt;
      替换为&lt;style name="AppTheme.CardViewStyle" parent="Widget.MaterialComponents.CardView"&gt;

    • &lt;style name="AppTheme.NavigationDrawerStyle" parent="ThemeOverlay.AppCompat.Light"&gt;
      替换为&lt;style name="AppTheme.NavigationDrawerStyle" parent="ThemeOverlay.MaterialComponents.Light"&gt;

    我还替换了一些其他样式条目,尽管其中一些没有完全对应的样式:

    • &lt;style name="RoundedButtonStyle" parent="Widget.AppCompat.Button.Colored"&gt;
      替换为&lt;style name="RoundedButtonStyle" parent="Widget.MaterialComponents.Button.OutlinedButton"&gt;

    • &lt;style name="AboutApplicationDialog" parent="@style/Theme.AppCompat.Light.Dialog"&gt;
      替换为&lt;style name="AboutApplicationDialog" parent="@style/Theme.MaterialComponents.Light.Dialog"&gt;

    • &lt;style name="AppTheme.EditTextStyle" parent="Widget.AppCompat.EditText"&gt;
      替换为&lt;style name="AppTheme.EditTextStyle" parent="Widget.MaterialComponents.TextInputLayout.OutlinedBox"&gt;

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-06-27
      • 2015-08-16
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多