我正在尝试创建一种方法来确定游戏的赢家,而不是担心找到胜利者的逻辑。
在代码中,我试图添加一个getWinner()方法,当计数器达到10次移动时,该方法将确定胜出者。在getWinner()方法中,我使用了textview
对象tv并编写了tv.setText(“working”);。该应用程序会崩溃。如果我跳过添加getWinner()方法,只需将tv.setText()直接放入代码即可。
由于我不确定我的解释是什么,也许我在整个代码中的评论可以提供更好的理解。
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");
}
**/
}
}
答案 0 :(得分:2)
尝试将textview obj传递给方法getWinner,然后尝试设置它.getWinner应该在oncreate方法之外。
答案 1 :(得分:1)
通过将getWinner()方法放在onCreate()函数之外并将textView引用传递给它来尝试它。
我想它应该有用。
答案 2 :(得分:1)
正如其他答案所指出的那样,你在方法中创建了一个方法,这是一个代码错误,而你正在尝试访问另一个方法范围内的变量。
首先,我们需要解决第一个问题。
如果我们简化您的代码,它将是这样的
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");
}
**/
}
}
从上面的代码中,您可以看到您试图在getWinner()
内部创建onCreate()
,这是错误的。
应该是:
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");
}
}
现在代码正常运行,但由于getWinner()
无法找到仅在variable
方法内的电视onCreate()
,因此会给您一个错误。所以,你需要把它变成这样的类范围:
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");
}
}
这两个问题都发生了,因为您正在创建一个包含过多代码的方法。你应该总是让你的方法不超过15行。这是使您的代码在将来更易于维护的最佳实践。
不要在单个方法中做太多逻辑。专注于解决方法中的一个问题。始终使用Divide et impera
主体将更大的问题与许多较小的问题分开。
答案 3 :(得分:0)
你可能会做的是,
答案 4 :(得分:0)
全局定义tv,无需将getWinner()置于创建中,因此将其取出
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");
}
}
答案 5 :(得分:0)
在onCreate()之外创建getWinner()方法并制作tv全局变量。它应该工作。