android应用程序崩溃

0ve6wy6x  于 2021-08-25  发布在  Java
关注(0)|答案(0)|浏览(197)

此问题已在此处找到答案

编辑文本显示异常无效int“”(5个答案)
如何防止输入字符串的java.lang.numberformatexception:“n/a”[(五个答案)
五天前关门。
仍然是android应用程序开发的初学者。我正在尝试构建一个简单的应用程序,它将根据用户在edittext中输入的分数向用户显示分数等级。问题是,当用户单击按钮而不输入任何文本时,我的应用程序崩溃。我尝试使用isempty和gettext().tostring.equals(“”)来解决这个问题,但没有效果。我需要做什么?请帮忙。

int score;

public void checkResults (View view){

    Log.i("Results","Button Tapped");

    Button button = (Button) findViewById(R.id.button);

    EditText editTextNumber = (EditText) findViewById(R.id.editTextNumber);

    button.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {

            score = Integer.valueOf(editTextNumber.getText().toString());

            if (editTextNumber.getText().toString().equals("")){

                Toast.makeText(getApplicationContext(), "Please  Enter at least one number", Toast.LENGTH_SHORT).show();
            }

            if(score < 29){

                Toast.makeText(getApplicationContext(), "You failed", Toast.LENGTH_SHORT).show();
            }
            else if (score >= 30 && score <= 39){

                Toast.makeText(getApplicationContext(), "You got F", Toast.LENGTH_SHORT).show();
            } else if (score >= 40 && score <= 49){

                Toast.makeText(getApplicationContext(), "You got E", Toast.LENGTH_SHORT).show();
            } else if (score >= 50 && score <= 59){

                Toast.makeText(getApplicationContext(), "You got D", Toast.LENGTH_SHORT).show();
            } else if (score >= 60 && score <= 69){

                Toast.makeText(getApplicationContext(), "You got C", Toast.LENGTH_SHORT).show();
            } else if (score >= 70 && score <= 79){

                Toast.makeText(getApplicationContext(), "You got B", Toast.LENGTH_SHORT).show();
            } else if (score >= 80 && score <= 100){

                Toast.makeText(getApplicationContext(), "You got A", Toast.LENGTH_SHORT).show();
            }

            else {

               Toast.makeText(getApplicationContext(), "Invalid input", Toast.LENGTH_SHORT).show();
            }

        }

    });

}

暂无答案!

目前还没有任何答案,快来回答吧!

相关问题