Warm tip: This article is reproduced from stackoverflow.com, please click
android file string

Android: android.content.res.Resources$NotFoundException: String resource ID #0x5

发布于 2020-04-03 23:45:26

I get the exception from the title when I run my app. What it does is it has a .txt file with words for a Hangman game and I think the exception is thrown when accessing the file. My file, cuvinte.txt is located into /assets/. Here is my code (i skipped the layout/xml part, which works fine):

public void onCreate() {
    // all the onCreate() stuff, then this:
    try {
        AssetManager am = this.getAssets();
        InputStream is = am.open("cuvinte.txt");
        InputStreamReader inputStreamReader = new InputStreamReader(is);
        BufferedReader b = new BufferedReader(inputStreamReader);
        String rand;
        while((rand=b.readLine())!=null){
            cuvinte.add(rand);
        }
    } catch (IOException e) {
        Toast.makeText(this, "No words file", Toast.LENGTH_LONG).show();
        e.printStackTrace();
    }

    newGame(newG);
}

public void newGame(View view){
    Random rand = new Random();
    String stringCuvant = cuvinte.get(rand.nextInt(cuvinte.size()));
    cuvant.setText("");
    System.out.println(stringCuvant);
    for(int i = 0; i< stringCuvant.length(); i++){
        cuvant.append("_ ");
    }
    incercari.setText(valIncercari);
}

The function newGame() is called both when the new game button is pressed and at the beginning of the activity, in the onCreate() function.

Questioner
FloIancu
Viewed
87
user370305 2012-06-25 20:40

(Just assumption, less info of Exception stacktrace)

I think, this line, incercari.setText(valIncercari); throws Exception because valIncercari is int

So it should be,

incercari.setText(valIncercari+"");

Or

incercari.setText(Integer.toString(valIncercari));