【问题标题】:Navigation drawer header menu and othet menu导航抽屉标题菜单和其他菜单
【发布时间】:2016-05-27 05:41:25
【问题描述】:

我正在开发一个我编写应用程序类的应用程序。因此,我的 navigation drawer header menu 无法正常工作。它在Layouttextview 上显示Nullpointer 异常

public class MyApplication extends Application {

@Override
protected void attachBaseContext(Context base) {
    super.attachBaseContext(base);
    MultiDex.install(base);
}

}

这是我使用的应用程序类,因为我使用了multidexenabled true。

<android.support.design.widget.CoordinatorLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/main_content"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <android.support.design.widget.AppBarLayout
        android:id="@+id/appbar"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:theme="@style/Base.Theme.DesignDemo">

        <android.support.v7.widget.Toolbar
            android:id="@+id/toolbar1"
            android:layout_width="match_parent"
            android:layout_height="?attr/actionBarSize"
            android:background="?attr/colorPrimary" />
    </android.support.design.widget.AppBarLayout>

    <FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_marginTop="?attr/actionBarSize"
        android:id="@+id/fragment_container">
    </FrameLayout>


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

<android.support.design.widget.NavigationView
    android:id="@+id/nav_view"
    android:layout_height="match_parent"
    android:layout_width="match_parent"
    android:layout_gravity="start"
    android:fitsSystemWindows="true"
    app:headerLayout="@layout/nav_header"
    app:menu="@menu/drawer_view"
    android:background="#FFFFFF"/>

但是由于我的 xml 中的那个应用程序类无法工作它给null pointer exception in

  <android.support.design.widget.NavigationView
    android:id="@+id/nav_view"
    android:layout_height="match_parent"
    android:layout_width="match_parent"
    android:layout_gravity="start"
    android:fitsSystemWindows="true"
    app:headerLayout="@layout/nav_header"
    app:menu="@menu/drawer_view"
    android:background="#FFFFFF"/>

   app:headerLayout="@layout/nav_header" is not working i am using component from it but in java class i get null pointer exception

【问题讨论】:

  • 如何断言导航视图由于 Application 类而被禁用或不可见?
  • 这是可见的,但是当我初始化文本视图和线性布局时,我得到空指针异常

标签: android


【解决方案1】:

您的导航视图应位于协调器布局标签内。

【讨论】:

    【解决方案2】:

    你的布局应该是这样的

    <?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"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/drawer_layout"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:fitsSystemWindows="true"
    tools:openDrawer="start">
    
    <include
        layout="@layout/app_bar_app_home"
        android:layout_width="match_parent"
        android:layout_height="match_parent" />
    
    <android.support.design.widget.NavigationView
          android:id="@+id/nav_view"
          android:layout_height="match_parent"
          android:layout_width="match_parent"
          android:layout_gravity="start"
          android:fitsSystemWindows="true"
          app:headerLayout="@layout/nav_header"
          app:menu="@menu/drawer_view"
          android:background="#FFFFFF"/>
    </android.support.design.widget.NavigationView>
    

    app_bar_app_home 的布局会是这样的

    <?xml version="1.0" encoding="utf-8"?>
    

    <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="?attr/colorPrimary"
            app:popupTheme="@style/Theme.DesignDemo">
    
            <include layout="@layout/toolbar_custom_view" />
        </android.support.v7.widget.Toolbar>
    
    </android.support.design.widget.AppBarLayout>
    
    <include layout="@layout/content_app_home" />
    
    
    <TextView
        android:id="@+id/tvNoList"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:gravity="center"
        android:text="No Items"
        android:textColor="@color/black"
        android:textSize="@dimen/text_size_18sp"
        android:visibility="gone" />
    </android.support.design.widget.CoordinatorLayout>
    

    将 content_app_home 布局替换为您的实际布局。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-11-08
      • 2015-09-26
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多