【问题标题】:Android.Views.InflateException: Binary XML file line #17: Error inflating class android.support.design.internal.NavigationMenuItemViewAndroid.Views.InflateException: Binary XML file line #17: Error inflating class android.support.design.internal.NavigationMenuItemView
【发布时间】:2020-06-12 21:50:17
【问题描述】:

我正在使用 Xamarin 的 android 项目中使用材质导航抽屉。 我一直在尝试将两个 Framelayouts 添加到抽屉布局中,以便我可以从不同的片段切换内容,请参见下面的 xml:

 <?xml version="1.0" encoding="utf-8"?>
    <android.support.v4.widget.DrawerLayout xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:app="http://schemas.android.com/apk/res-auto"
        android:id="@+id/drawer_layout"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:fitsSystemWindows="true">
      <!-- This LinearLayout represents the contents of the screen  -->
      <LinearLayout
          android:layout_width="match_parent"
          android:layout_height="match_parent"
          android:orientation="vertical">
        <!-- The ActionBar displayed at the top -->
        <include
            layout="@layout/toolbar"
android:id="@+id/toolbar" />
        <!-- The main content view where fragments are loaded -->
        <ScrollView
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:fillViewport="true"
            android:id="@+id/scrollview">
          <LinearLayout
              android:layout_width="match_parent"
              android:layout_height="match_parent"
              android:orientation="vertical">
            <FrameLayout
                android:id="@+id/search_frame"
                android:layout_width="match_parent"
                android:layout_height="wrap_content" />
            <FrameLayout
                android:id="@+id/blah"
                android:layout_width="match_parent"
                android:layout_height="match_parent" />
          </LinearLayout>
        </ScrollView>
      </LinearLayout>
      <!-- The navigation drawer that comes from the left -->
      <!-- Note that `android:layout_gravity` needs to be set to 'start' -->
      <android.support.design.widget.NavigationView
          android:id="@+id/nav_view"
          android:layout_width="wrap_content"
          android:layout_height="match_parent"
          android:layout_gravity="start"
          app:menu="@menu/drawer_view"
          app:itemTextColor="#FFFFFF"
          app:itemIconTint="#FFFFFF"
          android:background="#464646"/>
    </android.support.v4.widget.DrawerLayout>

主要活动:

protected override void OnCreate(Android_OS.Bundle bundle)
{
    base.OnCreate(bundle);

    SetContentView(Resource.Layout.activity_main);

    drawerLayout = FindViewById<DrawerLayout>(Resource.Id.drawer_layout);
    // Setting up the toolbar
    var toolbar = FindViewById<Toolbar>(Resource.Id.toolbar);
    SetSupportActionBar(toolbar);
    SupportActionBar.SetTitle(Resource.String.ApplicationName);
    SupportActionBar.SetDisplayHomeAsUpEnabled(true);
    SupportActionBar.SetDisplayShowHomeEnabled(true);

    // Attach item selected handler to navigation view
    var navigationView = FindViewById<NavigationView>(Resource.Id.nav_view);
    navigationView.NavigationItemSelected += NavigationView_NavigationItemSelected;

    // Create ActionBarDrawerToggle button and add it to the toolbar
    var drawerToggle = new Android_Support.V7.App.ActionBarDrawerToggle(this, drawerLayout, toolbar, Resource.String.open_drawer, Resource.String.close_drawer);
    drawerLayout.SetDrawerListener(drawerToggle);
    drawerToggle.SyncState();
}

我收到以下错误:Android.Views.InflateException: Binary XML file line #17: Error inflating class android.support.design.internal.NavigationMenuItemView

我已尝试移动 Drawerlayout 中的元素,但错误仍然存​​在。我查看了 Android 文档,一切似乎都是正确的。我还检查了我是否拥有最新版本的 Design 和 AppCompact 库

【问题讨论】:

  • 它可能会帮助您github.com/chrisbanes/cheesesquare,下载此项目并运行,按照相同的方法并根据您的要求更改代码。希望这对您有所帮助。
  • 尝试清理并重建,看看是否能解决问题。在进行需要重新生成资源的更改后,我偶尔会收到类似的错误。重建后,它对我来说就消失了。
  • 清理和重建似乎不能解决问题。
  • 干净和重建对我有用。
  • Clean + Build 也对我有用。

标签: android xamarin


【解决方案1】:

前段时间我也遇到过同样的问题,我不知道到底是什么原因造成的,但我可以告诉你我做了什么,也许它可以帮助你。

  1. 我的手机内存快满了,所以我删除了一些应用程序
  2. 我卸载了出现错误的应用程序
  3. 我清理并重建了项目
  4. 再次运行。

瞧!它奏效了。

【讨论】:

  • 你确定这个异常是因为内存不足而引发的吗?卸载几个应用程序就可以解决?
  • 它对我有用,我从来没有说过这是正确的答案
  • 这对我也有用,但我的猜测只是第 3 步解决了问题。无论如何,谢谢!
  • 是的!我花了一天时间在谷歌上搜索,我想知道,它也对我有用。
  • 对我来说也是#3
