开发者

Android, string resource not found

I'm working on an App, and I am getting weired errors. I put in some String resource in res/values/strings and saved it. Now If I want to access it in an Activity I get an error like this

07-13 11:16:20.050: ERROR/AndroidRuntime(5883): Caused by: android.content.res.Resources$NotFoundException: String resource ID #0x7f060006

And I can't figure out why. Resource looks like this:

<string name="dialog_download_text">please wait while downloading</string>

and I want to Access it like

public void onCreate(Bundle b){
    super.onCreate(b);
    String s = getResources().getStrin开发者_JS百科g(R.string.dialog_download_text);
}

I also looked into R.java and found my Entry

public static final class string {
    public static final int dialog_download_cancel=0x7f060005;
    public static final int dialog_download_text=0x7f060007;
    public static final int dialog_download_title=0x7f060006;
}

I don't know what to do, because I never had a problem like this before. Please help me. Thanks all.


Sometimes I get this error (once every 2 days), and it's because eclipse is not compiling the new code and it's deploying the old apk. I fix this by

  • doing a clean on eclipse (Project -> clean)
  • closing the emulator
  • restarting the adb server by running adb kill-server and adb start-server from the command line


what i had was

import android.R;

instead of

import com.example.project.R;

android.R does not have your resources it only has the general android resources.


Most likely there's an extra import android.R; Then your string can't be found there. Otherwise, clean the project and rebuild it, try again, then report back.


Checkout this thread, it fixed my problem. R cannot be resolved - Android error


Clean the project in eclipse, and try running.If this doesn't resolve your issue, from a already running emulator uninstall the app, and run it again from eclispe.


I am currently writing the "net.superlinux.tcltktutorials" you can find it on play store now. now in the application, I want to use the standard way of using locales instead of my way of doing locales. I had to add for Arabic /res/vlaues-ar and I have the default for English /res/values. The app is about ad based YouTube Playlist TCL/Tk programming language tutorials. now the playlist can be in Arabic and in English. What I noticed is that if you have in the default /res/values 36 entries and in /res/values-ar 35 entries for the same playlist, this will make the ResourceNotFound exception. All you have to do is add the missing entry as empty at the bottom of your list just to make it equal in numbers in English and Arabic, Even if the English playlist is less in numbers.

This was my method of adding to the playlist formed inside the list activity, and also a clever way of using the resources as xml built in data:

package net.superlinux.tcltktutorials;
import java.util.ArrayList;
import java.util.List;
import android.app.ListActivity;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.ListView;

public class ListOfVideos extends ListActivity {

List<String> model = new ArrayList<String>();
ArrayAdapter<String> adapter = null;
List<String> filename = new ArrayList<String>();

public void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState);
    setContentView(R.layout.listview);



     adapter = new ArrayAdapter<String> (this, R.layout.list_item, model);


    load_playlist();
    setListAdapter(adapter);



}


@Override
protected void onListItemClick(ListView l, View v, int position, long id) {
    // TODO Auto-generated method stub
            super.onListItemClick(l, v, position, id);
    String selected_youtube_video=filename.get(position);
    try {Intent i = new Intent(Intent.ACTION_VIEW, Uri.parse("vnd.youtube:"+selected_youtube_video));
startActivity(i);
    }
    catch(Exception e){

        startActivity(new Intent(Intent.ACTION_VIEW, Uri.parse("http://www.youtube.com/watch?v="+selected_youtube_video+"&loop=1&autoplay=1")));
        e.printStackTrace();
    }
}

void load_playlist()
{

    int display_id=0;
    int file_id=0;
    //loop forever until nothing has to be added to the ListView or stop if the list item
    // to be added does not exist.
    for (int i=0;;i++){
        display_id=getResources().getIdentifier("display_"+i, "string", getPackageName());
    if (display_id!=0 && getString(display_id).length()!=0)
        adapter.add(getString(display_id));
    else {
        Log.e("string id not found or empty","R.string.display_"+i );
        return;
    }
    file_id=getResources().getIdentifier("file_"+i, "string", getPackageName());
    if (file_id!=0 && getString(file_id).length()!=0){
    filename.add(getString(file_id));
    }
    else {
        Log.e("string id not found or empty","R.string.file_"+i );
        return;
    }
    }
}

}


I also met this problem and it is very wired at first. 1: I make sure I import the correct R into my class 2: I checked R.java in the gen folder and the resource id is there. 3: I cleaned my project and remove from my phone and relaunch it the exception still there.

Finally I found the reason: I just added my resource string into the strings.xml in language-specific folder(say values-zh) but missed it in the default strings.xml in the default values folder.

then it is solved.


There are two cases you may get this error

1.your strings.xml file is not referred correctly?

Ans:give the correct package name for R.java file as per you application?

2.if you kept correct string.xlm path you may get still error?

Ans:once clean and build the projector restart the eclipse or android studio.

Thanks krishh


For Kotlin use

this->context.resources.getString(R.string.your_string)
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