我有一个包含3个EditTexts和Button的活动。用户可以填写两个edittexts(edittext 1和2或1和3)。始终只有一个编辑文本(第2或第3个)为空。点击按钮后显示正确的结果。我不知道如何更改我的代码。如果我有空的edittext,应用程序已意外停止。如果我将值0插入到不使用edittext,则Everythings就可以了。有没有人有任何想法?如何解决这个问题呢?非常感谢您的帮助。 这是我的主要活动代码:
public class Glowna extends Activity implements OnClickListener{
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
View button1Button = this.findViewById(R.id.oblicz);
button1Button.setOnClickListener(this);
}
public void onClick(View v){
EditText numa = (EditText)findViewById(R.id.a);
EditText numb = (EditText)findViewById(R.id.b);
EditText numc = (EditText)findViewById(R.id.c);
float eq1 = 0;
float eq2 = 0;
float eq3 = 0;
float eq4 = 0;
float num1 = Float.parseFloat(numa.getText().toString());
float num2 = Float.parseFloat(numb.getText().toString());
float num3 = Float.parseFloat(numc.getText().toString());
if (num1>0 & num2>0){
try{
switch (v.getId()) {
case R.id.oblicz:
eq1 = 4*num1;
eq2 = num1*num2;
break; }
String wynik1 = Float.toString(eq1);
String wynik2 = Float.toString(eq2);
Intent i = new Intent(this, Wynik.class);
i.putExtra("a",wynik1);
i.putExtra("b",wynik2);
startActivity(i); }
catch (NumberFormatException e) {Intent o = new Intent(this, Komunikat.class);
startActivity(o); return; }}
else if (num1>0&num3>0){
try{
switch (v.getId()) {
case R.id.oblicz:
eq4 = 4*num1;
eq3 = (num3/2)*num3;
break; }
String wynik4 = Float.toString(eq4);
String wynik3 = Float.toString(eq3);
Intent j = new Intent(this, Wynik1.class);
j.putExtra("d",wynik4);
j.putExtra("c",wynik3);
startActivity(j); }
catch (NumberFormatException e) {Intent o = new Intent(this, Komunikat.class);
startActivity(o);
return;}}
else {Intent o = new Intent(this, Komunikat.class);
startActivity(o);}
}}
和错误:
E/AndroidRuntime(249): Uncaught handler: thread main exiting due to uncaught exception
E/AndroidRuntime(249): java.lang.NumberFormatException:
E/AndroidRuntime(249): at org.apache.harmony.luni.util.FloatingPointParser.parseFloat(FloatingPointParser.java:296)
E/AndroidRuntime(249): at java.lang.Float.parseFloat(Float.java:327)
E/AndroidRuntime(249): at arek.gemetrytesty.glowna.Glowna.onClick(Glowna.java:38)
E/AndroidRuntime(249): at android.view.View.performClick(View.java:2344)
E/AndroidRuntime(249): at android.view.View.onTouchEvent(View.java:4133)
E/AndroidRuntime(249): at android.widget.TextView.onTouchEvent(TextView.java:6510)
E/AndroidRuntime(249): at android.view.View.dispatchTouchEvent(View.java:3672)
E/AndroidRuntime(249): at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:882)
E/AndroidRuntime(249): at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:882)
E/AndroidRuntime(249): at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:882)
E/AndroidRuntime(249): at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:882)
E/AndroidRuntime(249): at com.android.internal.policy.impl.PhoneWindow$DecorView.superDispatchTouchEvent(PhoneWindow.java:1712)
E/AndroidRuntime(249): at com.android.internal.policy.impl.PhoneWindow.superDispatchTouchEvent(PhoneWindow.java:1202)
E/AndroidRuntime(249): at android.app.Activity.dispatchTouchEvent(Activity.java:1987)
E/AndroidRuntime(249): at com.android.internal.policy.impl.PhoneWindow$DecorView.dispatchTouchEvent(PhoneWindow.java:1696)
E/AndroidRuntime(249): at android.view.ViewRoot.handleMessage(ViewRoot.java:1658)
E/AndroidRuntime(249): at android.os.Handler.dispatchMessage(Handler.java:99)
E/AndroidRuntime(249): at android.os.Looper.loop(Looper.java:123)
E/AndroidRuntime(249): at android.app.ActivityThread.main(ActivityThread.java:4203)
E/AndroidRuntime(249): at java.lang.reflect.Method.invokeNative(Native Method)
E/AndroidRuntime(249): at java.lang.reflect.Method.invoke(Method.java:521)
E/AndroidRuntime(249): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:791)
E/AndroidRuntime(249): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:549)
E/AndroidRuntime(249): at dalvik.system.NativeStart.main(Native Method)
答案 0 :(得分:0)
要抓住java.lang.NumberFormatException
,请尝试以下方法:
float num1 = Float.parseFloat(numa.getText().toString());
float num2;
float num3;
try {
num2 = Float.parseFloat(numb.getText().toString());
} catch (Exception e) {
num2 = 0;
}
try {
num3 = Float.parseFloat(numc.getText().toString());
} catch (Exception e) {
num3 = 0;
}
取代此代码段:
float num1 = Float.parseFloat(numa.getText().toString());
float num2 = Float.parseFloat(numb.getText().toString());
float num3 = Float.parseFloat(numc.getText().toString());