I am new to Android (but not to Java), I follow sample exercice NotepadV1 but I get a strange error while executing on virtual device (Hello World worked fine on this same vd):
I get a "Resource not found" exception when running the program. The used ID is correct (Eclipse show it to me as an autocompletion proposal, and it's well defined in R.java). If I use directly the string instead of the resource ID, all things are good.
Here is my string.xml:
<?xml version="1.0" encoding="utf-8"?>
<resources>
<string
name="app_name">Notepad v1</string>
<string
name="no_notes">No Notes Yet</string>
<string
name="menu_insert">Add Item</string>
</resources>
And here is the function where the exception is thrown:
@Override
public boolean onCreateOptionsMenu( Menu menu )
{
boolean result = super.onCreateOptionsMenu( menu );
// menu.add( 0, INSERT_ID, 0, R.string.menu_insert ); // exception !
menu.add( 0, INSERT_ID, 0, "Add Item" ); // ok like this
return result;
}
The commented out line is the one which throws exception. As you see, when giving directly the string instead of resource ID, it pass. I've tried to load this resource elsewhere in the same program, and the exception is thrown everywhere. Other resources are used on other places in the program, without problem.
Anybody have an idea ? Did I missed something ?
Thanks a lot for your ideas
I have had the same issue and cleaning the Project in Eclipse solved it.
Very strange, but I was able to get this to work by referencing the string as getResources().getString(R.string.menu_insert)
and by reordering the string constants in the R.java file. no_notes
had a higher value than menu_insert
, but was listed ahead of menu_insert
. So I listed them in order of numeric constant, and it worked:
public static final class string {
public static final int app_name=0x7f040000;
public static final int menu_insert=0x7f040001;
public static final int no_notes=0x7f040002;
}
Accessing the string through getResources().getString()
usually is sufficient, so this must be some bug in Eclipse or the sdk.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With