我正在开发一个ATM安卓应用程序,每当应用程序打开它就会崩溃。 这是我的代码:
public class MainActivity extends AppCompatActivity {
TextView balanceText;
EditText input;
Button withdrawButton;
int balance = 0;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//Create the random number
Random number = new Random();
int balance = number.nextInt(10000);
//Initialize the variables
balanceText = (TextView)findViewById(R.id.balanceText);
input = (EditText)findViewById(R.id.input);
withdrawButton = (Button)findViewById(R.id.withdrawButton);
do {
balanceText.setText(balance);
} while (balance > 0);
}
public void withdraw(View view) {
try {
String text = input.getText().toString();
int withdrawal = Integer.parseInt(text);
balance = balance - withdrawal;
if (withdrawal > balance) {
balanceText.setText("Insufficient funds");
}
}
catch (Exception e) {
balanceText.setText("Something went wrong");
}
}
}
注意:我没有在模拟器上朗读,所以我不知道我得到了什么异常或错误。
答案 0 :(得分:0)
onCreate中的循环:
do {
balanceText.setText(balance);
} while (balance > 0);
do {
balanceText.setText(balance);
} while (balance > 0);