我正在尝试在Android中创建数据库。以下是导致错误的代码:
DatabaseHelper helper = new DatabaseHelper(getApplicationContext());
SQLiteDatabase db = helper.getWritableDatabase();
这是我的DatabaseHelper类:
package com.example.livelab;
import android.content.Context;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import android.util.Log;
public class DatabaseHelper extends SQLiteOpenHelper {
public static final String TABLE_CALLS = "calls";
public static final String COLUMN_ID = "_id";
public static final String COLUMN_CALL_NUMBER = "number";
public static final String COLUMN_CALL_DATE = "date";
public static final String COLUMN_CALL_DURATION = "duration";
private static final String DATABASE_NAME = "LiveLab.db";
private static final int DATABASE_VERSION = 1;
// SQL statement for creating our CALLS table.
private static final String CALLS_CREATE = "create table "
+ TABLE_CALLS + "(" + COLUMN_ID
+ " integer primary key autoincrement, " + COLUMN_CALL_NUMBER
+ " integer primary key autoincrement, " + COLUMN_CALL_DATE
+ " integer primary key autoincrement, " + COLUMN_CALL_DURATION
+ " text not null);";
public DatabaseHelper(Context context) {
super(context, DATABASE_NAME, null, DATABASE_VERSION);
}
@Override
public void onCreate(SQLiteDatabase database) {
database.execSQL(CALLS_CREATE);
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
Log.w(DatabaseHelper.class.getName(),
"Upgrading database from version " + oldVersion + " to "
+ newVersion + ", which will destroy all old data");
db.execSQL("DROP TABLE IF EXISTS " + TABLE_CALLS);
onCreate(db);
}
}
出于某种原因,调用helper.getWritableDatabase()会弹出一个新窗口,显示:
ActivityThread.handleCreateService(ActivityThread $ CreateServiceData) 找不到来源。
这是什么原因?
顺便说一句,这都是在服务中,而不是在活动中。