Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/190.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Android 从数据库显示listview时出错_Android_Database_Sqlite_Android Listview - Fatal编程技术网

Android 从数据库显示listview时出错

Android 从数据库显示listview时出错,android,database,sqlite,android-listview,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.

我试图显示数据库中的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);

}
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;
}
}
我不确定哪一部分出了问题。谢谢你的帮助。如果你能帮我编辑代码,我将不胜感激。我已经有好几次头痛了。由于这个原因,我无法进入下一部分

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);
在上一行中,您传递的是空引用,因此您得到的是 原因:

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);

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

db=openOrCreateDatabase(DATABASE_NAME, MODE_WORLD_WRITEABLE, null);

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

db=openOrCreateDatabase(DATABASE_NAME, MODE_WORLD_WRITEABLE, null);

为什么要调用dbhelper.onCreate(null)?为什么要调用dbhelper.onCreate(null)?他正在实现SQLiteOpenHelper。在这种情况下,不需要自己打开数据库。他正在实现SQLiteOpenHelper。在这种情况下,不需要自己打开数据库。