This code crashes the application, If i refer to database name from the static variable it works. When i try to get it from the strings.xml it crashes the app. Any idea why it fails? This is a class not an activity so i imported android.content.res.Resources. Also if i try context.getString it will also crash.
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
}
}
You don’t need to use a string resource for your database name, and in fact it doesn’t make much sense to. String resources are mainly for anything the user will see so that you can provide alternative resources for other configurations (specifically, other languages). What you are providing is essentially a file name for your database. Since this is entirely internal to your app and the user will never see it, you should just use a static final String with the name you want for your database file.