当EditText为空时防止nullPointerException

时间:2018-12-24 15:01:16

标签: java android nullpointerexception android-edittext

嗨,我正在制作一个应用程序,用于使用EditTexts中的用户输入来计算平均值。当我运行该应用程序时,它可以工作,但是当我将该字段保留为空白时,它将引发nullPointerException。我知道它是什么,以及我一般如何修复它们。对于我的代码,我正在考虑使用if语句检查其是否为空。但是我不知道如何用整数检查一个空/空值。有人可以解释一下对此进行检查的最佳方法/做法是什么吗?

欢迎任何提示等!

我的代码:

final Button calculateButton = findViewById(R.id.calculateAverageButton);
calaculateButton.setOnClickListener(new  View.onClickListener() {
public void onClick(View v) { TextView averageView = findViewById(R.id.averageView);

int grade[] = {Integer.parseInt(((EditText) findViewById(R.id.grade1)).getText().toString());
int weight[] = {Integer.parseInt(((EditText)  findViewById(R.id.weight1)).getText().toString());

int weightTotal = weight[0];

int sum = grade[0] * weight[0];

int average = sum / weightTotal

String averageText = getString(R.string.average);
averageView.setText(averageText + Integer.toString(average));

4 个答案:

答案 0 :(得分:1)

这就是为什么您应该避免执行大量内联代码的原因。

没有空整数,您需要检查编辑文本中的字符串

String value = ((EditText) findViewById(R.id.grade1)).getText().toString();
if(value != null && value.length() > 0){
    try{
        //Since edit text gives you a string you cannot guarantee you have a number input
        int numberValue = Integer.parseInt(value);
    catch(NumberFormatException e){
        //handle exception
    }
}

答案 1 :(得分:0)

我建议对此检查editText是否为空:

if(editText.length()>0){
        //there is text
    }else{
        //your code in case of empty editText
    }

答案 2 :(得分:0)

首先不要在按钮单击侦听器中启动您的视图(例如:averageView),然后放置以下行:

TextView averageView = findViewById(R.id.averageView);

移出按钮的点击侦听器

字符串具有isEmpty()函数,因此为了在初始化您的成绩[]之前检查空字符串:

EditText grades = ((EditText) findViewById(R.id.grade1));
if (!grades.getText().toString().isEmpty())
    do your logic here
else
    show some error

答案 3 :(得分:0)

欢迎来到@ J.Doe

关于您的代码有很多要点。

无论如何,我试图快速提及其中的一些并将其修改为如下所示:

  final Button calculateButton = findViewById(R.id.calculateAverageButton);
 EditText e1 = (EditText) findViewById(R.id.grade1);
 EditText e2 = ((EditText)  findViewById(R.id.weight1);
TextView averageView = findViewById(R.id.averageView);
calaculateButton.setOnClickListener(new  View.onClickListener() {
  public void onClick(View v) {

     /** better practice for checking NullPointerException **/
 if(e1.getText() || e2.getText()){
  Log.d(TAG, "Null values");
return;
 }

String txt1 =  e1.getText().toString();
 String tx2 =  e2.getText().toString();

 boolean emptyE1 = txt1.equals("");
boolean emptyE2 = txt2.equals("");

 /** Checking for empty values **/
  if(emptyE1 || emptyE2){
Log.d(TAG, "Empty values");
return;
 }

   int grade;
 int weightTotal;

/** You have also to   ensure that the strings are numeric**/
try{
grade = new Integer(txt1);
 weightTotal = new Integer(txt1);
  }
 catch(NumberFormatException e){){
   Log.d(TAG, "Number input required");
   return;
      }

          /** Now, you are sure you have received numbers :) **/



      int sum = grade  * weight; // sum ? ;)

 int average = sum / weightTotal

    String averageText = getString(R.string.average);
   averageView.setText(averageText + Integer.toString(average));

      }