到目前为止,我已经编写了一个应用程序,它会提示用户猜测1到20之间的数字。我很难理解应用程序在占用用户数时崩溃的原因。第一种方法适用于名称所在的位置。 guessingGame方法处理用户输入并检查它是否等于应用程序生成的随机数。
package Ass.example.task1c3433870;
import java.util.Random;
import android.app.Activity;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;
import android.view.View.OnClickListener;
public class MainActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.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();
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
public void onClick(View v) {
EditText name = (EditText) findViewById(R.id.name);
String sUsername = name.getText().toString();
TextView displayName = (TextView) findViewById(R.id.displayName);
if(TextUtils.isEmpty(sUsername)) {
displayName.setText(sUsername);
}else{
displayName.setText(sUsername);
Toast.makeText(MainActivity.this, "Button Clicked", Toast.LENGTH_SHORT).show();
}
}
public void guessingGame(View v){
EditText guess = (EditText) findViewById(R.id.guess);
TextView displayGuess = (TextView) findViewById(R.id.displayGuess);
TextView numAttempts = (TextView) findViewById(R.id.numAttempts);
TextView result = (TextView) findViewById(R.id.Result);
TextView guessCorrect = (TextView) findViewById(R.id.guessCorrect);
TextView resetCount = (TextView) findViewById(R.id.resetCount);
int correctGuess = Integer.parseInt(guessCorrect.getText().toString());
int guessValue = Integer.parseInt(guess.getText().toString());
int numAttemptsValue = `enter code`enter code here` here`Integer.parseInt(numAttempts.getText().toString());
int resetCounter = Integer.parseInt(resetCount.getText().toString());
if (guessValue < 1)
{
result.setText("Enter valid number");
guess.getText().clear();
}
else if (guessValue > 20)
{
result.setText("Enter valid number");
guess.getText().clear();
}else
{
int min = 1;
int max = 20;
numAttemptsValue -= 1;
numAttempts.setText(Integer.toString(numAttemptsValue));
if(numAttemptsValue > 1)
{
Random r = new Random();
int i = r.nextInt(max - min + 1) + min;
int randomNumber = i;
displayGuess.setText(Integer.toString(guessValue));
if(randomNumber > guessValue)
{
result.setText("Low Guess");
}
else if(randomNumber < guessValue)
{
result.setText("High Guess");
}
else if (randomNumber == guessValue)
{
result.setText("Correct!!!!!");
correctGuess += 1;
guessCorrect.setText(Integer.toString(correctGuess));
}
else if (numAttemptsValue == 0)
{
result.setText("No more lives! GAME OVER!");
numAttemptsValue += 10;
numAttempts.setText(5);
resetCounter += 1;
resetCount.setText(Integer.toString(correctGuess));
}
}
}
}
}
这是运行时的日志猫。该问题仅基于guessingGame方法。如果有人能够指出我正确的方向,我将非常感激?感谢。
03-14 16:21:33.500: D/dalvikvm(796): Not late-enabling CheckJNI (already on)
03-14 16:21:35.050: D/gralloc_goldfish(796): Emulator without GPU emulation detected.
03-14 16:21:45.910: D/AndroidRuntime(796): Shutting down VM
03-14 16:21:45.910: W/dalvikvm(796): threadid=1: thread exiting with uncaught exception (group=0xb4ad2ba8)
03-14 16:21:45.960: E/AndroidRuntime(796): FATAL EXCEPTION: main
03-14 16:21:45.960: E/AndroidRuntime(796): Process: Ass.example.task1c3433870, PID: 796
03-14 16:21:45.960: E/AndroidRuntime(796): java.lang.IllegalStateException: Could not execute method of the activity
03-14 16:21:45.960: E/AndroidRuntime(796): at android.view.View$1.onClick(View.java:3823)
03-14 16:21:45.960: E/AndroidRuntime(796): at android.view.View.performClick(View.java:4438)
03-14 16:21:45.960: E/AndroidRuntime(796): at android.view.View$PerformClick.run(View.java:18422)
03-14 16:21:45.960: E/AndroidRuntime(796): at android.os.Handler.handleCallback(Handler.java:733)
03-14 16:21:45.960: E/AndroidRuntime(796): at android.os.Handler.dispatchMessage(Handler.java:95)
03-14 16:21:45.960: E/AndroidRuntime(796): at android.os.Looper.loop(Looper.java:136)
03-14 16:21:45.960: E/AndroidRuntime(796): at android.app.ActivityThread.main(ActivityThread.java:5017)
03-14 16:21:45.960: E/AndroidRuntime(796): at java.lang.reflect.Method.invokeNative(Native Method)
03-14 16:21:45.960: E/AndroidRuntime(796): at java.lang.reflect.Method.invoke(Method.java:515)
03-14 16:21:45.960: E/AndroidRuntime(796): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:779)
03-14 16:21:45.960: E/AndroidRuntime(796): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:595)
03-14 16:21:45.960: E/AndroidRuntime(796): at dalvik.system.NativeStart.main(Native Method)
03-14 16:21:45.960: E/AndroidRuntime(796): Caused by: java.lang.reflect.InvocationTargetException
03-14 16:21:45.960: E/AndroidRuntime(796): at java.lang.reflect.Method.invokeNative(Native Method)
03-14 16:21:45.960: E/AndroidRuntime(796): at java.lang.reflect.Method.invoke(Method.java:515)
03-14 16:21:45.960: E/AndroidRuntime(796): at android.view.View$1.onClick(View.java:3818)
03-14 16:21:45.960: E/AndroidRuntime(796): ... 11 more
03-14 16:21:45.960: E/AndroidRuntime(796): Caused by: java.lang.NumberFormatException: Invalid int: ""
03-14 16:21:45.960: E/AndroidRuntime(796): at java.lang.Integer.invalidInt(Integer.java:137)
03-14 16:21:45.960: E/AndroidRuntime(796): at java.lang.Integer.parseInt(Integer.java:358)
03-14 16:21:45.960: E/AndroidRuntime(796): at java.lang.Integer.parseInt(Integer.java:331)
03-14 16:21:45.960: E/AndroidRuntime(796): at Ass.example.task1c3433870.MainActivity.guessingGame(MainActivity.java:77)
03-14 16:21:45.960: E/AndroidRuntime(796): ... 14 more
03-14 16:27:13.464: I/Process(796): Sending signal. PID: 796 SIG: 9
答案 0 :(得分:0)
根据你的logcat,它试图将空字符串解析为int:
03-14 16:21:45.960: E/AndroidRuntime(796): Caused by: java.lang.NumberFormatException: Invalid int: ""
这是你的问题:
int correctGuess = Integer.parseInt(guessCorrect.getText().toString());
int guessValue = Integer.parseInt(guess.getText().toString());
您正在将edittext转换为int,而不先检查它们是否为空或非int值