
得到无效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")@OverrIDeprotected 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无效的双:“”logcat中的错误
有人请帮忙
解决方法:
原因是,“”不是有效的双倍.您需要先测试String或捕获此类异常
double w;try { w = new Double(input3.getText().toString());} catch (NumberFormatException e) { w = 0; // your default value} 总结 以上是内存溢出为你收集整理的java.lang.numberformatexception:无效的double:“”全部内容,希望文章能够帮你解决java.lang.numberformatexception:无效的double:“”所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
微信扫一扫
支付宝扫一扫
评论列表(0条)