从数据库显示listview时出错

时间:2012-07-25 08:40:57

标签: android database sqlite android-listview

我试图从数据库中显示listview项目。但我的应用程序一直在崩溃。我使用我过去的项目代码来指导我,但仍然没有运气。

Module.java

 @Override
protected void onResume() {
    super.onResume();
    ArrayList<String> labels = new ArrayList<String>();
    ListView lstDine = (ListView) this.findViewById(R.id.lst_module);
    dbhelper.onCreate(null);
    Cursor dinners = (Cursor) dbhelper.getAllLabels();
    dinners.close();
    lstDine.setAdapter(new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, labels));
    lstDine.setOnItemClickListener(this);

}

DBHelper.java

public class DBHelper extends SQLiteOpenHelper {
// Database Version
private static final int DATABASE_VERSION = 1;

// Database Name
private static final String DATABASE_NAME = "spinnerExample";

// Labels table name
private static final String TABLE_LABELS = "labels";

// Labels Table Columns names
private static final String KEY_ID = "id";
private static final String KEY_NAME = "name";

public DBHelper(Context context) {
    super(context, DATABASE_NAME, null, DATABASE_VERSION);
}

// Creating Tables
@Override
public void onCreate(SQLiteDatabase db) {
    // Category table create query
    String CREATE_CATEGORIES_TABLE = "CREATE TABLE " + TABLE_LABELS + "("
            + KEY_ID + " INTEGER PRIMARY KEY," + KEY_NAME + " TEXT)";
    db.execSQL(CREATE_CATEGORIES_TABLE);
}

// Upgrading database
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
    // Drop older table if existed
    db.execSQL("DROP TABLE IF EXISTS " + TABLE_LABELS);

    // Create tables again
    onCreate(db);
}

/**
 * Inserting new lable into lables table
 * */
public void insertLabel(String label){
    SQLiteDatabase db = this.getWritableDatabase();

    ContentValues values = new ContentValues();
    values.put(KEY_NAME, label);

    // Inserting Row
    db.insert(TABLE_LABELS, null, values);
    db.close(); // Closing database connection
}

/**
* Removing label
* */
public void removeLabel(String label){
SQLiteDatabase db = this.getWritableDatabase();

ContentValues values = new ContentValues();
values.put(KEY_NAME, label);

// Removing Row
db.delete(TABLE_LABELS, KEY_NAME + "=?", new String[] {label});
db.close(); // Closing database connection
}

/**
 * Getting all labels
 * returns list of labels
 * */
public List<String> getAllLabels(){
    List<String> labels = new ArrayList<String>();

    // Select All Query
    String selectQuery = "SELECT  * FROM " + TABLE_LABELS;

    SQLiteDatabase db = this.getReadableDatabase();
    Cursor cursor = db.rawQuery(selectQuery, null);

    // looping through all rows and adding to list
    if (cursor.moveToFirst()) {
        do {
            labels.add(cursor.getString(1));
        } while (cursor.moveToNext());
    }

    // closing connection
    cursor.close();
    db.close();

    // returning lables
    return labels;
}
}

错误记录

07-25 08:36:37.007: D/dalvikvm(463): GC_EXTERNAL_ALLOC freed 600 objects / 48072 bytes in 58ms
07-25 08:36:40.347: D/dalvikvm(463): GC_EXTERNAL_ALLOC freed 321 objects / 15992 bytes in 104ms
07-25 08:36:42.907: D/AndroidRuntime(463): Shutting down VM
07-25 08:36:42.907: W/dalvikvm(463): threadid=1: thread exiting with uncaught exception (group=0x4001d800)
07-25 08:36:42.937: E/AndroidRuntime(463): FATAL EXCEPTION: main
07-25 08:36:42.937: E/AndroidRuntime(463): java.lang.RuntimeException: Unable to resume activity {sg.edu.tp.iit.mns/sg.edu.tp.iit.mns.Module}: java.lang.NullPointerException
07-25 08:36:42.937: E/AndroidRuntime(463):  at android.app.ActivityThread.performResumeActivity(ActivityThread.java:3128)
07-25 08:36:42.937: E/AndroidRuntime(463):  at android.app.ActivityThread.handleResumeActivity(ActivityThread.java:3143)
07-25 08:36:42.937: E/AndroidRuntime(463):  at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2684)
07-25 08:36:42.937: E/AndroidRuntime(463):  at android.app.ActivityThread.access$2300(ActivityThread.java:125)
07-25 08:36:42.937: E/AndroidRuntime(463):  at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2033)
07-25 08:36:42.937: E/AndroidRuntime(463):  at android.os.Handler.dispatchMessage(Handler.java:99)
07-25 08:36:42.937: E/AndroidRuntime(463):  at android.os.Looper.loop(Looper.java:123)
07-25 08:36:42.937: E/AndroidRuntime(463):  at android.app.ActivityThread.main(ActivityThread.java:4627)
07-25 08:36:42.937: E/AndroidRuntime(463):  at java.lang.reflect.Method.invokeNative(Native Method)
07-25 08:36:42.937: E/AndroidRuntime(463):  at java.lang.reflect.Method.invoke(Method.java:521)
07-25 08:36:42.937: E/AndroidRuntime(463):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868)
07-25 08:36:42.937: E/AndroidRuntime(463):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626)
07-25 08:36:42.937: E/AndroidRuntime(463):  at dalvik.system.NativeStart.main(Native Method)
07-25 08:36:42.937: E/AndroidRuntime(463): Caused by: java.lang.NullPointerException
07-25 08:36:42.937: E/AndroidRuntime(463):  at sg.edu.tp.iit.mns.DBHelper.onCreate(DBHelper.java:36)
07-25 08:36:42.937: E/AndroidRuntime(463):  at sg.edu.tp.iit.mns.Module.onResume(Module.java:50)
07-25 08:36:42.937: E/AndroidRuntime(463):  at android.app.Instrumentation.callActivityOnResume(Instrumentation.java:1149)
07-25 08:36:42.937: E/AndroidRuntime(463):  at android.app.Activity.performResume(Activity.java:3823)
07-25 08:36:42.937: E/AndroidRuntime(463):  at android.app.ActivityThread.performResumeActivity(ActivityThread.java:3118)
07-25 08:36:42.937: E/AndroidRuntime(463):  ... 12 more
我不确定哪个部分出错了。谢谢你的帮助。非常感谢你能为我编辑代码。我现在已经开了几次头了。由于这个原因,我不能继续下一部分。

2 个答案:

答案 0 :(得分:3)

dbhelper.onCreate(null);

在上面一行中,您传递了空引用,因此您正在 引起:

java.lang.NullPointerException
07-25 08:36:42.937: E/AndroidRuntime(463):  at sg.edu.tp.iit.mns.DBHelper.onCreate(DBHelper.java:36)

您可以传递 DBHelper 类对象

dbhelper.onCreate(dbhelper);

答案 1 :(得分:0)

首先,您应该打开数据库 - 您将从数据库中获取数据:

db=openOrCreateDatabase(DATABASE_NAME, MODE_WORLD_WRITEABLE, null);