Reputation: 81
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");
}
**/
}
}
Upvotes: 0
Views: 77
Reputation: 29783
As other answer has pointing out, you're creating a method inside a method which is a code error and you're trying to access a variable inside of another method which is a method scope.
First, we need to resolve the first problem.
If we simplify your code, it will be something like this
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);
// get all the view.
final TextView tv = (TextView) findViewById(R.id.winnerTextView);
button1.setOnClickListener(listener);
button2.setOnClickListener(listener);
button3.setOnClickListener(listener);
button4.setOnClickListener(listener);
button5.setOnClickListener(listener);
button6.setOnClickListener(listener);
button7.setOnClickListener(listener);
button8.setOnClickListener(listener);
button9.setOnClickListener(listener);
/**
public void getWinner() {
tv.setText("does not work");
}
**/
}
}
From the above code, you can see that you're trying to create getWinner()
inside of onCreate()
which is wrong.
It should be:
public class MainActivity extends AppCompatActivity {
protected void onCreate(Bundle savedInstanceState) {
final TextView tv = (TextView) findViewById(R.id.winnerTextView);
}
public void getWinner() {
tv.setText("does not work");
}
}
Now the code is working but it will giving you an error because getWinner()
can't found the tv variable
which is only inside the onCreate()
method. So, you need to make it a class scope like this:
public class MainActivity extends AppCompatActivity {
// make it class scope.
private TextView tv;
protected void onCreate(Bundle savedInstanceState) {
...
TextView tv = (TextView) findViewById(R.id.winnerTextView);
...
}
public void getWinner() {
tv.setText("does not work");
}
}
Both of the problems happen because you're creating a method with a too much line of code. You should always make your method not more than 15 line. This is the best practice to make your code more maintainable in the future.
Don't do too much logic in a single method. Focus only solving one problem in a method. Always use a Divide et impera
principal to separate a bigger problem to many smaller problems.
Upvotes: 1
Reputation: 57
make getWinner() method outside of onCreate() and make tv global variable. it should work.
Upvotes: 0
Reputation: 2301
Define tv at globally and no need to put getWinner() into on create so pickup it out
public class MainActivity extends AppCompatActivity {
public int counter = 1;
private TextView tv;
@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);
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();
}
}
});
}
public void getWinner() {
tv.setText("does not work");
}
}
Upvotes: 0
Reputation: 940
What you may do is the,
Upvotes: 0
Reputation: 21
Try passing textview obj to the method getWinner and then try to set it.getWinner should be outside oncreate method.
Upvotes: 2
Reputation: 99
Try it by putting the getWinner() method outside your onCreate() function and pass the textView reference to it.
I guess it should work.
Upvotes: 1