我正在尝试设置数组适配器并有以下代码:
private String[] mServices;
private ListView mDrawerList;
//Ends Here
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//For Navigation Drawer
mServices = getResources().getStringArray(R.array.drawer_services);
mDrawerList = (ListView) findViewById (R.id.left_drawer);
mDrawerList.setAdapter(new ArrayAdapter<String>(this, R.layout.drawer_list_item, mServices));
我试图设置适配器的最后一行代码,我得到一个 NullPointerException。如果我尝试导航到包含此代码的页面,应用程序会崩溃。
任何指针?
这是调试:
07-08 20:05:22.885: E/AndroidRuntime(2351): FATAL EXCEPTION: main
07-08 20:05:22.885: E/AndroidRuntime(2351): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.parrot.urlripper/com.parrot.urlripper.google}: java.lang.NullPointerException
07-08 20:05:22.885: E/AndroidRuntime(2351): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1647)
07-08 20:05:22.885: E/AndroidRuntime(2351): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1663)
07-08 20:05:22.885: E/AndroidRuntime(2351): at android.app.ActivityThread.access$1500(ActivityThread.java:117)
07-08 20:05:22.885: E/AndroidRuntime(2351): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:931)
07-08 20:05:22.885: E/AndroidRuntime(2351): at android.os.Handler.dispatchMessage(Handler.java:99)
07-08 20:05:22.885: E/AndroidRuntime(2351): at android.os.Looper.loop(Looper.java:123)
07-08 20:05:22.885: E/AndroidRuntime(2351): at android.app.ActivityThread.main(ActivityThread.java:3683)
07-08 20:05:22.885: E/AndroidRuntime(2351): at java.lang.reflect.Method.invokeNative(Native Method)
07-08 20:05:22.885: E/AndroidRuntime(2351): at java.lang.reflect.Method.invoke(Method.java:507)
07-08 20:05:22.885: E/AndroidRuntime(2351): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839)
07-08 20:05:22.885: E/AndroidRuntime(2351): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597)
07-08 20:05:22.885: E/AndroidRuntime(2351): at dalvik.system.NativeStart.main(Native Method)
07-08 20:05:22.885: E/AndroidRuntime(2351): Caused by: java.lang.NullPointerException
07-08 20:05:22.885: E/AndroidRuntime(2351): at com.parrot.urlripper.google.onCreate(google.java:35)
07-08 20:05:22.885: E/AndroidRuntime(2351): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
07-08 20:05:22.885: E/AndroidRuntime(2351): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1611)