Android编辑框为空时点击按钮导致崩溃。

huangapple go评论77阅读模式
英文:

Android crash when edittext is empty and tap Button

问题

package com.example.converfuel;

import androidx.appcompat.app.AppCompatActivity;

import android.os.Bundle;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;

public class Ticket extends AppCompatActivity {
    private EditText ED1, ED2;
    private TextView TW;
    private Button calcular;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_ticket);
        ED1 = (EditText) findViewById(R.id.Num1);
        ED2 = (EditText) findViewById(R.id.Num2);
        TW = (TextView) findViewById(R.id.DIVISION);

        calcular = (Button) findViewById(R.id.calcular);
        calcular.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                float Num1 = Float.parseFloat(ED1.getText().toString());
                float Num2 = Float.parseFloat(ED2.getText().toString());
                float DIV = Num1/Num2;
                TW.setText(DIV+"");
                
                double val = DIV;
                val = val*100;
                val = Math.round(val);
                val = val / 100;
                mostrar(val);
            }

            private void mostrar(double DIV){
                TW.setText(DIV + "" + " €");
            }
        });
    }
}
英文:

For some reason which I don't know, when I press the button with the EditText empty, the app crashes, but if I put some numbers, it works perfectly. I don't want to add flags like "This field is enabled", I just want to fix the issue.

package com.example.converfuel;

import androidx.appcompat.app.AppCompatActivity;

import android.os.Bundle;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;

public class Ticket extends AppCompatActivity {
    private EditText ED1, ED2;
    private TextView TW;
    private Button calcular;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_ticket);
        ED1 = (EditText) findViewById(R.id.Num1);
        ED2 = (EditText) findViewById(R.id.Num2);
        TW = (TextView) findViewById(R.id.DIVISION);


        calcular = (Button) findViewById(R.id.calcular);
        calcular.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                float Num1 = Float.parseFloat(ED1.getText().toString());
                float Num2 = Float.parseFloat(ED2.getText().toString());
                float DIV = Num1/Num2;
                TW.setText(DIV+"");

                double val = DIV;
                val = val*100;
                val = Math.round(val);
                val = val / 100;
                mostrar(val);
            }

            private void mostrar(double DIV){
                TW.setText(DIV + "" +" ");
            }
        });
    }
}

答案1

得分: 1

你可能会得到 NumberFormatException 错误

在解析数值之前进行检查

public static boolean isParsableToFloat(String value) { 
  try {  
    Float.parseFloat(value);  
    return true;
  } catch(NumberFormatException e){  
    return false;  
  }  
}

然后

String valueOfED1 = ED1.getText().toString();
String valueOfED2 = ED2.getText().toString();

if (isParsableToFloat(valueOfED1) && isParsableToFloat(valueOfED2)) {
    float Num1 = Float.parseFloat(valueOfED2);
    float Num2 = Float.parseFloat(valueOfED2);
    //...
} else {
    //...
}
英文:

You're probably getting NumberFormatException

Check before parsing the values

public static boolean isParsableToFloat(String value) { 
try {  
Float.parseFloat(value);  
return true;
} catch(NumberFormatException e){  
return false;  
}  
}

Then

String valueOfED1 = ED1.getText().toString();
String valueOfED2 = ED2.getText().toString();
if (isParsableToFloat(valueOfED1) && isParsableToFloat(valueOfED2)) {
float Num1 = Float.parseFloat(valueOfED2);
float Num2 = Float.parseFloat(valueOfED2);
//...
} else {
//...
}

答案2

得分: 0

因为您在Num1和NUM2变量中存储了值,并且尝试执行DIV = Num1/Num2操作,所以会出现错误。

if (ED1.getText().length() > 0 && ED2.getText().length() > 0) {
    // 您的代码
} else {
    Toast.makeText(getApplicationContext(), "错误", Toast.LENGTH_LONG).show();
}
英文:

It's Give Error Because Your are Store Value in Num1 & NUM2 Variable and you try to perform DIV = Num1/Num2 this operation that it Gives Error.

if(ED1.getText().length()>0 && ED2.getText().length()>0)
{
// Your Code
}
else
{
Toast.makeText(getApplicationContext(),"Error",Toast.LENGTH_LONG).show();
}

huangapple
  • 本文由 发表于 2020年5月5日 00:19:58
  • 转载请务必保留本文链接:https://go.coder-hub.com/61596844.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定