【问题标题】:java.lang.numberformatexception: invalid double: " "java.lang.numberformatexception:无效双:“”
【发布时间】:2013-09-11 10:05:33
【问题描述】:

收到无效 Double 的错误 java.lang.numberformatexception 无效双精度:“” 这是什么原因

活动 1

package com.example.solarcalculator;

        import android.os.Bundle;
        import android.widget.Button;
        import android.annotation.SuppressLint;
        import android.app.Activity;
        import android.view.Menu;
        import android.view.View;
        import android.view.View.OnClickListener;
        import android.widget.EditText;
        import android.app.AlertDialog;
        import android.content.Intent;

        @SuppressLint("UseValueOf")
        public class MainActivity extends Activity {
            private EditText input1;
            private EditText input2;
            private EditText input3;
            private EditText input4;
            private EditText input5;
            private MainActivity mContext;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        mContext = this;
        setContentView(R.layout.activity_main);

        input5 = (EditText) findViewById(R.id.input5);
        Button button1 = (Button) findViewById(R.id.button1);
        input4 = (EditText) findViewById(R.id.input4);
        input1 = (EditText) findViewById(R.id.input1);
        input2 = (EditText) findViewById(R.id.input2);
        input3 = (EditText) findViewById(R.id.input3);
        button1.setOnClickListener(new OnClickListener() {

            @SuppressWarnings("unused")
            private AlertDialog show;

            @SuppressLint("UseValueOf")
            @Override
            public void onClick(View arg0) {
                if (  (input4.getText().toString() == " ") 
                        || (input4.getText().length() ==0) ||
                                                (input5.getText().length() == 0)                         
                        || (input5.getText().toString() == " ")){
                show = new      AlertDialog.Builder(mContext).setTitle("Error")
                            .setMessage("Some inputs are empty")
                            .setPositiveButton("OK", null).show();
                }
    else if ((input1.getText().length() != 0) &&
        (input3.getText().length() ==0) && (input2.getText().length() ==    0)){
                     double w = new Double(input3.getText().toString());
                     double t = new Double(input4.getText().toString());
                     double x = new Double(input5.getText().toString());
                     float e = 7;
                     double num = 1000*x;
                     double den = w*t*e;
                     double payback = num/den;
                     double money = w*t*e/1000;
         Intent intent = new Intent(MainActivity.this, Power.class);
                     intent.putExtra("payback", payback);
                     intent.putExtra("money", money);
                     startActivity(intent);

                }
    else if ((input1.getText().length() == 0) &&   (input3.getText().length() != 0) &&
                                (input2.getText().length() != 0)){
                     double t = new    
                                     Double(input4.getText().toString());
                     double x = new Double(input5.getText().toString());
                     double v = new Double(input2.getText().toString());
                     double i = new Double(input3.getText().toString());
                     float e = 7;
                     double num = 1000*x;
                     double den = v*i*t*e;
                     double payback = num/den;
                     double money = v*i*t*e/1000;
             Intent intent = new Intent(MainActivity.this, Power.class);
                     intent.putExtra("payback", payback);
                     intent.putExtra("money", money);
                     startActivity(intent);

                }
                else {
                    double t = new Double(input4.getText().toString());
                     double x = new Double(input5.getText().toString());
                     double v = new Double(input2.getText().toString());
                     double i = new Double(input3.getText().toString());
                     float e = 7;
                     double num = 1000*x;
                     double den = v*i*t*e;
                     double payback = num/den;
                     double money = v*i*t*e/1000;
               Intent intent = new Intent(MainActivity.this, Power.class);
                     intent.putExtra("payback", payback);
                     intent.putExtra("money", money);
                     startActivity(intent);

                }
            }
        });

    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.main, menu);
        return true;
    }

       }

活动2

package com.example.solarcalculator;

    import android.annotation.SuppressLint;
    import android.app.Activity;
    import android.os.Bundle;
    import android.widget.TextView;
   @SuppressLint("NewApi")
     public class Power extends Activity {

private double money;
private double payback;

@SuppressLint("NewApi")
@Override
protected void onCreate(Bundle savedInstanceState) {
    // TODO Auto-generated method stub
    super.onCreate(savedInstanceState);
    setContentView(R.layout.power);
    payback = getIntent().getDoubleExtra("payback",0);
    money = getIntent().getDoubleExtra("money", 0);
    TextView pay = (TextView) findViewById(R.id.textView2);
    String payback1 = Double.toString(payback);
    pay.setText(payback1);
    TextView mon = (TextView) findViewById(R.id.textView4);
    String money1 = Double.toString(money);
    mon.setText(money1);

     }
    }

