猿问

在 onClickListener 中生成随机值时应用程序崩溃

当我没有在 EditText 中输入任何内容然后按下“猜测”按钮时,我的应用程序不断崩溃并且我似乎无法找到我的代码的问题。


public void generateRandomNos(){

      Random rand = new Random();

      randomNumber = rand.nextInt(20)+1;

}


btnGuess.setOnClickListener(new View.OnClickListener() {

            @Override

            public void onClick(View view) {

                int guessValue = Integer.parseInt(etNumber.getText().toString());

                String message;


                if(guessValue > randomNumber){

                    message = "Lower !";

                    etNumber.setText("");

                }

                else if (guessValue < randomNumber){

                    message = "Higher !";

                    etNumber.setText("");

                }

                else{

                    message = "You got it right. GG ";

                    etNumber.setText("");

                    generateRandomNos();

                }


                Toast.makeText(MainActivity.this,message,Toast.LENGTH_SHORT).show();

            }

});

当框中没有文本EditText和按下按钮时,应用程序不应崩溃。可能是什么问题呢?


Helenr
浏览 132回答 3
3回答

人到中年有点甜

正如您在问题中提到的那样,当您未在编辑文本中输入任何内容时会发生此错误。所以首先,检查是否edit text为空,然后执行所需的操作btnGuess.setOnClickListener(new View.OnClickListener() {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; @Override&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; public void onClick(View view) {&nbsp; &nbsp; &nbsp; &nbsp;if(!etNumber.getText.toString().equals("")) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;int guessValue =&nbsp; Integer.parseInt(etNumber.getText().toString());&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; String message;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if(guessValue > randomNumber){&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; message = "Lower !";&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; etNumber.setText("");&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; else if (guessValue < randomNumber){&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; message = "Higher !";&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; etNumber.setText("");&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; else{&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; message = "You got it right. GG ";&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; etNumber.setText("");&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; generateRandomNos();&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; Toast.makeText(MainActivity.this,message,Toast.LENGTH_SHORT).show();&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;} else {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;Toast.makeText(MainActivity.this,"EditText Is empty",Toast.LENGTH_SHORT).show();&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;}&nbsp; &nbsp;});注意:并确保您使用android:inputType="number"属性来编辑文本,以便您只能获得数字作为输入

陪伴而非守候

您是否在 EditText 中输入了除十进制数字以外的任何其他字符?parseInt文档说:Parses the string argument as a signed decimal integer.&nbsp;The characters in the string must all be decimal digits,&nbsp;except that the first character may be an ASCII minus sign '-' ('\u002D') to&nbsp;indicate a negative value or an ASCII plus sign '+' ('\u002B') to indicate a&nbsp;positive value. The resulting integer value is returned, exactly as if the&nbsp;argument and the radix 10 were given as arguments to the&nbsp;parseInt(java.lang.String, int) method.您可以使用 try/catch 语法来检查这个try {&nbsp; &nbsp; int value = Integer.parseInt(str);} catch(NumberFormatException ex) {&nbsp; &nbsp; //Input is not a number}

qq_笑_17

当然它会崩溃,因为您从未generateRandomNos()在 Click 事件之前分配此方法。所以你的应用程序不知道randomNumber事件触发前的值是多少。您现在可以做的是在执行 btnGuess clicked 之前获取值randomNumber。例如btnGuess.setOnClickListener(new View.OnClickListener() {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; @Override&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; public void onClick(View view) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; generateRandomNos();&nbsp; // i change this position&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; int guessValue =&nbsp;&nbsp; &nbsp; Integer.parseInt(etNumber.getText().toString());&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; String message;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if(guessValue > randomNumber){&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; message = "Lower !";&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; etNumber.setText("");&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; else if (guessValue < randomNumber){&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; message = "Higher !";&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; etNumber.setText("");&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; else{&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; message = "You got it right. GG ";&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; etNumber.setText("");&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }
随时随地看视频慕课网APP

相关分类

Java
我要回答