繁体   English   中英

Android初学者:空编辑文本

[英]Android Beginner: Empty Edit Text

我正在创建一个应用程序,将一个数字除以另一个数字。 我曾尝试添加代码以在有空编辑文本时显示对话框,但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)

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

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

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

即使你的EditText是空的,你仍在解析那些EditText的空Strings ,因为你的代码在你显示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();
        }

尝试这个...

DecimalFormat localDecimalFormat = new DecimalFormat("0.000");

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

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

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等(双格式)不要使用逗号...

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM