Android DrawLayout 实现

Android DrawLayout Implementation

我一直在关注这个 post

Android Navigation Drawer implemented with Activities

但是,应用程序在显示任何内容之前就崩溃了。而且它似乎没有得到 OnCreate 方法,因为它没有触发任何断点

这是我的 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);
        }
    }
}

这是我的 xml 布局

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

这是我的堆栈跟踪

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.access0(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.access0(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)

我可能是错的,但我认为您不应该在调用 onCreate() 方法之前初始化您的全局参数。您试图在设置 contentView 之前用 findViewById() 初始化它们。

根据您的错误日志,您在 mDrawerLayout 处有一个 NullPointer,这让我猜测以下行没有找到视图:
public DrawerLayout mDrawerLayout = (DrawerLayout) findViewById(R.id.drawer_layout);

您应该在 onCreate 中找到您的视图,之后setContentView(R.layout.activity_main); 像这样:

public ListView mLeftDrawerList ;
public DrawerLayout mDrawerLayout ;
 protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

   mLeftDrawerList = (ListView)findViewById(R.id.left_drawer);
   mDrawerLayout = (DrawerLayout) findViewById(R.id.drawer_layout)
        // add a listener to the drawer list view
        mLeftDrawerList.setOnItemClickListener(new DrawerItemClickListener());

}

另外我推荐 Butterknife 库来让你的代码更干净