【问题标题】:I am getting runtime error #25: You must supply a layout_width attribute我收到运行时错误 #25:您必须提供 layout_width 属性
【发布时间】:2017-03-07 17:55:43
【问题描述】:

我收到运行时错误,Android 应用程序正在停止

android.view.InflateException: Binary XML file line #9: Binary XML file line #9: Error inflating class android.support.design.widget.NavigationView

原因:android.view.InflateException:二进制 XML 文件第 9 行:错误膨胀类 android.support.design.widget.NavigationView

原因:android.view.InflateException:二进制 XML 文件第 25 行:二进制 XML 文件第 25 行:您必须提供 layout_width 属性。 原因:java.lang.UnsupportedOperationException:二进制 XML 文件第 25 行:您必须提供 layout_width 属性。

这是我的 xml 代码

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
xmlns:app="http://schemas.android.com/apk/res-auto"
tools:context="com.example.xxx.xxx.Main_Tab"
>

<android.support.design.widget.NavigationView
    android:id="@+id/nav_view"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:layout_gravity="start"
    android:fitsSystemWindows="true"
    app:headerLayout="@layout/nav_header_main"
    app:menu="@menu/activity_main_drawer" >
    <include
        layout="@layout/app_bar"
        android:layout_width="match_parent"
        android:layout_height="match_parent" />
</android.support.design.widget.NavigationView>

<android.support.design.widget.TabLayout
    android:id="@+id/tab_layout1"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_below="@+id/nav_view"
    android:background="#00BCD4"

    android:minHeight="?attr/actionBarSize"
    android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar"/>

<android.support.v4.view.ViewPager
    android:id="@+id/pager1"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_below="@id/tab_layout1"/>


 </RelativeLayout>

这里是 app_bar.xml

<?xml version="1.0" encoding="utf-8"?>
<android.support.design.widget.CoordinatorLayout      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:layout_width="match_parent"
android:layout_height="match_parent"
android:fitsSystemWindows="true"
tools:context="com.example.xxx.xxx.Main_Tab">

<android.support.design.widget.AppBarLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:theme="@style/AppTheme.AppBarOverlay">

    <android.support.v7.widget.Toolbar
        android:id="@+id/toolbar"
        android:layout_width="match_parent"
        android:layout_height="?attr/actionBarSize"
        android:background="#8BC34A"
        app:popupTheme="@style/AppTheme.PopupOverlay" />

</android.support.design.widget.AppBarLayout>

<FrameLayout
    android:id="@+id/frame"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    app:layout_behavior="@string/appbar_scrolling_view_behavior">    </FrameLayout>
 </android.support.design.widget.CoordinatorLayout>

这里是代码activity_main_drawer

<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android"

>
<group android:checkableBehavior="single">
    <item
        android:id="@+id/nav_home"
        android:icon="@drawable/ic_home_black_24dp"
        android:title="@string/nav_home" />

    <item
        android:id="@+id/nav_notifications"
        android:icon="@drawable/ic_notifications_black_24dp"
        android:title="@string/nav_notifications" />
  </group>

  <item android:title="Other">
    <menu>
        <item
            android:id="@+id/nav_about_us"
            android:title="@string/nav_about_us" />
        <item
            android:id="@+id/nav_privacy_policy"
            android:title="@string/privacy_policy" />
    </menu>
</item>

</menu>

【问题讨论】:

  • 指定该行的宽度。例如 150dp
  • 我猜问题出在您的@layout/app_bar 中。可以发一下吗?
  • 您在 app_bar.xml 中错过了 layout_width。检查一下。
  • app_bar 我已经发布了结帐 layout_width 有没有
  • 检查我更新的答案。您还包括@menu/activity_main_drawer@layout/nav_header_main

标签: android performance android-layout android-studio android-xml


【解决方案1】:

您的布局似乎很好。但是您在此布局中包含了更多布局。

app:headerLayout="@layout/nav_header_main"
app:menu="@menu/activity_main_drawer"

<include
    layout="@layout/app_bar"
    android:layout_width="match_parent"
    android:layout_height="match_parent" />

您很可能在@layout/app_bar@layout/nav_header_main@menu/activity_main_drawer 的任一布局中都缺少android:layout_width 属性。

【讨论】:

  • 我已经发布了@menu/activity_main_drawer 结帐
  • nav_header_main 有 android:layout_width
最近更新 更多