Android初学者:空编辑文本

时间:2012-05-27 15:45:37

标签: java android eclipse android-layout android-intent

我正在创建一个应用程序,它将一个数字除以另一个数字。我曾尝试添加代码以在有空编辑文本时显示对话框,但app forcecloses。这是我的代码。

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 个答案:

答案 0 :(得分:2)

尝试记录str1&的值致电str2之前Double.parseDouble(String);您可以使用Log.d(String, String)来执行此操作。

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

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

答案 1 :(得分: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();
        }

答案 2 :(得分:0)

试试这个......

DecimalFormat localDecimalFormat = new DecimalFormat("0.000");

只是提一下尝试检查字符串str1和str2,并保持谨慎.. Double不要给出除零错误

答案 3 :(得分: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等(双格式)不要使用逗号...