0

MainActivity.java

public class MainActivity extends AppCompatActivity {

     Toolbar mToolbar;
     DrawerLayout mDrawerLayout;
     NavigationView mNavigationView;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.navigation_drawer);

        mToolbar = findViewById(R.id.toolbar);
        setSupportActionBar(mToolbar);

        mDrawerLayout = findViewById(R.id.drawer_layout);
        mNavigationView = findViewById(R.id.navigation_view);

        ActionBarDrawerToggle toggle = new ActionBarDrawerToggle(this, mDrawerLayout, mToolbar, R.string.open_drawer, R.string.close_drawer);
        mDrawerLayout.setDrawerListener(toggle);

        toggle.syncState();

    }
}

Activity_main.xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:id="@+id/activity_main"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <android.support.v7.widget.Toolbar
        android:layout_width="match_parent"
        android:layout_height="?attr/actionBarSize"
        android:id="@+id/toolbar"
        android:background="?attr/colorPrimary"
        app:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar"/>

</RelativeLayout>

navigation_drawer.xml

<?xml version="1.0" encoding="utf-8"?>
<android.support.v4.widget.DrawerLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    xmlns:tools="http://schemas.android.com/tools"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:id="@+id/drawer_layout"
    android:fitsSystemWindows="true"
    tools:openDrawer="start">

    <include layout="@layout/activity_main"/>

    <android.support.design.widget.NavigationView
        android:layout_width="wrap_content"
        android:layout_height="match_parent"
        android:id="@+id/navigation_view"
        android:layout_gravity="start"
        android:fitsSystemWindows="true"
        app:headerLayout="@layout/navigation_header"
        app:menu="@menu/navigation_menu">

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

</android.support.v4.widget.DrawerLayout>

navigation_header.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="match_parent"
    android:layout_height="180dp"
    android:background="@drawable/header"
    android:padding="16dp"
    android:theme="@style/ThemeOverlay.AppCompat.Dark"
    android:gravity="bottom">

    <android.support.v7.widget.AppCompatImageView
        android:layout_width="56dp"
        android:layout_height="56dp"
        android:src="@drawable/goku_image"
        android:scaleType="centerCrop"
        android:contentDescription="@string/profile_image"/>

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Goku"
        android:paddingTop="8dp"
        android:textAppearance="@style/TextAppearance.AppCompat.Body1"/>

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="gokusaiyangod@cc.com"
        android:paddingTop="4dp"/>

</LinearLayout>

navigation_menu.xml

<?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/inbox_id"
            android:title="@string/inbox"
            android:icon="@drawable/inbox"/>

        <item
            android:id="@+id/starred_id"
            android:title="@string/starred"
            android:icon="@drawable/starred"/>

        <item
            android:id="@+id/sent_id"
            android:title="@string/sent_mail"
            android:icon="@drawable/sent"/>

        <item

            android:id="@+id/drafts_id"
            android:title="@string/drafts"
            android:icon="@drawable/drafts"/>

    </group>

    <item android:title="@string/more_labels">

        <menu>

            <item
                android:id="@+id/allMail_id"
                android:title="@string/all_mail"
                android:icon="@drawable/allmail"/>

            <item
                android:id="@+id/trash_id"
                android:title="@string/trash"
                android:icon="@drawable/trash"/>

            <item
                android:id="@+id/spam_id"
                android:title="@string/spam"
                android:icon="@drawable/spam"/>

        </menu>

    </item>

</menu>

when i supply navigation_menu and navigation_header to navigation_drawer it doesn't work on physical device but without these two supplied it works very fine. and also as the heading says it works fine with "menu" and "header" supplied to navigation_drawer when i run it on emulator but it doesn't work on physical device.

Aditya Tandon
  • 650
  • 1
  • 5
  • 8
  • how did you created a navigation bar. do you right click and choose activity -> Navigation drawer activity if true it will work on both devices – Ashokkumar Adichill Apr 10 '18 at 10:37
  • What exactly do you mean by "doesn't work"? Does the drawer not slide as it should? Does it just not show? Does the app crash? Does your device explode? Please be specific. – Mike M. Apr 10 '18 at 17:24
  • well my device doesn't explodes. as soon as the app installs on my physical device it crashes and shows the error on the screen "unfortunately, Navigation Drawer has stopped working". – Aditya Tandon Apr 11 '18 at 10:07
  • i created navigation drawer from the scratch. i selected empty activity and then coded all the stuff – Aditya Tandon Apr 11 '18 at 10:10
  • as soon as i remove this code from my navigation_drawer.xml app:menu="@menu/menu" app:headerLayout="@layout/headerlayout" and then run my app it work fine without header and menu – Aditya Tandon Apr 11 '18 at 10:13
  • Look at [the stack trace](http://stackoverflow.com/questions/23353173) to determine the cause of the crash. You can [edit] your question to post it here, if you need assistance interpreting it. – Mike M. Apr 11 '18 at 15:36
  • ok thanks i'll look at stack trace – Aditya Tandon Apr 14 '18 at 10:42

0 Answers0