点击按钮时的应用程序崩溃
我正在制作一个应用程序,其中有三个editText和一个按钮。我正在做的是在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)
问题是你是解析一些空的文本。 因此,在解析导致异常的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);
'x.equals(“”)'是这样吗? – 2015-01-04 10:48:09
当我添加x = 0时出现错误,“无法从int转换为双倍” – Karma5 2015-01-04 10:57:12
@shayanpourvatan - 再次修改整个代码。感谢通知.. :) – 2015-01-04 10:57:56
您使用此行
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());
}
你需要添加更多的测试来验证是没有别的但一些
把你的logcat输出 – 2015-01-04 10:38:17
'x.equals(“”)'? 'x'是双倍的,它如何可以是''“'?你必须为null – 2015-01-04 10:40:52