0

朋友我搜索了很多,但没有得到我的问题的答案。我正在尝试制作一个android计算器。一个简单的,实际上它只是我更大应用程序的一个模块......

在这首先我需要用户输入第一个和第二个 sunber 并选择他是否想添加、sub、mul、divide 或 get mod..然后有一个 textview 将显示结果。

这是我正在使用的代码:

Java代码:

package com.droidacid.apticalc;

import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;

public class numSys extends Activity{


EditText etnumber1;
EditText etnumber2;

Button badd;
Button bsub;
Button bmul;
Button bdiv;
Button bmod;

@Override
protected void onCreate(Bundle savedInstanceState) {
    // TODO Auto-generated method stub
    super.onCreate(savedInstanceState);
    setContentView(R.layout.numsys);

    etnumber1 = (EditText) findViewById(R.id.number1);
    etnumber2 = (EditText) findViewById(R.id.number2);



    String number1 = etnumber1.getText().toString();
    String number2 = etnumber2.getText().toString();

    final Double number1D = Double.parseDouble(number1);
    final Double number2D = Double.parseDouble(number2);

badd.setOnClickListener(new View.OnClickListener() {

    @Override
    public void onClick(View v) {
        // TODO Auto-generated method stub

    double resultD = number1D + number2D;
    String result = "result :"+resultD;
    TextView tvresult = (TextView) findViewById(R.id.tvresult);
    tvresult.setText(result);
    }
});

bsub.setOnClickListener(new View.OnClickListener() {

    @Override
    public void onClick(View v) {
        // TODO Auto-generated method stub

    double resultD = number1D - number2D;
    String result = "result :"+resultD;
    TextView tvresult = (TextView) findViewById(R.id.tvresult);
    tvresult.setText(result);
    }
});

bmul.setOnClickListener(new View.OnClickListener() {

@Override
public void onClick(View v) {
    // TODO Auto-generated method stub

double resultD = number1D * number2D;
String result = "result :"+resultD;
TextView tvresult = (TextView) findViewById(R.id.tvresult);
tvresult.setText(result);
}
});

bdiv.setOnClickListener(new View.OnClickListener() {

@Override
public void onClick(View v) {
    // TODO Auto-generated method stub

double resultD = number1D / number2D;
String result = "result :"+resultD;
TextView tvresult = (TextView) findViewById(R.id.tvresult);
tvresult.setText(result);
}
});

bmod.setOnClickListener(new View.OnClickListener() {

@Override
public void onClick(View v) {
    // TODO Auto-generated method stub

double resultD = number1D % number2D;
String result = "result :"+resultD;
TextView tvresult = (TextView) findViewById(R.id.tvresult);
tvresult.setText(result);
}
});



}

 }

这是 logcat 错误:

05-04 10:24:18.098: E/AndroidRuntime(1321): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.droidacid.apticalc/com.droidacid.apticalc.numSys}: java.lang.NumberFormatException: 

请不要将此帖子评为负面。自持续超过 24 小时以来,我只是在尝试学习并尝试解决此问题。尝试了 Stack Overflow 上发布的所有内容。

谢谢

4

2 回答 2

1

这可能是你的问题:

    String number1 = etnumber1.getText().toString();
    String number2 = etnumber2.getText().toString();

    final Double number1D = Double.parseDouble(number1);
    final Double number2D = Double.parseDouble(number2);

此时,您正在尝试将空 EditText 解析为 Double 值。要么,要么您尝试解析的 EditText 中有一些默认文本。

这(可能)会导致 NumberFormatException。

于 2013-05-08T13:36:46.533 回答
0
final Double number1D = Double.parseDouble(number1);
final Double number2D = Double.parseDouble(number2);

异常表示number1number2不能转换为double. 尝试trim使用 number1 和 number2 字符串

于 2013-05-08T13:35:25.877 回答