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.
(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));
or even better:
Integer.toString(valIncercari);
From above code there is no any other code line which cause this exception. Only the line which I pointed in my answer.
same as String.valueOf(); which does call Integer.toString and other respective toString methods
I am wondering why Android Studio does not advice you. I had the same problem setting an int value into TextView.setText for error.
@bheatcoker - because
setText(int)
also used to get string resource by id. And In Android all resource id hasint
value.