我正在创建一个简单的Android应用程序,但是当我运行它时,应用程序停止工作(响应)。我附上了我的申请代码。请帮我。 这是MainActivity.java
这是play.java package com.example.guessit;
import java.util.Random;
import android.R.bool;
import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;
public class Play extends Activity{
EditText et;
Button bt;
int num;
int count = 0;
int min = 1;
int max = 100;
Random r = new Random();
int i1 = r.nextInt(max - min + 1) + min;
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.game_play);
et = (EditText) findViewById(R.id.editText1);
bt = (Button) findViewById(R.id.b1);
final boolean is_guess = false;
num = Integer.parseInt(et.getText().toString());
bt.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
while(is_guess == false)
{
if(num > 100 && num < 0)
{
AlertDialog alertDialog = new AlertDialog.Builder(Play.this).create();
alertDialog.setTitle("Wrong Input !!");
alertDialog.setMessage("Please enter the number within the limit 1 to 100");
alertDialog.show();
}
else if(num > i1)
{
Toast.makeText(getApplicationContext(), "Entered number is greater than guessing number", Toast.LENGTH_LONG);
count++;
}
else if(num < i1)
{
Toast.makeText(getApplicationContext(), "Entered number is smaller than guessing number", Toast.LENGTH_LONG);
count++;
}
else if(num == i1)
{
count++;
AlertDialog alertDialog = new AlertDialog.Builder(Play.this).create();
alertDialog.setTitle("Congratulations ");
alertDialog.setMessage("You Win\n");
Log.d("Total", "Attempt = "+count);
alertDialog.show();
}
}
}
});
}
}
每当我运行这个应用程序时,它运行正常但是当我点击按钮转到play.xml和play.java活动时,它就会运行。但是2或3秒后。它停止了工作。 请帮忙。
答案 0 :(得分:1)
我认为由于这一行你可能会遇到NumberFormatException:
num = Integer.parseInt(et.getText().toString());
您正在将editText值提取到早期,这样做总是会得到一个无法转换为数字的空字符串。您需要做的是仅在用户输入某个值并单击按钮时从editText获取值。所以只需按照以下代码即可。希望它的帮助!
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.game_play);
et = (EditText) findViewById(R.id.editText1);
bt = (Button) findViewById(R.id.b1);
final boolean is_guess = false;
bt.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
int num = 0;
try {
num = Integer.parseInt(et.getText().toString());
} catch (NumberFormatException e) {
num = 0;
e.printStackTrace();
}
while(is_guess == false)
{
if(num > 100 && num < 0)
{
AlertDialog alertDialog = new AlertDialog.Builder(Play.this).create();
alertDialog.setTitle("Wrong Input !!");
alertDialog.setMessage("Please enter the number within the limit 1 to 100");
alertDialog.show();
}
else if(num > i1)
{
Toast.makeText(getApplicationContext(), "Entered number is greater than guessing number", Toast.LENGTH_LONG);
count++;
}
else if(num < i1)
{
Toast.makeText(getApplicationContext(), "Entered number is smaller than guessing number", Toast.LENGTH_LONG);
count++;
}
else if(num == i1)
{
count++;
AlertDialog alertDialog = new AlertDialog.Builder(Play.this).create();
alertDialog.setTitle("Congratulations ");
alertDialog.setMessage("You Win\n");
Log.d("Total", "Attempt = "+count);
alertDialog.show();
}
}
}
});
}
答案 1 :(得分:0)
请将logcat添加到您的问题中以帮助您更好,无论如何您在xml中只定义了1 btn并希望在您的活动类中找到3 btn!
play = (Button) findViewById(R.id.button1);
help = (Button) findViewById(R.id.button2);
about = (Button) findViewById(R.id.button3);
在XML中定义button2和button3并查看结果