我尝试创建一个猜数游戏,我设置了一系列数字和编辑文本供用户输入数字。我试图输入,例如" 50"但是当我试图删除时,我只能删除" 0"。如果我继续删除号码" 5"该应用程序崩溃了。以下是我的MainActivity代码:
import java.util.Random;
public class MainActivity extends AppCompatActivity {
TextView textViewChoice, textViewStatus;
EditText editTextNum ;
Button buttonSetting;
SharedPreferences preferences;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
textViewChoice = (TextView) findViewById(R.id.textViewChoice);
textViewStatus =(TextView) findViewById(R.id.textviewStatus);
editTextNum = (EditText) findViewById(R.id.edittextGuess);
buttonSetting =(Button) findViewById(R.id.buttonSetting);
preferences = getSharedPreferences("LIMITS",MODE_PRIVATE);
buttonSetting.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Intent intent = new Intent(MainActivity.this, SecondaryActivity.class);
startActivity(intent);
}
});
}
@Override
protected void onStart() {
super.onStart();
final int MinNumber = preferences.getInt("Seekbar Min", 0);
final int MaxNumber = preferences.getInt("Seekbar Max", 0);
textViewChoice.setText("" + MinNumber + "-" + MaxNumber);
editTextNum.addTextChangedListener(new TextWatcher() {
@Override
public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) {
}
@Override
public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) {
Random random = new Random();
int RanNum = random.nextInt(MaxNumber-MinNumber)+MinNumber;
String StringEditNum= editTextNum.getText().toString();
int EditNum = Integer.parseInt(StringEditNum);
if (EditNum >RanNum){
textViewStatus.setText("Enter Higher Number");
}
else if(EditNum <RanNum){
textViewStatus.setText("Enter Lower Numer!");
}
else if (StringEditNum.equals("")){
Toast.makeText(MainActivity.this,"Error!",Toast.LENGTH_SHORT).show();
}
else {
textViewStatus.setText("You won! You Nutz!");
}
}
@Override
public void afterTextChanged(Editable editable) {
}
});
}
@Override
protected void onStop() {
super.onStop();
}
@Override
protected void onDestroy() {
super.onDestroy();
}
}
我调试后得到这个:
E / AndroidRuntime:致命异常:主要 处理:com.example.user.ggame,PID:3291 java.lang.NumberFormatException:对于输入字符串:&#34;&#34; 在java.lang.Integer.parseInt(Integer.java:620) 在java.lang.Integer.parseInt(Integer.java:643) at com.example.user.ggame.MainActivity $ 2.onTextChanged(MainActivity.java:71) 在android.widget.TextView.sendOnTextChanged(TextView.java:9359) 在android.widget.TextView.handleTextChanged(TextView.java:9446) 在android.widget.TextView $ ChangeWatcher.onTextChanged(TextView.java:11904) 在android.text.SpannableStringBuilder.sendTextChanged(SpannableStringBuilder.java:1252) 在android.text.SpannableStringBuilder.replace(SpannableStringBuilder.java:573) 在android.text.SpannableStringBuilder.delete(SpannableStringBuilder.java:229) 在android.text.SpannableStringBuilder.delete(SpannableStringBuilder.java:227) 在android.text.method.BaseKeyListener.backspaceOrForwardDelete(BaseKeyListener.java:370) 在android.text.method.BaseKeyListener.backspace(BaseKeyListener.java:71) 在android.text.method.BaseKeyListener.onKeyDown(BaseKeyListener.java:479) 在android.text.method.NumberKeyListener.onKeyDown(NumberKeyListener.java:148) 在android.widget.TextView.doKeyDown(TextView.java:7316) 在android.widget.TextView.onKeyDown(TextView.java:7093) 在android.view.KeyEvent.dispatch(KeyEvent.java:2688) 在android.view.View.dispatchKeyEvent(View.java:11662) 在android.view.ViewGroup.dispatchKeyEvent(ViewGroup.java:1827) 在android.view.ViewGroup.dispatchKeyEvent(ViewGroup.java:1827) 在android.view.ViewGroup.dispatchKeyEvent(ViewGroup.java:1827) 在android.view.ViewGroup.dispatchKeyEvent(ViewGroup.java:1827) 在android.view.ViewGroup.dispatchKeyEvent(ViewGroup.java:1827) 在android.view.ViewGroup.dispatchKeyEvent(ViewGroup.java:1827) 在android.view.ViewGroup.dispatchKeyEvent(ViewGroup.java:1827) 在com.android.internal.policy.DecorView.superDispatchKeyEvent(DecorView.java:437) 在com.android.internal.policy.PhoneWindow.superDispatchKeyEvent(PhoneWindow.java:1818) 在android.app.Activity.dispatchKeyEvent(Activity.java:3252) 在android.support.v7.app.AppCompatActivity.dispatchKeyEvent(AppCompatActivity.java:534) 在android.support.v7.view.WindowCallbackWrapper.dispatchKeyEvent(WindowCallbackWrapper.java:58) 在android.support.v7.app.AppCompatDelegateImplBase $ AppCompatWindowCallbackBase.dispatchKeyEvent(AppCompatDelegateImplBase.java:316) 在com.android.internal.policy.DecorView.dispatchKeyEvent(DecorView.java:351) 在android.view.ViewRootImpl $ ViewPostImeInputStage.processKeyEvent(ViewRootImpl.java:4714) 在android.view.ViewRootImpl $ ViewPostImeInputStage.onProcess(ViewRootImpl.java:4586) 在android.view.ViewRootImpl $ InputStage.deliver(ViewRootImpl.java:4128) 在android.view.ViewRootImpl $ InputStage.onDeliverToNext(ViewRootImpl.java:4181) 在android.view.ViewRootImpl $ InputStage.forward(ViewRootImpl.java:4147) 在android.view.ViewRootImpl $ AsyncInputStage.forward(ViewRootImpl.java:4274) 在android.view.ViewRootImpl $ InputStage.apply(ViewRootImpl.java:4155) 在android.view.ViewRootImpl $ AsyncInputStage.apply(ViewRootImpl.java:4331) 在android.view.ViewRootImpl $ InputStage.deliver(ViewRootImpl.java:4128) 在android.view.ViewRootImpl $ InputStage.onDeliverToNext(ViewRootImpl.java:4181) 在android.view.ViewRootImpl $ InputStage.forward(ViewRootImpl.java:4147) 在android.view.ViewRootImpl $ InputStage.apply(ViewRootImpl.java:4155) 在android.view.ViewRootImpl $ InputStage.deliver(ViewRootImpl.java:4128) 在android.view.ViewRootImpl $ InputStage.onDeliverToNext(ViewRootImpl.java:4181) 在android.view.ViewRootImpl $ InputStage.forward(ViewRootImpl.java:4147) 在android.view.ViewRootImpl $ AsyncInputStage.forward(ViewRootImpl.java:4307) 在android.view.ViewRootImpl $ ImeInputStage.onFinishedInputEvent(ViewRootImpl.java:4468) 在android.view.inputmethod.InputMethodManager $ PendingEvent.run(InputMethodManager.java:2435) 在android.view.inputmethod.InputMethodManager.invokeFinishedInputEventCallback(InputMethodManager.java:1998) E / AndroidRuntime:在android.view.inputmethod.InputMethodManager.finishedInputEvent(InputMethodManager.java:1989) 在android.view.inputmethod.InputMethodManager $ ImeInputEventSender.onInputEventFinished(InputMethodManager.java:2412) 在android.view.InputEventSender.dispatchInputEventFinished(InputEventSender.java:141) 在android.os.MessageQueue.nativePollOnce(本机方法) 在android.os.MessageQueue.next(MessageQueue.java:325) 在android.os.Looper.loop(Looper.java:142) 在android.app.ActivityThread.main(ActivityThread.java:6541) at java.lang.reflect.Method.invoke(Native Method) 在com.android.internal.os.Zygote $ MethodAndArgsCaller.run(Zygote.java:240) 在com.android.internal.os.ZygoteInit.main(ZygoteInit.java:767)
答案 0 :(得分:1)
你的问题在这一行:
add###
虽然清除 String StringEditNum= editTextNum.getText().toString();
int EditNum = Integer.parseInt(StringEditNum);
5
的值将为空字符串,但您无法解析空字符串以使其崩溃
尝试添加:
StringEditNum
答案 1 :(得分:1)
检查以下代码。
editTextNum.addTextChangedListener(new TextWatcher() {
@Override
public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) {
}
@Override
public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) {
if(editTextNum.getText().toString().length() > 0)
{
Random random = new Random();
int RanNum = random.nextInt(MaxNumber-MinNumber)+MinNumber;
String StringEditNum= editTextNum.getText().toString();
int EditNum = Integer.parseInt(StringEditNum);
if (EditNum >RanNum){
textViewStatus.setText("Enter Higher Number");
}
else if(EditNum <RanNum){
textViewStatus.setText("Enter Lower Numer!");
}
else if (StringEditNum.equals("")){
Toast.makeText(MainActivity.this,"Error!",Toast.LENGTH_SHORT).show();
}
else {
textViewStatus.setText("You won! You Nutz!");
}
}
}
@Override
public void afterTextChanged(Editable editable) {
}
});
答案 2 :(得分:0)
尝试以下代码
Random random = new Random();
int RanNum = random.nextInt(MaxNumber-MinNumber)+MinNumber;
String StringEditNum= editTextNum.getText().toString();
if(!StringEditNum.equals("")){
int EditNum = Integer.parseInt(StringEditNum);
if (EditNum >RanNum){
textViewStatus.setText("Enter Higher Number");
}else if(EditNum <RanNum){
textViewStatus.setText("Enter Lower Numer!");
}else {
textViewStatus.setText("You won! You Nutz!");
}
} else{
Toast.makeText(MainActivity.this,"Try again!",Toast.LENGTH_SHORT).show();
}
答案 3 :(得分:0)
试试这个
这会产生java.lang.IllegalArgumentException: n <= 0: 0
,因为绑定应该是random.nextInt(bound)
0&lt; = bound
Random random = new Random();
int RanNum = random.nextInt(MaxNumber-MinNumber)+MinNumber;
try this
Random random = new Random();
int RanNum = random.nextInt((MaxNumber-MinNumber)+1);
2.转换为整数
时,2.String不应为null @Override
public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) {
Random random = new Random();
int RanNum = random.nextInt((MaxNumber-MinNumber)+1);
String StringEditNum= editTextNum.getText().toString();
if (!TextUtils.isEmpty(editTextNum.getText().toString()))
{
int EditNum = Integer.parseInt(StringEditNum);
if (EditNum >RanNum){
textViewStatus.setText("Enter Higher Number");
}
else if(EditNum <RanNum){
textViewStatus.setText("Enter Lower Numer!");
}
else if (StringEditNum.equals("")){
Toast.makeText(MainActivity.this,"Error!",Toast.LENGTH_SHORT).show();
}
else {
textViewStatus.setText("You won! You Nutz!");
}
}else {
}
}
答案 4 :(得分:0)
检查 StringEditNum 非空或空,然后再将其转换为 int
if(!StringEditNum.equalsIgnoreCase("")){
int EditNum = Integer.parseInt(StringEditNum);
}
答案 5 :(得分:0)
问题是你正在使用textWatcher监听器,每当你编写/删除这个监听器的东西时。现在,当你写出数字50时,你写了5和0.然后当你删除它时,你先删除0,然后留下5.在这种情况下,以下行是好的
int EditNum = Integer.parseInt(StringEditNum);
但是当你删除5时,StringEditNum变为“”,上面的行将导致java.lang.NumberFormatException。所以请检查下面的代码,使其完美运作
import java.util.Random;
public class MainActivity extends AppCompatActivity {
TextView textViewChoice, textViewStatus;
EditText editTextNum ;
Button buttonSetting;
SharedPreferences preferences;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
textViewChoice = (TextView) findViewById(R.id.textViewChoice);
textViewStatus =(TextView) findViewById(R.id.textviewStatus);
editTextNum = (EditText) findViewById(R.id.edittextGuess);
buttonSetting =(Button) findViewById(R.id.buttonSetting);
preferences = getSharedPreferences("LIMITS",MODE_PRIVATE);
buttonSetting.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Intent intent = new Intent(MainActivity.this, SecondaryActivity.class);
startActivity(intent);
}
});
}
@Override
protected void onStart() {
super.onStart();
final int MinNumber = preferences.getInt("Seekbar Min", 0);
final int MaxNumber = preferences.getInt("Seekbar Max", 0);
textViewChoice.setText("" + MinNumber + "-" + MaxNumber);
editTextNum.addTextChangedListener(new TextWatcher() {
@Override
public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) {
}
@Override
public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) {
Random random = new Random();
int RanNum = random.nextInt(MaxNumber-MinNumber)+MinNumber;
String StringEditNum= editTextNum.getText().toString();
if(!StringEditNum.equals("")){
int EditNum = Integer.parseInt(StringEditNum);
if (EditNum >RanNum){
textViewStatus.setText("Enter Higher Number");
}
else if(EditNum <RanNum){
textViewStatus.setText("Enter Lower Numer!");
}
else {
textViewStatus.setText("You won! You Nutz!");
}
}else{
Toast.makeText(MainActivity.this,"Error!",Toast.LENGTH_SHORT).show();
}
}
@Override
public void afterTextChanged(Editable editable) {
}
});
}
@Override
protected void onStop() {
super.onStop();
}
@Override
protected void onDestroy() {
super.onDestroy();
}
}