データベースからリストビュー項目を表示しようとしています。しかし、私のアプリケーションはクラッシュし続けます。過去のプロジェクト コードを使用してガイドしますが、まだ運がありません。
モジュール.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
どの部分が間違っているのかわかりません。コードを編集していただければ幸いです。私は今、頭を割っています。これが原因で次のパートに進むことができません。