香港專業教育學院一直在關注這個帖子的Android DrawLayout實施
Android Navigation Drawer implemented with Activities
但是應用程序崩潰它顯示任何內容之前。同時它不似乎得到了OnCreate中menthod作爲其不會觸發任何斷點
這裏是我的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.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)
findViewById必須被稱爲fter setContentView()。 – Harry
爲什麼要投票? – PowerMan2015