我正在尝试制作一个简单的Android应用程序。目标是开展2项活动。在第一个(主要活动)中,有一些关于应用程序目的的信息,以及一个开始按钮。单击开始按钮应将活动更改为“CalculatingActivity”。在此活动中,有一个显示数字的文本视图。这个数字是介于1和10之间的两个整数的总和。有4个butttons,每个都显示为int加在一起。其中一个是正确的选择。有一种计算错误的机制,当给出10个正确答案时,程序结束。 但是我无法测试代码,因为当我点击MainActivity中的开始按钮时,屏幕变黑。我试图找出原因。如果我从最后一个while循环中删除了代码,那么第二个活动屏幕显示,这里的代码一定有问题,但是我找不到它是什么......
以下是mainActivity的代码: import android.content.Intent; import android.os.Bundle; import android.support.v7.app.AppCompatActivity; import android.view.Menu; import android.view.MenuItem; import android.view.View; import android.widget.Button; import android.widget.TextView;
public class MainActivity extends AppCompatActivity {
private Button startButton;
private TextView txtView1;
private TextView txtView2;
private TextView txtView3;
private TextView txtView4;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
startButton = (Button) findViewById(R.id.StartButton);
startButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent intent = new Intent(MainActivity.this, CalculatingActivity.class);
startActivity(intent);
}
});
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.menu_main, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
//noinspection SimplifiableIfStatement
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
这是第二个活动的代码(CalculatingActivity):
import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
import java.util.Random;
public class CalculatingActivity extends AppCompatActivity {
private int a,b; // Numbers
private int a1,b1,a2,b2,a3,b3;// False numbers
private int c; //= a + b
private Button button1;
private Button button2;
private Button button3;
private Button button4;
private Button backButton;
private TextView sum;
private TextView resultat;
private int countWrong = 0;
private boolean correctButton1 = false;
private boolean correctButton2 = false;
private boolean correctButton3 = false;
private boolean correctButton4 = false;
private boolean nextRound = false;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_calculating);
Intent intent = getIntent();
final Random rndNumber = new Random();
sum = (TextView) findViewById(R.id.textView);
button1 = (Button) findViewById(R.id.btn1);
button2 = (Button) findViewById(R.id.btn2);
button3 = (Button) findViewById(R.id.btn3);
button4 = (Button) findViewById(R.id.btn4);
resultat = (TextView) findViewById(R.id.textView2);
backButton = (Button) findViewById(R.id.btnBack);
boolean play = false;
for (final int[] countRight = {1}; countRight[0] <= 10; countRight[0]++) {
a = rndNumber.nextInt(9) + 1;
String strA = Integer.toString(a); //Transfering int numbers to strings
b = rndNumber.nextInt(9) + 1;
String strB = Integer.toString(b);
a1 = rndNumber.nextInt(9) + 1;
String strA1 = Integer.toString(a1);
b1 = rndNumber.nextInt(9) + 1;
String strB1 = Integer.toString(b1);
a2 = rndNumber.nextInt(9) + 1;
String strA2 = Integer.toString(a2);
b2 = rndNumber.nextInt(9) + 1;
String strB2 = Integer.toString(b2);
a3 = rndNumber.nextInt(9) + 1;
String strA3 = Integer.toString(a3);
b3 = rndNumber.nextInt(9) + 1;
String strB3 = Integer.toString(b3);
c = a + b;
sum.setText(Integer.toString(c)); //Display sum in Textview (sum)!
String buttonArray[] = {(strA + " + " + strB), (strA1 + " + " + strB1), (strA2 + " + " + strB2), (strA3 + " + " + strB3)}; //Plasserer regnestykkene i rekkefølge.
int[] randomArray = {-1, -1, -1, -1}; //Genererating random Array
int counter = 0;
int randomArrNumber;
while (counter <= 3) {
boolean add = true;
randomArrNumber = rndNumber.nextInt(4);
if (randomArrNumber == randomArray[counter]) {
add = false;
}
if (counter == 1 && randomArrNumber == randomArray[0]) {
add = false;
}
if (counter == 2 && randomArrNumber == randomArray[0]) {
add = false;
}
if (counter == 2 && randomArrNumber == randomArray[1]) {
add = false;
}
if (counter == 3 && randomArrNumber == randomArray[0]) {
add = false;
}
if (counter == 3 && randomArrNumber == randomArray[1]) {
add = false;
}
if (counter == 3 && randomArrNumber == randomArray[2]) {
add = false;
}
if (add == true) {
randomArray[counter] = randomArrNumber;
counter++;
}
} //End while-loop Random Array generated
//Displaying numbers on buttons, and linking right buttons
button1.setText(buttonArray[randomArray[0]]);
if (buttonArray[0].equals(buttonArray[randomArray[0]])) {
correctButton1 = true;
}
button2.setText(buttonArray[randomArray[1]]);
if (buttonArray[0].equals(buttonArray[randomArray[1]])) {
correctButton2 = true;
}
button3.setText(buttonArray[randomArray[2]]);
if (buttonArray[0].equals(buttonArray[randomArray[2]])) {
correctButton3 = true;
}
button4.setText(buttonArray[randomArray[3]]);
if (buttonArray[0].equals(buttonArray[randomArray[3]])) {
correctButton4 = true;
}
//Program runs fine until here. When code under here is taken out second activity runs fine.
//Generating listeners to all buttons:
while (nextRound == false) {
button1.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if (correctButton1 == true) {
countRight[0]++;
nextRound = true;
} else {
countWrong++;
}
} //End onClick
});// End button1 OnClick listener
button2.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if (correctButton2 == true) {
countRight[0]++;
nextRound = true;
} else {
countWrong++;
}
} //End onClick
});// End button2 OnClick listener
button3.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if (correctButton3 == true) {
countRight[0]++;
nextRound = true;
} else {
countWrong++;
}
} //End onClick
});// End button3 OnClick listener
button4.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if (correctButton4 == true) {
countRight[0]++;
nextRound = true;
} else {
countWrong++;
}
} //End onClick(View v)
});// End button4 OnClick listener
}// End while(nextRound == false)
} //End for-countRight-loop
backButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent intent = new Intent(CalculatingActivity.this, MainActivity.class);
startActivity(intent);
}//End On click
});//End backButton listener
}//End onCreate
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.menu_calculating, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
//noinspection SimplifiableIfStatement
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
}
答案 0 :(得分:2)
你在这里有一个无限循环:
while (nextRound == false) {
button1.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if (correctButton1 == true) {
countRight[0]++;
nextRound = true;
} else {
countWrong++;
}
} //End onClick
});// End button1 OnClick listener
button2.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if (correctButton2 == true) {
countRight[0]++;
nextRound = true;
} else {
countWrong++;
}
} //End onClick
});// End button2 OnClick listener
button3.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if (correctButton3 == true) {
countRight[0]++;
nextRound = true;
} else {
countWrong++;
}
} //End onClick
});// End button3 OnClick listener
button4.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if (correctButton4 == true) {
countRight[0]++;
nextRound = true;
} else {
countWrong++;
}
} //End onClick(View v)
});// End button4 OnClick listener
}// End while(nextRound == false)
您只需要设置一次侦听器。只需摆脱while循环就可以继续前进。
答案 1 :(得分:1)
您需要添加意图并在onClick侦听器中运行startActivity(intent);
你在backbutton onClick上做,但不是很多其他的。