我在这方面几乎是新手,所以我可以使用一些帮助来了解我的应用程序为什么会不断崩溃?我已经尝试了很多方法,这是我迄今为止最好的方法。
public class MainActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
final TextView lastAnswer = (TextView) findViewById(R.id.textView5);
final TextView answerLabel = (TextView) findViewById(R.id.textView1);
final TextView answerLabel1 = (TextView) findViewById(R.id.textView2);
final TextView answerLabel2 = (TextView) findViewById(R.id.textView3);
final EditText enteredNumber = (EditText) findViewById(R.id.editText1);
Button getAnswerButton = (Button) findViewById(R.id.button1);
getAnswerButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View arg0) {
Random randomGenerator = new Random();
//rand gene
int randomNumber2 = randomGenerator.nextInt(99);
int randomNumber = randomGenerator.nextInt(99);
String number = "";
String number2 = "";
String jtext = " Times ";
number2= Integer.toString(randomNumber2);
number= Integer.toString(randomNumber);
answerLabel.setText(jtext);
answerLabel1.setText(number2);
answerLabel2.setText(number);
String content = enteredNumber.getText().toString();
当我评论条件时,应用程序工作正常,但这并不是我想要的全部,我想问题出在条件中,但我似乎找不到它。在日食上没有任何错误。
if (content != null){
int anInt=Integer.parseInt(content);
int result= randomNumber2 *randomNumber;
if(anInt == result ){
Context context =getApplicationContext();
CharSequence text = "That's correct!!";
int duration = Toast.LENGTH_SHORT;
Toast toast = Toast.makeText(context, text, duration);
toast.show();
} else{
Context context = getApplicationContext();
CharSequence text = "That's not correct";
int duration = Toast.LENGTH_SHORT;
Toast toast = Toast.makeText(context, text, duration);
toast.show();
}
lastAnswer.setText(content);
}
}
});}
所以我在这里尝试做的(注意菜鸟代码)只是生成2个随机数,显示它们,然后让用户输入结果,比较它,通知是否正确,然后重新生成..我一直在努力这几天
任何帮助表示赞赏,甚至是遵循此代码的新方法。谢谢。