我收到 java.lang.numberformatexception invalid double: "" logcat 错误 有人请帮忙

【问题讨论】:

  • "" 没有双倍!在转换为双精度之前,您必须测试空字符串。
  • 您在哪一行收到此类错误?
  • 我在第 52 行遇到了这个错误

标签: java android


【解决方案1】:

原因是,"" 不是有效的双精度。您需要在之前测试字符串或捕获此类异常

double w;

try {
    w = new Double(input3.getText().toString());
} catch (NumberFormatException e) {
    w = 0; // your default value
}

【讨论】:

  • 代替:double amount = Double.parseDouble(amt.toString()); ,我不得不这样做: double amount = new Double(amt.getText().toString());,检索用户输入时。谢谢。
【解决方案2】:

double 的值取决于设备的语言。例如,对于法语设备,数字 0.179927 变为 0,179927,这将始终抛出 @ 987654321@ 解析为double 时因为逗号。

您需要将分隔符从 comma 更改为 point。 您可以通过设置区域设置或使用DecimalFormatSymbols 来更改分隔符。

如果您希望分组分隔符是一个点,您可以使用欧洲语言环境:

NumberFormat nf = NumberFormat.getNumberInstance(Locale.GERMAN);
DecimalFormat df = (DecimalFormat)nf;

或者,您可以使用DecimalFormatSymbols 类来更改出现在format 方法生成的格式化数字中的符号。这些符号包括小数分隔符、分组分隔符、减号和百分号等:

DecimalFormatSymbols otherSymbols = new DecimalFormatSymbols(currentLocale);
otherSymbols.setDecimalSeparator(',');
otherSymbols.setGroupingSeparator('.'); 
DecimalFormat df = new DecimalFormat(formatString, otherSymbols);

【讨论】:

  • 这个答案在这里没有帮助,因为这里的这个异常是由一个空字符串引起的。
  • 也许这个答案并不能解决这里提出的问题,但是对于搜索相同异常的人来说,需要知道所有可能的问题。就我而言,这是相同异常和代码的有效答案。
【解决方案3】:

您应该执行以下操作。把它放在一个 try catch 块中

   double w = new Double(input3.getText().toString());

【讨论】:

    【解决方案4】:

    最好同时测试.equals("")null

    假设您在 EditText 上有一个 afterTextChanged 侦听器。当你按下并清除输入的文本时,它会传递!= null,但仍然有""

    这对我有用:

        double myDouble;
    
        String myString = ((EditText) findViewById(R.id.editText1)).getText().toString();
    
        if (myString != null && !myString.equals("")) {
            myDouble = Double.valueOf(myString);
        } else {
            myDouble = 0;
        }
    

    【讨论】:

      【解决方案5】:

      基本上,你需要测试你要转成双精度字符串是否为空

      如果它是empty,那么你可以初始化它用一些值然后继续。

      例如:

      if(myString.isEmpty()){
      myString = ""+0.0;
      }
      double answer = Double.parseDouble(myString);
      

      【讨论】:

        【解决方案6】:
         double latitude;
                    double longitude;
                    try {
                        latitude = Double.parseDouble(mApoAppointmentBeanList.get(position).getLatitude());
                        longitude = Double.parseDouble(mApoAppointmentBeanList.get(position).getLongitude());
        
                        String uri = String.format(Locale.ENGLISH, "http://maps.google.com/maps?q=loc:%f,%f", latitude, longitude);
                        Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse(uri));
                        startActivity(intent);
        
                    } catch (NumberFormatException e) {
                        // your default value
                        Toast.makeText(AppointmentsActivity.this, "Invalid location", Toast.LENGTH_LONG).show();
                    }
        

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 2016-10-05
          • 2015-12-14
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2017-04-21
          • 1970-01-01
          相关资源
          最近更新 更多