英文:
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();
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论