0

I am developing one app where i write application class. Because of that my navigation drawer header menu is not working. It shows Nullpointer exception on Layout and textview

public class MyApplication extends Application {

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

}

This is my application class which i used becasuse i used 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"/>

But because of that application class from my xml following could could not work it give 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
Box Box Box Box
  • 5,094
  • 10
  • 49
  • 67
jr.abhi
  • 25
  • 7
  • How can assert that the navigationview was disabled or not visible because of Application class? – GvSharma Feb 16 '16 at 05:10
  • this is visible but when i initlize the textview and linearlayout that time i am getting nullpointer exception – jr.abhi Feb 16 '16 at 05:12

2 Answers2

0

Your navigation view should come inside coordinator layout tag.

Ragesh Ramesh
  • 3,470
  • 2
  • 14
  • 20
0

Your layout should be like this

<?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>

and the layout app_bar_app_home will be like this

<?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>

replace content_app_home layout with your actual layout.

GvSharma
  • 2,632
  • 1
  • 24
  • 30