Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

"Resource not found" exception while trying to load resource in NotepadV1 sample exercice

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

like image 554
Esppat Avatar asked Mar 06 '11 19:03

Esppat


2 Answers

I have had the same issue and cleaning the Project in Eclipse solved it.

like image 86
rndstr Avatar answered Sep 21 '22 08:09

rndstr


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.

like image 36
adamcodes Avatar answered Sep 20 '22 08:09

adamcodes