0

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

}

}

2
  • try context.getResource().getString(R.string.DATABASE_NAME); Commented Jan 2, 2013 at 4:18
  • I figured out whats wrong guys. Idiot me was using hard coded query with wrong table names. gave a null pointer error and crashed im sorry i wasted everybody's time. Commented Jan 2, 2013 at 17:39

3 Answers 3

1

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.

/* Database file name */
private static final String BD_NAME = "something.db";

public DatabaseHelper(Context context) {
    super(context, DB_NAME, null, null);
}
Sign up to request clarification or add additional context in comments.

4 Comments

I Guess that makes sense design wise. It did work if when i had a static final string. I dont understand why the app crashes the second i attempt to get the string from the xml
but what is problem if we get string from string.xml
I wouldn't know unless I saw the logcat message.
Well im just new to android development, and im just new to proper convention i though it was best to put everything in strings.xml, but i can see the conventions is just to make a static string. Now im just wondering what caused the crash
0

it is wrong

super(context, Resources.getSystem().getString(R.string.DATABASE_NAME), null, 1);

Try This

super(context, context.getResources().getString(R.string.DATABASE_NAME), null, 1);

1 Comment

No still instant crash, but how would you get values from strings.xml without extending activity such as this class what if i have other methods
0

change Resources.getSystem().getString(R.string.DATABASE_NAME)

to

context.getResources().getString(R.string.DATABASE_NAME)

1 Comment

No still instant crash, but how would you get values from strings.xml without extending activity such as this class what if i have other methods

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.