I have just started learning how to make an android app, and for my first try by myself, I decided to make a Tic Tac Toe game. Unfortunately, whenever i try to run the app on the emulator, it crashes. I checked the logcat, and i have narrowed the crash down to this error
:
03-28 19:23:45.385: E/AndroidRuntime(1095): Caused by:android.content.res.Resources$NotFoundException: String resource ID #0x0
The only problem is, that i can't find where this error is coming from.
Code is:
public class Main extends ActionBarActivity {
TextView compChoice, highscore;
Button rock, paper, scissors;
int scoreCount = 0;
boolean win = false;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.fragment_main);
compChoice = (TextView) findViewById(R.id.compChoice);
highscore = (TextView) findViewById(R.id.highscore);
rock = (Button) findViewById(R.id.rock);
paper = (Button) findViewById(R.id.paper);
scissors = (Button) findViewById(R.id.scissors);
rock.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
boolean won = roll(0);
if (won) {
scoreCount++;
}
else{
scoreCount = 0;
}
}
});
paper.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
boolean won=roll(1);
if (won) {
scoreCount++;
}
else{
scoreCount = 0;
}
}
});
scissors.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
boolean won= roll(2);
if (won) {
scoreCount++;
}
else{
scoreCount = 0;
}
}
});
highscore.setText(scoreCount);
}
public boolean roll(int choice) {
int comp = (int) (Math.random()*3);
if(choice == 0 && comp == 2){
win = true;
compChoice.setText("Scissors");
}
if(choice == 1 && comp == 0){
win = true;
compChoice.setText("Rock");
}
if(choice == 2 && comp == 1){
win = true;
compChoice.setText("Paper");
}
if (choice == 0 &&comp==1) {
win=false;
compChoice.setText("Paper");
}
if (choice == 1 &&comp==2) {
win=false;
compChoice.setText("Scissors");
}
if (choice == 2 &&comp==0) {
win=false;
compChoice.setText("Rock");
}
return win;
}
And my XML file is:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
android:textAlignment="center"
tools:context="com.hosfordryan.tictactoe.Main$PlaceholderFragment" >
<TextView
android:id="@+id/title"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:text="Tic Tac Toe"
android:textSize="45sp" />
<TextView
android:id="@+id/textView2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/title"
android:layout_centerHorizontal="true"
android:layout_marginTop="16dp"
android:text="Choose either Rock, Paper, or Scissors!"
android:textAlignment="center"
android:textSize="20sp" />
<Button
android:id="@+id/rock"
android:layout_width="90sp"
android:layout_height="wrap_content"
android:layout_alignBaseline="@+id/paper"
android:layout_alignBottom="@+id/paper"
android:layout_toLeftOf="@+id/paper"
android:text="Rock"
android:textSize="15sp" />
<TextView
android:id="@+id/textView3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true"
android:textSize="20sp"
android:text="Computer chose:" />
<Button
android:id="@+id/paper"
android:layout_width="90sp"
android:layout_height="wrap_content"
android:layout_above="@+id/textView3"
android:layout_centerHorizontal="true"
android:layout_marginBottom="18dp"
android:text="Paper"
android:textSize="15sp" />
<Button
android:id="@+id/scissors"
android:layout_width="90sp"
android:layout_height="wrap_content"
android:layout_alignBaseline="@+id/paper"
android:layout_alignBottom="@+id/paper"
android:layout_toRightOf="@+id/paper"
android:text="Scissors"
android:textSize="15sp" />
<TextView
android:id="@+id/scoreprompt"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_above="@+id/highscore"
android:layout_centerHorizontal="true"
android:layout_marginBottom="15dp"
android:text="High Score:"
android:textSize="20sp" />
<TextView
android:id="@+id/highscore"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_centerHorizontal="true"
android:layout_marginBottom="64dp"
android:text="0"
android:textSize="20sp" />
<TextView
android:id="@+id/compChoice"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/textView3"
android:layout_centerHorizontal="true"
android:textSize="25sp"
android:layout_marginTop="28dp"
android:text="Null" />
So any ideas on what is causing this error would be a lot of help. And please excuse my bad coding, I am still new to this :)
I believe this line is the problem:
highscore.setText(scoreCount);
The setText()
method has an overload that takes an int referring to a String resource. Change it to:
highscore.setText("" + scoreCount);