“Resource not found” exception while trying to loa

2019-04-18 12:34发布

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

6条回答
再贱就再见
2楼-- · 2019-04-18 13:20

Try removing the R file from the gen Eclipse folder. It will then be regenerated and the problem hopefully resolved.

查看更多
爷的心禁止访问
3楼-- · 2019-04-18 13:25

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.

查看更多
地球回转人心会变
4楼-- · 2019-04-18 13:26

Adding it manually to the strings.xml instead of using the "Resources" tab helped me.

查看更多
Ridiculous、
5楼-- · 2019-04-18 13:38

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

查看更多
\"骚年 ilove
6楼-- · 2019-04-18 13:38

I think:

Resource:

<?xml version="1.0" encoding="utf-8"?>   
<resources>   
    <string name="exEditTextEmpty">Program could not get access to EditText view</string>
</resources>  

Code:

Exception ex = new Exception(getResources().getString(R.string.exEditTextEmpy));
查看更多
小情绪 Triste *
7楼-- · 2019-04-18 13:39

I had a similar issue when I renamed (with refactor) a project. Clean didn't help, but Eclipse restart do help.

查看更多
登录 后发表回答