Android:android.content.res.Resources$NotFoundException:字符串资源 ID #0x5 [英] Android: android.content.res.Resources$NotFoundException: String resource ID #0x5

查看:26
本文介绍了Android:android.content.res.Resources$NotFoundException:字符串资源 ID #0x5的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当我运行我的应用程序时,我从标题中得到了异常.它的作用是它有一个包含 Hangman 游戏文字的 .txt 文件,我认为在访问该文件时会抛出异常.我的文件 cuvinte.txt 位于/assets/中.这是我的代码(我跳过了布局/xml 部分,效果很好):

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);
}

newGame() 函数在按下新游戏按钮时和活动开始时在 onCreate() 函数中调用.

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

推荐答案

(只是假设,Exception stacktrace 的信息较少)

(Just assumption, less info of Exception stacktrace)

我认为,这一行,incercari.setText(valIncercari); throws Exception因为 valIncercariint

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

应该是这样,

incercari.setText(valIncercari+"");

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

这篇关于Android:android.content.res.Resources$NotFoundException:字符串资源 ID #0x5的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