我正在创建一个应用程序,我有三个editText和一个Button。我正在做的是在EditText中获取一些输入,进行计算并按下按钮进入另一个活动。
我的问题是当我没有输入任何值并按下按钮时应用程序崩溃 这是我的代码 -
btn.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
Double x = Double.parseDouble(editText1.getText().toString());
try{
if(x == null || x.equals("")){
Toast.makeText(getApplicationContext(), "Insert All The Fields", Toast.LENGTH_LONG).show();
return;
}else{
}
}
catch(Exception e){
Toast.makeText(getApplicationContext(), "Insert All The Fields", Toast.LENGTH_LONG).show();
}
Double y = Double.parseDouble(editText2.getText().toString());
try{
if(y == null || y.equals("")){
Toast.makeText(getApplicationContext(), "Insert All The Fields", Toast.LENGTH_LONG).show();
return;
}else{
}
}
catch(Exception e){
Toast.makeText(getApplicationContext(), "Insert All The Fields", Toast.LENGTH_LONG).show();
}
Double z = Double.parseDouble(editText3.getText().toString());
try{
if(z == null || z.equals("")){
Toast.makeText(getApplicationContext(), "Insert All The Fields", Toast.LENGTH_LONG).show();
return;
}else{
}
}
catch(Exception e){
Toast.makeText(getApplicationContext(), "Insert All The Fields", Toast.LENGTH_LONG).show();
}
i = x+y+z;
Intent intent = new Intent(getApplicationContext(),SecondActivity.class);
intent.putExtra("key", +i);
startActivity(intent);
这是Logcat
01-04 05:43:20.342: E/AndroidRuntime(1814): FATAL EXCEPTION: main
01-04 05:43:20.342: E/AndroidRuntime(1814): Process: com.example.calculatorpd, PID: 1814
01-04 05:43:20.342: E/AndroidRuntime(1814): java.lang.NumberFormatException: Invalid double: ""
01-04 05:43:20.342: E/AndroidRuntime(1814): at java.lang.StringToReal.invalidReal(StringToReal.java:63)
01-04 05:43:20.342: E/AndroidRuntime(1814): at java.lang.StringToReal.parseDouble(StringToReal.java:248)
01-04 05:43:20.342: E/AndroidRuntime(1814): at java.lang.Double.parseDouble(Double.java:295)
01-04 05:43:20.342: E/AndroidRuntime(1814): at com.example.calculatorpd.MainActivity$1.onClick(MainActivity.java:76)
01-04 05:43:20.342: E/AndroidRuntime(1814): at android.view.View.performClick(View.java:4438)
01-04 05:43:20.342: E/AndroidRuntime(1814): at android.view.View$PerformClick.run(View.java:18422)
01-04 05:43:20.342: E/AndroidRuntime(1814): at android.os.Handler.handleCallback(Handler.java:733)
01-04 05:43:20.342: E/AndroidRuntime(1814): at android.os.Handler.dispatchMessage(Handler.java:95)
01-04 05:43:20.342: E/AndroidRuntime(1814): at android.os.Looper.loop(Looper.java:136)
01-04 05:43:20.342: E/AndroidRuntime(1814): at android.app.ActivityThread.main(ActivityThread.java:5017)
01-04 05:43:20.342: E/AndroidRuntime(1814): at java.lang.reflect.Method.invokeNative(Native Method)
01-04 05:43:20.342: E/AndroidRuntime(1814): at java.lang.reflect.Method.invoke(Method.java:515)
01-04 05:43:20.342: E/AndroidRuntime(1814): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:779)
01-04 05:43:20.342: E/AndroidRuntime(1814): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:595)
01-04 05:43:20.342: E/AndroidRuntime(1814): at dalvik.system.NativeStart.main(Native Method)
答案 0 :(得分:1)
问题是你正在解析一些null的文本。 因此,在进行导致异常的解析double之前,首先检查文本本身。
btn.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
Double x, y,z;
try
{
String a = editText1.getText().toString();
if(a == null || a.equals(""))
{
x = 0;
Toast.makeText(getApplicationContext(),
"Insert All The Fields", Toast.LENGTH_LONG).show();
return;
}
else
{
x = Double.parseDouble(a);
}
}
catch(Exception e){
Toast.makeText(getApplicationContext(),
"Insert All The Fields", Toast.LENGTH_LONG).show();
}
try{
String b = editText2.getText().toString();
if(b == null || b.equals(""))
{
y = 0;
Toast.makeText(getApplicationContext(),
"Insert All The Fields", Toast.LENGTH_LONG).show();
return;
}else{
y = Double.parseDouble(editText2.getText().toString());
}
}
catch(Exception e){
Toast.makeText(getApplicationContext(),
"Insert All The Fields", Toast.LENGTH_LONG).show();
}
try{
String c = editText3.getText().toString();
if(c == null || c.equals(""))
{
z = 0;
Toast.makeText(getApplicationContext(),
"Insert All The Fields", Toast.LENGTH_LONG).show();
return;
}else{
z = Double.parseDouble(editText3.getText().toString());
}
}
catch(Exception e){
Toast.makeText(getApplicationContext(),
"Insert All The Fields", Toast.LENGTH_LONG).show();
}
i = x +y+z;
Intent intent = new Intent(getApplicationContext(),SecondActivity.class);
intent.putExtra("key", +i);
startActivity(intent);
答案 1 :(得分:0)
在使用此行之前
Double x = Double.parseDouble(editText1.getText().toString());
您需要检查该字符串是否代表数字。
if(editText1.getText().toString() == null || editText1.getText().toString().equals("")){
Toast.makeText(getApplicationContext(),
"Insert All The Fields", Toast.LENGTH_LONG).show();
return;
}else{
Double x = Double.parseDouble(editText1.getText().toString());
}
您需要添加更多测试以验证除了数字
之外的其他内容