此代码使应用程序崩溃,如果我从静态变量中引用数据库名称,它将起作用。当我尝试从 strings.xml 获取它时,它会使应用程序崩溃。知道为什么会失败吗?这是一个类而不是一个活动,所以我导入了 android.content.res.Resources。另外,如果我尝试 context.getString 它也会崩溃。
import android.content.Context;
import android.content.res.Resources;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteDatabase.CursorFactory;
import android.database.sqlite.SQLiteOpenHelper;
public class DatabaseHelper extends SQLiteOpenHelper {
//public static final String DATABASE_NAME = "library.db";
public static final String TITLE = "title";
public static final String AUTHOR = "author";
public static final String ISBN = "isbn";
public DatabaseHelper(Context context) {
super(context, Resources.getSystem().getString(R.string.DATABASE_NAME), null, 1);
}
@Override
public void onCreate(SQLiteDatabase db) {
db.execSQL("CREATE TABLE books (_id INTEGER PRIMARY KEY AUTOINCREMENT, title TEXT, author TEXT, isbn TEXT);");
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
// TODO Auto-generated method stub
}
}