此代码有效
public class listActivity extends ListActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
testAdapter mDbHelper = new testAdapter(this);
mDbHelper.open();
Cursor c = mDbHelper.getAllData();
String[] from = new String[] { "name", "title" };
CursorAdapter dataSource = new SimpleCursorAdapter(this,
R.layout.list_row, c, from, new int[] { R.id.name,
R.id.title});
setListAdapter(dataSource);
mDbHelper.close();
}
但是当我尝试在我的一个布局中将适配器设置为列表视图时,它只会崩溃
public class listActivity extends ListActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
testAdapter mDbHelper = new testAdapter(this);
mDbHelper.open();
Cursor c = mDbHelper.getAllData();
String[] from = new String[] { "name", "title" };
CursorAdapter dataSource = new SimpleCursorAdapter(this,
R.layout.list_row, c, from, new int[] { R.id.name,
R.id.title});
ListView itemList = (ListView)findViewById(R.id.itemsList);
itemList.setAdapter(dataSource); // line 29 where it crashes
mDbHelper.close();
}
这就是 LogCat 抛出的
04-13 20:58:09.331: E/DataAdapter(929): opened db
04-13 20:58:09.451: D/AndroidRuntime(929): Shutting down VM
04-13 20:58:09.451: W/dalvikvm(929): threadid=1: thread exiting with uncaught exception (group=0x40015560)
04-13 20:58:09.521: E/AndroidRuntime(929): FATAL EXCEPTION: main
04-13 20:58:09.521: E/AndroidRuntime(929): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.app/com.example.app.listActivity}: java.lang.NullPointerException
04-13 20:58:09.521: E/AndroidRuntime(929): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1647)
04-13 20:58:09.521: E/AndroidRuntime(929): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1663)
04-13 20:58:09.521: E/AndroidRuntime(929): at android.app.ActivityThread.access$1500(ActivityThread.java:117)
04-13 20:58:09.521: E/AndroidRuntime(929): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:931)
04-13 20:58:09.521: E/AndroidRuntime(929): at android.os.Handler.dispatchMessage(Handler.java:99)
04-13 20:58:09.521: E/AndroidRuntime(929): at android.os.Looper.loop(Looper.java:123)
04-13 20:58:09.521: E/AndroidRuntime(929): at android.app.ActivityThread.main(ActivityThread.java:3683)
04-13 20:58:09.521: E/AndroidRuntime(929): at java.lang.reflect.Method.invokeNative(Native Method)
04-13 20:58:09.521: E/AndroidRuntime(929): at java.lang.reflect.Method.invoke(Method.java:507)
04-13 20:58:09.521: E/AndroidRuntime(929): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839)
04-13 20:58:09.521: E/AndroidRuntime(929): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597)
04-13 20:58:09.521: E/AndroidRuntime(929): at dalvik.system.NativeStart.main(Native Method)
04-13 20:58:09.521: E/AndroidRuntime(929): Caused by: java.lang.NullPointerException
04-13 20:58:09.521: E/AndroidRuntime(929): at com.exmaple.app.listActivity.onCreate(listActivity.java:29)
04-13 20:58:09.521: E/AndroidRuntime(929): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
04-13 20:58:09.521: E/AndroidRuntime(929): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1611)
04-13 20:58:09.521: E/AndroidRuntime(929): ... 11 more
我已经为此苦苦挣扎了好几个星期,所以请任何帮助将不胜感激。