【发布时间】:2012-06-26 17:18:07
【问题描述】:
当我运行我的应用程序时,我从标题中得到了异常。它的作用是它有一个带有 Hangman 游戏单词的 .txt 文件,我认为访问该文件时会引发异常。我的文件 cuvinte.txt 位于 /assets/。这是我的代码(我跳过了 layout/xml 部分,效果很好):
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() 函数中被调用。
【问题讨论】:
-
你应该在完成这些阅读器后关闭它们
-
为了专业起见,请用英文命名你的变量:)
-
就我而言,我遇到了这种情况,因为我试图在
setText()函数中传递一个 int 参数。