Search code examples
javaandroidmenudrawerlayout

Android DrawLayout Implementation


ive been following this post

Android Navigation Drawer implemented with Activities

However the application crashes before it displays any content. Also it doesnt appear to get the the OnCreate menthod as its not triggering any breakpoints

here is my MainActivity

package com.example.alex.menutest2;

import android.content.Intent;
import android.support.v4.widget.DrawerLayout;
import android.support.v7.app.ActionBarActivity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ListView;



public class MainActivity extends ActionBarActivity {

    public ListView mLeftDrawerList = (ListView)findViewById(R.id.left_drawer);
    public DrawerLayout mDrawerLayout = (DrawerLayout) findViewById(R.id.drawer_layout);


    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        // do other stuff to initialize drawer layout, add list items
        // ………
        // ……….
        // add a listener to the drawer list view
        mLeftDrawerList.setOnItemClickListener(new DrawerItemClickListener());

    }


    private class DrawerItemClickListener implements ListView.OnItemClickListener {
        @Override
        public void onItemClick(AdapterView<?> adapterView, View view, int position, long id) {
            switch (position) {
                case 0: {
                    Intent intent = new Intent(MainActivity.this, DocumentActivity.class);
                    startActivity(intent);
                    break;
                }
                default:
                    break;
            }
            mDrawerLayout.closeDrawer(mLeftDrawerList);
        }
    }
}

here is my xml layout

<android.support.v4.widget.DrawerLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_height="match_parent"
    android:layout_width="match_parent"
    android:id="@+id/drawer_layout">

    <FrameLayout
        android:id="@+id/activity_frame"/>

   <ListView
    android:id="@+id/left_drawer"
       android:entries="@array/menu_array"/>
</android.support.v4.widget.DrawerLayout>

here is my stack trace

06-14 19:14:26.127    8093-8093/com.example.alex.menutest2 E/AndroidRuntime﹕ FATAL EXCEPTION: main
    Process: com.example.alex.menutest2, PID: 8093
    java.lang.RuntimeException: Unable to instantiate activity ComponentInfo{com.example.alex.menutest2/com.example.alex.menutest2.MainActivity}: java.lang.NullPointerException: Attempt to invoke virtual method 'android.view.View android.view.Window.findViewById(int)' on a null object reference
            at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2236)
            at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2390)
            at android.app.ActivityThread.access$800(ActivityThread.java:151)
            at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1303)
            at android.os.Handler.dispatchMessage(Handler.java:102)
            at android.os.Looper.loop(Looper.java:135)
            at android.app.ActivityThread.main(ActivityThread.java:5257)
            at java.lang.reflect.Method.invoke(Native Method)
            at java.lang.reflect.Method.invoke(Method.java:372)
            at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:903)
            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:698)
     Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'android.view.View android.view.Window.findViewById(int)' on a null object reference
            at android.app.Activity.findViewById(Activity.java:2072)
            at com.example.alex.menutest2.MainActivity.<init>(MainActivity.java:17)
            at java.lang.reflect.Constructor.newInstance(Native Method)
            at java.lang.Class.newInstance(Class.java:1606)
            at android.app.Instrumentation.newActivity(Instrumentation.java:1066)
            at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2226)
            at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2390)
            at android.app.ActivityThread.access$800(ActivityThread.java:151)
            at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1303)
            at android.os.Handler.dispatchMessage(Handler.java:102)
            at android.os.Looper.loop(Looper.java:135)
            at android.app.ActivityThread.main(ActivityThread.java:5257)
            at java.lang.reflect.Method.invoke(Native Method)
            at java.lang.reflect.Method.invoke(Method.java:372)
            at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:903)
            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:698)

Solution

  • I might be wrong, but I think that you shouldn't initialize your global parameters before the onCreate() method is called. You're trying to initialize them with findViewById() before the contentView was even set.

    Based on your error log, you've got a NullPointer at mDrawerLayout, which makes me guess that the following line didn't find a View:
    public DrawerLayout mDrawerLayout = (DrawerLayout) findViewById(R.id.drawer_layout);