使用Android进行计算

时间:2013-05-30 11:04:45

标签: android

我有一个用户输入列表(EditTexts)并使用它们我想计算要在TextView TotalNum中显示的总数。

Logcat显示Expanse.java第70行出了问题,

TotalNum.setText("£" + exTotal);

我已经尝试取出exTotal双重数字,我认为这可能导致错误,但它仍然说'不幸应用程序名称已停止工作'当我在输入所有后按下计算按钮每个EditText的数字。

我做错了什么?

public class Expense extends Activity {

    double exTotal, exRent, exElect, exHeat, exNet, exMobile, exOther;
    TextView Total, TotalNum, Rent, Elect, Heat, Net, Mobile, Other;
    EditText eRent, eElect, eHeat, eNet, eMobile, eOther;
    String eRentS, eElectS, eHeatS, eNetS, eMobileS, eOtherS;
    Button cal;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.expense);

        Rent = (TextView) findViewById(R.id.tvRent);
        Elect = (TextView) findViewById(R.id.tvElectricity);
        Heat = (TextView) findViewById(R.id.tvHeating);
        Net = (TextView) findViewById(R.id.tvInternet);
        Mobile = (TextView) findViewById(R.id.tvMobile);
        Other = (TextView) findViewById(R.id.tvOther);
        Total = (TextView) findViewById(R.id.tvTotalExpense);
        TotalNum = (TextView) findViewById(R.id.tvExpenseNum);

        eRent = (EditText) findViewById(R.id.etRent);
        eElect = (EditText) findViewById(R.id.etElectricity);
        eHeat = (EditText) findViewById(R.id.etHeating);
        eNet = (EditText) findViewById(R.id.etInternet);
        eMobile = (EditText) findViewById(R.id.etMobile);
        eOther = (EditText) findViewById(R.id.etOther);

        cal = (Button) findViewById(R.id.bCal);
        cal.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                // TODO Auto-generated method stub              
                eRentS = eRent.getText().toString();
                eElectS = eElect.getText().toString();
                eHeatS = eHeat.getText().toString();
                eNetS = eNet.getText().toString();
                eMobileS = eMobile.getText().toString();
                eOtherS = eOther.getText().toString();

                exRent = Double.parseDouble(eRentS);
                exElect = Double.parseDouble(eElectS);
                exHeat = Double.parseDouble(eHeatS);
                exNet = Double.parseDouble(eNetS);
                exMobile = Double.parseDouble(eMobileS);
                exOther = Double.parseDouble(eOtherS);

                exTotal = exRent + exElect + exHeat + exNet + exMobile + exOther;
                TotalNum.setText("£" + exTotal);
            }
        });
    }
}

logcat的

05-30 11:45:44.694: E/AndroidRuntime(775): FATAL EXCEPTION: main
05-30 11:45:44.694: E/AndroidRuntime(775): java.lang.NullPointerException
05-30 11:45:44.694: E/AndroidRuntime(775):  at com.studentbudget.Expense$1.onClick(Expense.java:70)
05-30 11:45:44.694: E/AndroidRuntime(775):  at android.view.View.performClick(View.java:4204)
05-30 11:45:44.694: E/AndroidRuntime(775):  at android.view.View$PerformClick.run(View.java:17355)
05-30 11:45:44.694: E/AndroidRuntime(775):  at android.os.Handler.handleCallback(Handler.java:725)
05-30 11:45:44.694: E/AndroidRuntime(775):  at android.os.Handler.dispatchMessage(Handler.java:92)
05-30 11:45:44.694: E/AndroidRuntime(775):  at android.os.Looper.loop(Looper.java:137)
05-30 11:45:44.694: E/AndroidRuntime(775):  at android.app.ActivityThread.main(ActivityThread.java:5041)
05-30 11:45:44.694: E/AndroidRuntime(775):  at java.lang.reflect.Method.invokeNative(Native Method)
05-30 11:45:44.694: E/AndroidRuntime(775):  at java.lang.reflect.Method.invoke(Method.java:511)
05-30 11:45:44.694: E/AndroidRuntime(775):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
05-30 11:45:44.694: E/AndroidRuntime(775):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)
05-30 11:45:44.694: E/AndroidRuntime(775):  at dalvik.system.NativeStart.main(Native Method)

2 个答案:

答案 0 :(得分:0)

尝试使用unicode作为英镑符号\ u00A3而不是实际的£符号,java可能在识别它时遇到问题。

发布另一个错误的logcat,它应该给你发生崩溃的类文件的行号。

虽然看起来它可能会是一个NumberFormatException,因为它需要一个双精度格式的字符串,并且可能会得到其他东西

答案 1 :(得分:0)

这可能是由于您在编辑文本中输入的某些空间造成的。 你可以避免空间。

eRentS = eRent.getText().toString();
            eElectS = eElect.getText().toString().trim();
            eHeatS = eHeat.getText().toString().trim(;
            eNetS = eNet.getText().toString().trim(;
            eMobileS = eMobile.getText().toString().trim(;
            eOtherS = eOther.getText().toString().trim(;