类 #1 - 必须从 DB 获取数据并准备 ListAdapter:
public class DataListView extends ListActivity {
public ArrayList<String> results = new ArrayList<String>();
public String tableName = DBHelper.tableName;
public SQLiteDatabase newDB;
public void displayResultList() {
setListAdapter(new ArrayAdapter<String>(this,
android.R.layout.simple_list_item_1, results));
getListView().setTextFilterEnabled(true);
}
public void openAndQueryDatabase() {
try {
DBHelper dbHelper = new DBHelper(this.getApplicationContext());
newDB = dbHelper.getWritableDatabase();
Cursor c = newDB.rawQuery("SELECT FirstName, Age FROM " +
tableName, null);
if (c != null ) {
if (c.moveToFirst()) {
do {
String firstName = c.getString(c.getColumnIndex("FirstName"));
int age = c.getInt(c.getColumnIndex("Age"));
results.add("Name: " + firstName + ", Age: " + age);
}while (c.moveToNext());
}
}
} catch (SQLiteException se ) {
Log.e(getClass().getSimpleName(), "Could not create or Open the database");
} finally {
if (newDB != null)
newDB.execSQL("DELETE FROM " + tableName);
newDB.close();
}
}
}
类 #2 - 必须显示列表:
public class StartActivity extends ListActivity {
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
DataListView list = new DataListView();
list.openAndQueryDatabase();
list.displayResultList();
}
}
但它会抛出一个错误:
01-02 18:22:25.584: E/AndroidRuntime(30425): 致命异常: main 01-02 18:22:25.584: E/AndroidRuntime(30425): java.lang.RuntimeException: 无法启动活动 ComponentInfo{com. saigmn/com.saigmn.StartActivity}: java.lang.NullPointerException 01-02 18:22:25.584: E/AndroidRuntime(30425): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1956) 01-02 18: 22:25.584: E/AndroidRuntime(30425): 在 android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1981) 01-02 18:22:25.584: E/AndroidRuntime(30425): 在 android.app.ActivityThread.access $600(ActivityThread.java:123) 01-02 18:22:25.584: E/AndroidRuntime(30425): 在 android.app.ActivityThread$H.handleMessage(ActivityThread.java:1147) 01-02 18:22:25.584: E/AndroidRuntime(30425): 在 android.os.Handler.dispatchMessage(Handler.java:99) 01-02 18:22:25.584: E/AndroidRuntime(30425): 在 android.os.Looper.loop(Looper.java:137) 01-02 18:22:25.584: E/AndroidRuntime(30425): 在android.app.ActivityThread.main(ActivityThread.java:4424) 01-02 18:22:25.584: E/AndroidRuntime(30425): 在 java.lang.reflect.Method.invokeNative(Native Method) 01-02 18:22 :25.584: E/AndroidRuntime(30425): at java.lang.reflect.Method.invoke(Method.java:511) 01-02 18:22:25.584: E/AndroidRuntime(30425): at com.android.internal。 os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:784) 01-02 18:22:25.584: E/AndroidRuntime(30425): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:551) 01 -02 18:22:25.584: E/AndroidRuntime(30425): at dalvik.system.NativeStart.main(Native Method) 01-02 18:22:25.584: E/AndroidRuntime(30425): Caused by: java.lang.NullPointerException 01-02 18:22:25.584: E/AndroidRuntime(30425): at com.saigmn.DataListView.openAndQueryDatabase(DataListView.java:45) 01-02 18:22:25.584: E/AndroidRuntime(30425): at com .saigmn.StartActivity.onCreate(StartActivity.java:20) 01-02 18:22:25.584: E/AndroidRuntime(30425): 在 android.app.Activity.performCreate(Activity.java:4492) 01-02 18:22 :25.584: E/AndroidRuntime(30425): 在 android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1049) 01-02 18:22:25.584: E/AndroidRuntime(30425): 在 android.app.ActivityThread.performLaunchActivity( ActivityThread.java:1920)22:25.584: E/AndroidRuntime(30425): 在 android.app.Activity.performCreate(Activity.java:4492) 01-02 18:22:25.584: E/AndroidRuntime(30425): 在 android.app.Instrumentation.callActivityOnCreate (Instrumentation.java:1049) 01-02 18:22:25.584: E/AndroidRuntime(30425): 在 android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1920)22:25.584: E/AndroidRuntime(30425): 在 android.app.Activity.performCreate(Activity.java:4492) 01-02 18:22:25.584: E/AndroidRuntime(30425): 在 android.app.Instrumentation.callActivityOnCreate (Instrumentation.java:1049) 01-02 18:22:25.584: E/AndroidRuntime(30425): 在 android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1920)
经过一些测试,我发现 openAndQueryDatabase() 里面有 smth。
此外,将两个调用的方法都放入 StartActivity 类时 - 它工作正常。
请帮我找出这个问题。