Why can't I use Resources.getSystem() without a Runtime error?
Asked Answered
Y

3

13
public class BobDatabase extends SQLiteOpenHelper{
private static final String DATABASE_NAME = "bob.db";
private static final int DATABASE_VERSION = 1;
public static final String DEFAULT_PROFILE = "default_profile";

public BobDatabase(Context context) 
{
    super(context, DATABASE_NAME, null, DATABASE_VERSION);
}

@Override
public void onCreate(SQLiteDatabase database) 
{
    createProfileTable(database);
    createTimeTable(database);
    createEventTable(database);
    createLocationTable(database);
}

/**
 * Creates a table for Profile objects, executes the string create_profile_table_sql
 * contained within strings.xml
 * @param database
 */
public void createProfileTable(SQLiteDatabase database)
{
    database.execSQL(Resources.getSystem().getString(R.string.create_profile_table_sql));
}}

I get this error

01-14 12:20:57.591: E/AndroidRuntime(1825): Caused by: android.content.res.Resources$NotFoundException: String resource ID #0x7f040003

The code that causes the error is the single line inside createProfileTable specifically, Resources.getSystem().getString(R.string.create_profile_table_sql) if I use a class variable to hold a Context and do context.getString(R.string.create_profile_table_sql) I don't get any errors but I don't want to do that because I want to avoid memory leaks and according to what I know this should work. Any idea what's happening?

Yours answered 14/1, 2012 at 17:36 Comment(1)
This doesn't really answer your question, but the String resources are generally for user-displayable Strings (so you can provide different languages later if you'd like to). In this situation, defining a constant string that defines your create statement would be okay.Curr
C
32

According to Android documentation, Resources.getSystem() only provides system-level resources, not application-level ones (like the resources inside your strings.xml file).

http://developer.android.com/reference/android/content/res/Resources.html#getSystem()

Try using the application's context if you really want to retrieve your strings this way, or take my suggestion in the comment to your question.

Curr answered 14/1, 2012 at 17:50 Comment(2)
Thanks, Brandon. I was unaware that Resources.getSystem() wouldn't return my application resources, though it seems a bit obvious now (hindsight and such). I will also consider using a constant string instead.Yours
+1. It is a very unpleasant and hard-to-be-found error. Surely, useful message.Decontaminate
B
7

Using Resources.getSystem().getWhatEver() you can only access system-wide resources (you get the error because there is no system-wide resource with your ID). Since resource ID are not unique across applications you need to provide the application, when accessing a resource. In Android this is done using Context. So if you want to access some resource you need to use it like this

context.getResources().getString(myID);

Apart from that Brandon's comment is correct.

Bogan answered 14/1, 2012 at 17:52 Comment(2)
So, I must use a context to access strings.xml unless I choose to use a constant string in my Java code instead. Thank you for your answer.Yours
It's getResources with an s at the end like so: context.getResources().getString(R.string.yourStringID)Animatism
D
1

You could get the context parametr in the function by parameter, or by a static variable, or by getApplicationContext() function.

Decontaminate answered 14/1, 2012 at 20:48 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.