【解决方案2】:

我可能迟到了,但我遇到了同样的错误。所以在 AndroidStudio 上,我刚刚清理了我的项目(“Clean Projet”)。之后,我能够再次运行我的项目而不会出现该错误。

【讨论】:

    【解决方案3】:

    错误是因为您可能将MainActivity 扩展为Activity 而不是AppComptActivity

    如果您使用任何设计库组件,则应确保您的活动扩展 AppCompatActivity 并使用适当的 Theme.AppCompat 主题。请注意,FloatingActionButton 依赖于您的主题上设置的 colorAccent - 确保您的主题已定义。

    【讨论】:

      【解决方案4】:

      我尝试了我的解决方案,它确实运行成功: 1. 清空C盘温度(磁盘设置操作系统) 2.重建项目 3.插入头部活动 [活动(主题=“@style/MyTheme”)] 公共类 viewDetailsActivity : BaseActivity {.....} 4. 运行项目 注意:插入文件style.xml这段代码

           <style name="MyTheme" parent="MyTheme.Base">
          <item name="android:textColor">@color/black</item>
        </style>
        <!-- Base theme applied no matter what API -->
        <style name="MyTheme.Base" parent="Theme.AppCompat.Light.DarkActionBar">
          <item name="windowNoTitle">true</item>
          <!--We will be using the toolbar so no need to show ActionBar-->
          <item name="windowActionBar">false</item>
          <!-- Set theme colors from -->
          <!-- colorPrimary is used for the default action bar background -->
          <item name="colorPrimary">@color/primary</item>
          <!-- colorPrimaryDark is used for the status bar -->
          <item name="colorPrimaryDark">@color/primaryDark</item>
          <!-- colorAccent is used as the default value for colorControlActivated
               which is used to tint widgets -->
          <item name="colorAccent">@color/accent</item>
          <!-- You can also set colorControlNormal, colorControlActivated-
               colorControlHighlight and colorSwitchThumbNormal. -->
      
          <!-- Option to hide the drop shadow here
      
            <item name="actionBarStyle">@style/MyTheme.ActionBar</item>
            <item name="android:windowContentOverlay">@null</item>
          -->
        </style>
        <style name="MyTheme.NoActionBar">
          <!-- Both of these are needed -->
          <item name="windowActionBar">false</item>
          <item name="windowNoTitle">true</item>
        </style>
        <style name="MyTheme.ActionBar" parent="style/Widget.AppCompat.Light.ActionBar.Solid">
          <!-- remove shadow below action bar -->
          <!-- <item name="android:elevation">0dp</item> -->
          <!-- Support library compatibility -->
          <item name="elevation">0dp</item>
          <item name="android:background">@color/pictonBlue</item>
        </style>
      

      我觉得我说英语不好,但问题解决成功

      【讨论】:

        【解决方案5】:

        关于异常的简要说明:如果没有向DrawerLayout 添加内容布局,也会发生异常,因此请务必添加一个。

        一个例子:

        <?xml version="1.0" encoding="utf-8"?>
        <android.support.v4.widget.DrawerLayout 
            xmlns:android="http://schemas.android.com/apk/res/android"
            xmlns:app="http://schemas.android.com/apk/res-auto"
            android:id="@+id/activity_main_drawer_layout"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:fitsSystemWindows="true">
        
            <!-- Start of content -->
            <LinearLayout
             android:layout_width="match_parent"
             android:layout_height="match_parent"
             android:orientation="vertical">
            <include
                layout="@layout/toolbar"
                android:id="@+id/toolbar" />
            </LinearLayout>
            <!-- End of content -->
        
        <android.support.design.widget.NavigationView
            android:layout_width="wrap_content"
            android:layout_height="match_parent"
            android:layout_gravity="start"
            android:id="@+id/activity_main_navigation_view"
            app:menu="@menu/menu_activity_main_navigation" />
        </android.support.v4.widget.DrawerLayout>
        

        【讨论】:

          【解决方案6】:
          1. 删除项目 bin 文件夹中的所有内容。
          2. 删除项目 obj 文件夹中的所有内容。
          3. 运行构建->清理解决方案
          4. 然后再次尝试构建并运行您的解决方案。

          仅仅清洗溶液对我来说是不够的;我必须手动删除 binobj 文件夹中的所有内容才能重新启动并运行。

          【讨论】:

            【解决方案7】:

            我刚遇到这个问题。我需要做的就是右键单击我的 Android 解决方案 → 属性 → Android 选项 → 将链接设置为无。

            【讨论】:

              【解决方案8】:

              我通过右键单击 android 项目 -> 属性 -> Android 选项并在代码收缩器中不选择任何内容来修复它。

              【讨论】:

                猜你喜欢
                • 1970-01-01
                • 1970-01-01
                • 1970-01-01
                • 1970-01-01
                • 1970-01-01
                • 1970-01-01
                • 2016-11-03
                • 2020-06-27
                • 1970-01-01
                相关资源
                最近更新 更多