面对android中的计算器应用程序错误

时间:2013-05-08 13:32:35

标签: android

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

首先,我需要用户输入第1和第2个sunber并选择是否要添加,分,mul,除或得到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上发布的所有内容。

由于

2 个答案:

答案 0 :(得分: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。

答案 1 :(得分:0)

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

该例外表示number1number2无法转换为double。尝试trim number1和number2字符串