安卓:android.content.res.Resources $ NotFoundException:字符串资源ID#0x5的字符串、资源、res、content

2023-09-03 22:10:22 作者:青栀琉璃裙

我得到的异常从标题的时候我跑我的应用程序。它所做的是它有一个.txt文件的话了刽子手的游戏,我想访问该文件时抛出异常。我的文件,cuvinte.txt位于成/资产/。这是我的code(我跳过了布局/ 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):

// onCreate() with all the 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()被调用时,双方新的游戏按钮pressed,并在活动的开始,在所述的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.

推荐答案

(只是假设,异常堆栈跟踪较少的信息)

(Just assumption, less info of Exception stacktrace)

我想,这条线, incercari.setText(valIncercari); 抛出异常 因为 valIncercari INT

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

所以,它应该是,

incercari.setText(valIncercari+"");

incercari.setText(Integer.toString(valIncercari));
 
精彩推荐