在初始化我的导航抽屉组件时,android会抛出空指针异常。我无法弄清楚为什么我会这样做。请先解释一下问题是什么,然后再解决。提前谢谢。
这是我的堆栈跟踪:
09-21 17:24:15.632: E/AndroidRuntime(1842): FATAL EXCEPTION: main
09-21 17:24:15.632: E/AndroidRuntime(1842): Process: com.prome.orcadirectory, PID: 1842
09-21 17:24:15.632: E/AndroidRuntime(1842): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.prome.orcadirectory/com.prome.orcadirectory.MainActivity}: java.lang.NullPointerException
09-21 17:24:15.632: E/AndroidRuntime(1842): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2195)
09-21 17:24:15.632: E/AndroidRuntime(1842): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2245)
09-21 17:24:15.632: E/AndroidRuntime(1842): at android.app.ActivityThread.access$800(ActivityThread.java:135)
09-21 17:24:15.632: E/AndroidRuntime(1842): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1196)
09-21 17:24:15.632: E/AndroidRuntime(1842): at android.os.Handler.dispatchMessage(Handler.java:102)
09-21 17:24:15.632: E/AndroidRuntime(1842): at android.os.Looper.loop(Looper.java:136)
09-21 17:24:15.632: E/AndroidRuntime(1842): at android.app.ActivityThread.main(ActivityThread.java:5017)
09-21 17:24:15.632: E/AndroidRuntime(1842): at java.lang.reflect.Method.invokeNative(Native Method)
09-21 17:24:15.632: E/AndroidRuntime(1842): at java.lang.reflect.Method.invoke(Method.java:515)
09-21 17:24:15.632: E/AndroidRuntime(1842): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:779)
09-21 17:24:15.632: E/AndroidRuntime(1842): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:595)
09-21 17:24:15.632: E/AndroidRuntime(1842): at dalvik.system.NativeStart.main(Native Method)
09-21 17:24:15.632: E/AndroidRuntime(1842): Caused by: java.lang.NullPointerException
09-21 17:24:15.632: E/AndroidRuntime(1842): at com.prome.orcadirectory.MainActivity.onCreate(MainActivity.java:82)
09-21 17:24:15.632: E/AndroidRuntime(1842): at android.app.Activity.performCreate(Activity.java:5231)
09-21 17:24:15.632: E/AndroidRuntime(1842): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1087)
09-21 17:24:15.632: E/AndroidRuntime(1842): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2159)
09-21 17:24:15.632: E/AndroidRuntime(1842): ... 11 more
这是第82行:
listView.setAdapter( myAdapter );
listView和myAdapter在第60和80行的主要活动的onCreate方法中初始化为:
listView = (ListView) findViewById(R.id.drawer_left);
myAdapter = new MyAdapter(this);
这是MyAdapter类:
/* ----- Navigation Drawer Components ----- */
//custom array adapter class
class MyAdapter extends BaseAdapter {
private Context context;
// drawer String array and Drawable icon array
private String[] drawerList;
private int[] icons = { R.drawable.ic_action_group,
R.drawable.ic_action_search,
R.drawable.ic_action_person,
R.drawable.ic_action_add_person,
R.drawable.ic_action_about,
R.drawable.ic_action_back };
public MyAdapter(Context context) {
this.context = context;
// TODO Auto-generated constructor stub
drawerList = context.getResources().getStringArray(R.array.drawer_list);
}
@Override
public int getCount() {
// TODO Auto-generated method stub
return drawerList.length;
}
@Override
public Object getItem(int position) {
// TODO Auto-generated method stub
return drawerList[position];
}
@Override
public long getItemId(int position) {
// TODO Auto-generated method stub
return position;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
// TODO Auto-generated method stub
View row = null;
if( convertView == null ) {
LayoutInflater inflater = (LayoutInflater) context.getSystemService(context.LAYOUT_INFLATER_SERVICE);
row = inflater.inflate(R.layout.custom_row, parent, false);
} else {
row = convertView;
}
TextView titleText = (TextView) row.findViewById(R.id.drawerTextView);
ImageView titleImage = (ImageView) row.findViewById(R.id.drawerImage);
titleText.setText(drawerList[position]);
titleImage.setImageResource(icons[position]);
return row;
}
}
/* ----- Navigation Drawer Components ends ----- */
navigation_drawer.xml
<android.support.v4.widget.DrawerLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/drawer_layout"
android:layout_width="match_parent"
android:layout_height="match_parent">
<FrameLayout
android:id="@+id/main_content"
android:layout_width="match_parent"
android:layout_height="match_parent" />
<ListView
android:id="@+id/drawer_left"
android:layout_width="240dp"
android:layout_height="match_parent"
android:layout_gravity="start"
android:background="#FFFFFF"
android:choiceMode="singleChoice"
android:divider="@android:color/background_dark"
android:dividerHeight="1dp" />
</android.support.v4.widget.DrawerLayout>
custom_row.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal" >
<ImageView
android:id="@+id/drawerImage"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="5dp" />
<TextView
android:id="@+id/drawerTextView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="5dp"
android:textColor="#981717"
android:textAppearance="@android:attr/textAppearanceLarge" />
</LinearLayout>
的strings.xml
<?xml version="1.0" encoding="utf-8"?>
<resources>
<string name="app_name">Orca Directory</string>
<string name="hello_world">Hello world!</string>
<string name="action_settings">Settings</string>
<string name="msg1">Loading Contents. Please wait#8230;</string>
<string name="login_name">User name</string>
<string name="login_pass">Password</string>
<string name="login">Log In</string>
<string name="msg_cancel">Cancel</string>
<string name="msg_quit">Quit</string>
<string name="drawer_open">Drawer Opened</string>
<string name="drawer_close">Drawer Closed</string>
<string-array name="drawer_list">
<item>All members</item>
<item>Search member</item>
<item>Profile</item>
<item>Update Profile</item>
<item>About</item>
<item>Quit</item>
</string-array>
</resources>