1

我正在创建一个将一个数字除以另一个数字的应用程序。当编辑文本为空时,我尝试添加代码以显示对话框,但应用程序强制关闭。这是我的代码。

public class KdCalculator extends Activity implements OnClickListener{

    EditText kills;
    EditText deaths;
    TextView text;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.kd_activity);

        Button calculate = (Button)findViewById(R.id.ButtonCalculate);
            calculate.setOnClickListener(this);

        kills = (EditText)findViewById(R.id.EditTextKills);
        deaths = (EditText)findViewById(R.id.EditTextDeaths);

    }

    public void onClick(View paramView)
      { 
          boolean invalid = false;

          String str1 = kills.getText().toString();
          String str2 = deaths.getText().toString();

          if (str1.equals("") || str2.equals(""))
          {
              new AlertDialog.Builder(this)
                .setTitle("No Number")
                .setMessage("Please enter a number for both kills and deaths!")
                .setNeutralButton("Ok", null)
                .show();
              invalid = true;
          }

          double d1 = Double.parseDouble(str2);
          double d2 = Double.parseDouble(str1);

          if ((d1 == 0.0D) || (d2 == 0.0D))
          {
              new AlertDialog.Builder(this)
            .setTitle("Invalid Number")
            .setMessage("Please enter a number different than 0 for both kills and deaths!")
            .setNeutralButton("Ok", null)
            .show();
              invalid = true;
          }
          double d3 = d2 / d1;
          DecimalFormat localDecimalFormat = new DecimalFormat("#.###");
          String str3 = "Your K/D Ratio is : " + localDecimalFormat.format(d3);

          if(!invalid){
          new AlertDialog.Builder(this)
            .setTitle("K/D Ratio")
            .setMessage(str3)
            .setNeutralButton("Ok", null)
            .show();
          }
          invalid = false;
        }


}

logcat 错误是:

05-27 11:43:53.327: W/dalvikvm(6090): threadid=1: thread exiting with uncaught exception (group=0x40020b80)
05-27 11:43:53.357: E/AndroidRuntime(6090): FATAL EXCEPTION: main
05-27 11:43:53.357: E/AndroidRuntime(6090): java.lang.NumberFormatException: 
05-27 11:43:53.357: E/AndroidRuntime(6090):     at org.apache.harmony.luni.util.FloatingPointParser.parseDouble(FloatingPointParser.java:267)
05-27 11:43:53.357: E/AndroidRuntime(6090):     at java.lang.Double.parseDouble(Double.java:287)
05-27 11:43:53.357: E/AndroidRuntime(6090):     at com.blackops2.KdCalculator.onClick(KdCalculator.java:53)
05-27 11:43:53.357: E/AndroidRuntime(6090):     at android.view.View.performClick(View.java:2411)
05-27 11:43:53.357: E/AndroidRuntime(6090):     at android.view.View$PerformClick.run(View.java:8819)
05-27 11:43:53.357: E/AndroidRuntime(6090):     at android.os.Handler.handleCallback(Handler.java:587)
05-27 11:43:53.357: E/AndroidRuntime(6090):     at android.os.Handler.dispatchMessage(Handler.java:92)
05-27 11:43:53.357: E/AndroidRuntime(6090):     at android.os.Looper.loop(Looper.java:123)
05-27 11:43:53.357: E/AndroidRuntime(6090):     at android.app.ActivityThread.main(ActivityThread.java:4627)
05-27 11:43:53.357: E/AndroidRuntime(6090):     at java.lang.reflect.Method.invokeNative(Native Method)
05-27 11:43:53.357: E/AndroidRuntime(6090):     at java.lang.reflect.Method.invoke(Method.java:521)
05-27 11:43:53.357: E/AndroidRuntime(6090):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868)
05-27 11:43:53.357: E/AndroidRuntime(6090):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626)
05-27 11:43:53.357: E/AndroidRuntime(6090):     at dalvik.system.NativeStart.main(Native Method)
4

4 回答 4

2

