0

我有一个带有 3 个 EditTexts 和 Button 的活动。用户可以填写两个编辑文本(edittext 1 和 2 或 1 和 3)。始终有一个编辑文本(第二个或第三个)必须为空。单击按钮后显示正确的结果。我不知道如何更改我的代码。如果我有空的edittext,则应用程序意外停止。如果我将值 0 插入不使用 edittext 一切都可以。有没有人有任何想法?如何解决这个问题呢?非常感谢您的帮助。这是我的主要活动代码:

   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)
4

1 回答 1

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());
于 2012-07-28T07:21:04.603 回答