[英]Screen goes black when changing activity in Android app
我正在嘗試制作一個簡單的android應用。 目標是進行2個活動。 在第一個(主要活動)中,有一些關於應用程序目的的信息,以及一個啟動按鈕。 單擊開始按鈕應將活動更改為“ CalculatingActivity”。 在此活動中,有一個顯示數字的文本視圖。 此數字是1到10之間的兩個整數的總和。共有4個對接點,每個整數都顯示為整數。 其中之一是正確的選擇。 有一種計數錯誤的機制,當給出10個正確答案時,程序結束。 但是我還不能測試代碼,因為當我在MainActivity中單擊開始按鈕時,屏幕變黑了。 我試圖找出原因。 如果我從上一個while循環中切出代碼,則會顯示第二個活動屏幕,所以這里的代碼肯定有問題,但是我無法確定它是什么...
這是mainActivity的代碼:import android.content.Intent; 導入android.os.Bundle; 導入android.support.v7.app.AppCompatActivity; 導入android.view.Menu; 導入android.view.MenuItem; 導入android.view.View; 導入android.widget.Button; 導入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);
}
}
您在這里有一個無限循環:
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循環,您就可以繼續前進。
您需要添加意圖並運行startActivity(intent);
在您的onClick偵聽器中
您可以在后退按鈕onClick上執行此操作,但其他按鈕則不多。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.