在调用之前尝试记录str1&的值;你可以用它来做到这一点。str2Double.parseDouble(String)Log.d(String, String)

根据堆栈跟踪,其中一个值不能转换为数字。

我通常会做的是将任何数字格式调用放入一个try...catch块中 - 这种类型的操作可能会出现许多错误。这可能是避免此代码进一步崩溃的最成功方法。

于 2012-05-27T15:53:45.250 回答
2

即使你EditText是空的,你仍然会Strings从那些中解析空,EditText因为你的代码在你显示之后继续执行AlertDialog,而不是你应该从onClick方法返回以停止任何计算(当它们EditText为空或未填充时):

public void onClick(View paramView) { 
          String str1 = kills.getText().toString();
          String str2 = deaths.getText().toString();

          if (str1.equals("") || str2.equals("")) {
              new AlertDialog.Builder(this)
                .setTitle("No Number")
                .setMessage("Please enter a number for both kills and deaths!")
                .setNeutralButton("Ok", null)
                .show();
                return;   
          }
          double d1 = Double.parseDouble(str2);
          double d2 = Double.parseDouble(str1);
          if ((d1 == 0.0D) || (d2 == 0.0D)) {
              new AlertDialog.Builder(this)
            .setTitle("Invalid Number")
            .setMessage("Please enter a number different than 0 for both kills and deaths!")
            .setNeutralButton("Ok", null)
            .show();
            return;
          }
          double d3 = d2 / d1;
          DecimalFormat localDecimalFormat = new DecimalFormat("#.###");
          String str3 = "Your K/D Ratio is : " + localDecimalFormat.format(d3);

          new AlertDialog.Builder(this)
            .setTitle("K/D Ratio")
            .setMessage(str3)
            .setNeutralButton("Ok", null)
            .show();
        }
于 2012-05-27T15:56:13.070 回答
0

尝试这个...

DecimalFormat localDecimalFormat = new DecimalFormat("0.000");

只是提到尝试检查字符串 str1 和 str2,并要小心.. Double 不要给除以零错误

于 2012-05-27T15:56:54.800 回答
0

我不确定,但试试这个...

public class KdCalculator extends Activity implements OnClickListener
{    
    double d1 = 0.0;
    double d2 = 0.0;    
    EditText kills;
    EditText deaths;
    TextView text;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.kd_activity);

        Button calculate = (Button)findViewById(R.id.ButtonCalculate);
            calculate.setOnClickListener(this);

        kills = (EditText)findViewById(R.id.EditTextKills);
        deaths = (EditText)findViewById(R.id.EditTextDeaths);

    }

    public void onClick(View paramView)
      { 
          boolean invalid = false;

          String str1 = kills.getText().toString();
          String str2 = deaths.getText().toString();

          if (str1.equals("") || str2.equals(""))
          {
              new AlertDialog.Builder(this)
                .setTitle("No Number")
                .setMessage("Please enter a number for both kills and deaths!")
                .setNeutralButton("Ok", null)
                .show();
              invalid = true;
          }

          d1 = Double.parseDouble(str2);
          d2 = Double.parseDouble(str1);

          if ((d1 == 0.0D) || (d2 == 0.0D))
          {
              new AlertDialog.Builder(this)
            .setTitle("Invalid Number")
            .setMessage("Please enter a number different than 0 for both kills and eaths!")
            .setNeutralButton("Ok", null)
            .show();
              invalid = true;
          }
          double d3 = d2 / d1;
          DecimalFormat localDecimalFormat = new DecimalFormat("#.###");
          String str3 = "Your K/D Ratio is : " + localDecimalFormat.format(d3);

          if(!invalid){
          new AlertDialog.Builder(this)
            .setTitle("K/D Ratio")
            .setMessage(str3)
            .setNeutralButton("Ok", null)
            .show();
          }
          invalid = false;
        }
}

并确保您在计算器中的输入有类似 2.0 或 2.2 等(双格式)的内容,不要使用逗号...

于 2012-05-27T16:00:52.390 回答