I am trying to create a method to determine the winner of the game, not worried about the logic of the finding the winner.
In the code I was attempting to add a getWinner() method that would determine the winner when the counter hit 10 moves. In the getWinner() method I took my textview object tv and wrote tv.setText("worked");. The app would crash. If i skip adding the getWinner() method and just toss in the tv.setText() straight into the code it works though.
Since I am not sure how well I explained what's going on maybe my comments throughout the code can give a better understanding.
public class MainActivity extends AppCompatActivity {
public int counter = 1;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
final Button button1 = (Button) findViewById(R.id.button1);
final Button button2 = (Button) findViewById(R.id.button2);
final Button button3 = (Button) findViewById(R.id.button3);
final Button button4 = (Button) findViewById(R.id.button4);
final Button button5 = (Button) findViewById(R.id.button5);
final Button button6 = (Button) findViewById(R.id.button6);
final Button button7 = (Button) findViewById(R.id.button7);
final Button button8 = (Button) findViewById(R.id.button8);
final Button button9 = (Button) findViewById(R.id.button9);
final TextView tv = (TextView) findViewById(R.id.winnerTextView);
button1.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
if (counter % 2 == 0) {
button1.setText("O");
}
else
{
button1.setText("X");
}
counter++;
if (counter == 10)
{
//getWinner(); ---this does not work
//tv.setText("works"); ---this works
}
}
});
button2.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
if (counter % 2 == 0) {
button2.setText("O");
}
else
{
button2.setText("X");
}
counter++;
if (counter == 10)
{
//getWinner(); ---this does not work
//tv.setText("works"); ---this works
}
}
});
button3.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
if (counter % 2 == 0) {
button3.setText("O");
}
else
{
button3.setText("X");
}
counter++;
if (counter == 10)
{
//getWinner(); ---this does not work
//tv.setText("works"); ---this works
}
}
});
button4.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
if (counter % 2 == 0) {
button4.setText("O");
}
else
{
button4.setText("X");
}
counter++;
if (counter == 10)
{
//getWinner(); ---this does not work
//tv.setText("works"); ---this works
}
}
});
button5.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
if (counter % 2 == 0) {
button5.setText("O");
}
else
{
button5.setText("X");
}
counter++;
if (counter == 10)
{
//getWinner(); ---this does not work
//tv.setText("works"); ---this works
}
}
});
button6.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
if (counter % 2 == 0) {
button6.setText("O");
}
else
{
button6.setText("X");
}
counter++;
if (counter == 10)
{
//getWinner(); ---this does not work
//tv.setText("works"); ---this works
}
}
});
button7.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
if (counter % 2 == 0) {
button7.setText("O");
}
else
{
button7.setText("X");
}
counter++;
if (counter == 10)
{
//getWinner(); ---this does not work
//tv.setText("works"); ---this works
}
}
});
button8.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
if (counter % 2 == 0) {
button8.setText("O");
}
else
{
button8.setText("X");
}
counter++;
if (counter == 10)
{
//getWinner();
}
}
});
button9.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
if (counter % 2 == 0) {
button9.setText("O");
}
else
{
button9.setText("X");
}
counter++;
if (counter == 10)
{
//getWinner(); ---this does not work
//tv.setText("works"); ---this works
}
}
});
/**
public void getWinner() {
tv.setText("does not work");
}
**/
}
}