简体   繁体   English

Java android在运行时崩溃

[英]java android crashes on runtime

I have so far coded a Application that prompts the user to guess a number between 1 and 20. I'm struggling to see why the app crashes when it takes the users number. 到目前为止,我已经编写了一个应用程序代码,该应用程序提示用户猜测1到20之间的数字。我正在努力查看为什么应用程序在获取用户编号时会崩溃。 The first method works where it takes the name. 第一种方法在使用名称的地方起作用。 The guessingGame method is where it handles the users input and checks whether it is equal to the random number generated by the app. 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));
                            }


                }   
            }


            }

    }

This is the log cat from when ran. 这是运行时的原木猫。 The issue is only based in the guessingGame method. 该问题仅基于guessingGame方法。 I would be very grateful if someone would be able to point me in the right direction? 如果有人能够指出正确的方向,我将不胜感激。 Thanks. 谢谢。

   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

According to your logcat it is trying to parse an empty string to an int: 根据您的logcat,它正在尝试将一个空字符串解析为一个int:

03-14 16:21:45.960: E/AndroidRuntime(796): Caused by:    java.lang.NumberFormatException: Invalid int: ""

Here is your problem: 这是您的问题:

int correctGuess = Integer.parseInt(guessCorrect.getText().toString());
int guessValue = Integer.parseInt(guess.getText().toString());

You are converting an edittext to an int without first checking if they are empty or a non int value 您正在将edittext转换为int,而无需先检查它们是否为空或非int值

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM