我正在使用 SimpleCursorAdapter 和 ListView 从数据库中检索数据。这是我的活动课
public class TimelineActivity extends ListActivity {
LoginDataBaseAdapter loginDataBaseAdapter;
@SuppressWarnings("deprecation")
public void onCreate(Bundle icicle) {
super.onCreate(icicle);
setContentView(R.layout.list_view);
LoginDataBaseAdapter db = new LoginDataBaseAdapter(this);
//loginDataBaseAdapter = loginDataBaseAdapter.open();
Cursor cursor = db.test();
startManagingCursor(cursor);
SimpleCursorAdapter adapter = new SimpleCursorAdapter(this,
android.R.layout.simple_list_item_1, cursor,
new String[] {db.get()},
new int[] { android.R.id.text1 });
setListAdapter(adapter);
}
}
这是数据库类。
public Cursor test() {
Cursor cursor = db.query(true, "LOGIN", new String[]{"USERNAME"}, null, null, null, null, null, null);
return cursor;
}
public String get(){
Cursor c = null;
String a = c.getString(c.getColumnIndex("USERNAME"));
return a;
}
和错误信息
07-08 13:48:00.561: E/AndroidRuntime(14179): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.studio/com.example.studio.TimelineActivity}: java.lang.NullPointerException
07-08 13:48:00.561: E/AndroidRuntime(14179): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1651)
07-08 13:48:00.561: E/AndroidRuntime(14179): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1667)
07-08 13:48:00.561: E/AndroidRuntime(14179): at android.app.ActivityThread.access$1500(ActivityThread.java:117)
07-08 13:48:00.561: E/AndroidRuntime(14179): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:935)
07-08 13:48:00.561: E/AndroidRuntime(14179): at android.os.Handler.dispatchMessage(Handler.java:99)
07-08 13:48:00.561: E/AndroidRuntime(14179): at android.os.Looper.loop(Looper.java:130)
07-08 13:48:00.561: E/AndroidRuntime(14179): at android.app.ActivityThread.main(ActivityThread.java:3693)
07-08 13:48:00.561: E/AndroidRuntime(14179): at java.lang.reflect.Method.invokeNative(Native Method)
07-08 13:48:00.561: E/AndroidRuntime(14179): at java.lang.reflect.Method.invoke(Method.java:507)
07-08 13:48:00.561: E/AndroidRuntime(14179): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:907)
07-08 13:48:00.561: E/AndroidRuntime(14179): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:665)
07-08 13:48:00.561: E/AndroidRuntime(14179): at dalvik.system.NativeStart.main(Native Method)
07-08 13:48:00.561: E/AndroidRuntime(14179): Caused by: java.lang.NullPointerException
07-08 13:48:00.561: E/AndroidRuntime(14179): at com.example.studio.LoginDataBaseAdapter.get(LoginDataBaseAdapter.java:217)
07-08 13:48:00.561: E/AndroidRuntime(14179): at com.example.studio.TimelineActivity.onCreate(TimelineActivity.java:25)
07-08 13:48:00.561: E/AndroidRuntime(14179): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
07-08 13:48:00.561: E/AndroidRuntime(14179): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1615)
07-08 13:48:00.561: E/AndroidRuntime(14179): ... 11 more
请帮忙。