我是android新手并试图创建一个简单的程序,将三个输入相加,然后将输出打印到EditText。每当我按下按钮时,我的代码中的第63行(我在下面发布的最后一行)发生了崩溃...我认为这可能是由于一个空对象或类似的东西。我99%确定我的所有变量和其他所有变量都是正确的。无论如何,这是我的onClick方法的代码,如果有人能够弄清楚导致崩溃的原因,我会非常感激。
Button solve = (Button) findViewById(R.id.solve);
solve.setOnClickListener(new OnClickListener() {
public void onClick(View arg0) {
if ((a.getText().length() == 0)
|| (" ".equals(a.getText().toString()))
|| (" ".equals(b.getText().toString()))
|| (" ".equals(c.getText().toString()))
|| (b.getText().length() == 0)
|| (c.getText().length() == 0)) {
Toast.makeText(getApplicationContext(), "Some of your inputs are empty.", Toast.LENGTH_SHORT).show();
} else {
double sol = new Double(a.getText().toString())
+ new Double(b.getText().toString())
+ new Double(c.getText().toString());
res1.setText(Double.toString(sol)); //line that is causing the crash
答案 0 :(得分:0)
您可以尝试使用Double.Parse
...
和
res1.setText(String.valueOf(sol));
答案 1 :(得分:0)
您正在以错误的方式将String值转换为Double,请参阅以下两种方式执行此操作,
使用Double.parseDouble()
double sol = new Double.parseDouble((a.getText().toString().trim()); // also don't forget to use trim() method to remove white spaces
使用新的Double(值).doubleValue()
double sol = new Double(a.getText().toString().trim()).doubleValue();
现在将此sol
变量的值设置为EditText,请尝试以下方式
res1.setText((String)sol));
res1.setText(String.valueOf(sol));
res1.setText( sol + "" );