0

类 #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 类时 - 它工作正常。

请帮我找出这个问题。

4

1 回答 1

2
if (newDB != null) 
  newDB.execSQL("DELETE FROM " + tableName);
  newDB.close();

可能是源头。将两个语句都放在 if 块中,以确保close()不会对null newDB实例进行调用。尝试:

if (newDB != null) 
{ 
  newDB.execSQL("DELETE FROM " + tableName);
  newDB.close();
}

现在我很确定这是你的 NullPointer 的来源。检查 DataListView.java 中的第 45 行。

于 2013-01-02T16:35:57.513 回答