Android计算器领先期“。”导致崩溃

时间:2012-02-14 05:59:54

标签: android string double decimal calculator

嗨,我正在制作一个简单的欧姆定律计算器。我将它设置为自动计算值并在editText框改变时更新总数。当其中一个盒子是空的时,我解决了崩溃的问题。

我现在遇到的问题是每当我以小数点开头时“。”我的应用程序崩溃了。 我该怎么做才能解决这个问题。

这是我的代码。

package com.TWApps.SimpleOhmsLawCalc;

import android.app.Activity;
import android.os.Bundle;
import android.text.Editable;
import android.widget.*;
import android.text.TextWatcher;


public class voltageActivity extends Activity {
private EditText editTextCurrent;
private EditText editTextResistance;
private EditText editTextVoltage;

private double V = 0;
private double I = 0;
private double Re = 0;



public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.voltagelayout);

initcontrols();
}



public void initcontrols()
{
editTextVoltage = (EditText) findViewById(R.id.editTextVoltage);
editTextCurrent = (EditText) findViewById(R.id.editTextCurrent);
editTextResistance = (EditText) findViewById(R.id.editTextResistance);



editTextResistance.addTextChangedListener(new TextWatcher() {   
    public void beforeTextChanged(CharSequence s, int start, int count, int after) {
    }

    public void onTextChanged(CharSequence s, int start, int before, int count) {

    }

    public void afterTextChanged(Editable s) {
        int ReLen = editTextResistance.length();
        if (ReLen > 0){



        calculate();

        }
    }
});

}


private void calculate()
{

I = Double.parseDouble(editTextCurrent.getText().toString());
Re = Double.parseDouble(editTextResistance.getText().toString());


V = (I * Re);

editTextVoltage.setText(Double.toString(V));


}
}

logcat的:

02-13 22:20:41.412: I/Process(616): Sending signal. PID: 616 SIG: 9
02-13 22:20:47.652: W/KeyCharacterMap(625): No keyboard for id 0
02-13 22:20:47.652: W/KeyCharacterMap(625): Using default keymap: /system/usr/keychars/qwerty.kcm.bin
02-13 22:20:49.972: D/AndroidRuntime(625): Shutting down VM
02-13 22:20:49.972: W/dalvikvm(625): threadid=1: thread exiting with uncaught exception (group=0x40015560)
02-13 22:20:50.002: E/AndroidRuntime(625): FATAL EXCEPTION: main
02-13 22:20:50.002: E/AndroidRuntime(625): java.lang.NumberFormatException
02-13 22:20:50.002: E/AndroidRuntime(625):  at org.apache.harmony.luni.util.FloatingPointParser.initialParse(FloatingPointParser.java:160)
02-13 22:20:50.002: E/AndroidRuntime(625):  at org.apache.harmony.luni.util.FloatingPointParser.parseDouble(FloatingPointParser.java:281)
02-13 22:20:50.002: E/AndroidRuntime(625):  at java.lang.Double.parseDouble(Double.java:318)
02-13 22:20:50.002: E/AndroidRuntime(625):  at com.TWApps.SimpleOhmsLawCalc.voltageActivity.calculate(voltageActivity.java:70)
02-13 22:20:50.002: E/AndroidRuntime(625):  at com.TWApps.SimpleOhmsLawCalc.voltageActivity.access$1(voltageActivity.java:63)
02-13 22:20:50.002: E/AndroidRuntime(625):  at com.TWApps.SimpleOhmsLawCalc.voltageActivity$1.onTextChanged(voltageActivity.java:48)
02-13 22:20:50.002: E/AndroidRuntime(625):  at android.widget.TextView.sendOnTextChanged(TextView.java:6295)
02-13 22:20:50.002: E/AndroidRuntime(625):  at android.widget.TextView.handleTextChanged(TextView.java:6336)
02-13 22:20:50.002: E/AndroidRuntime(625):  at android.widget.TextView$ChangeWatcher.onTextChanged(TextView.java:6485)
02-13 22:20:50.002: E/AndroidRuntime(625):  at android.text.SpannableStringBuilder.sendTextChange(SpannableStringBuilder.java:889)
02-13 22:20:50.002: E/AndroidRuntime(625):  at android.text.SpannableStringBuilder.change(SpannableStringBuilder.java:352)
02-13 22:20:50.002: E/AndroidRuntime(625):  at android.text.SpannableStringBuilder.change(SpannableStringBuilder.java:269)
02-13 22:20:50.002: E/AndroidRuntime(625):  at android.text.SpannableStringBuilder.replace(SpannableStringBuilder.java:432)
02-13 22:20:50.002: E/AndroidRuntime(625):  at android.text.SpannableStringBuilder.replace(SpannableStringBuilder.java:409)
02-13 22:20:50.002: E/AndroidRuntime(625):  at android.text.SpannableStringBuilder.replace(SpannableStringBuilder.java:28)
02-13 22:20:50.002: E/AndroidRuntime(625):  at android.text.method.NumberKeyListener.onKeyDown(NumberKeyListener.java:117)
02-13 22:20:50.002: E/AndroidRuntime(625):  at android.widget.TextView.doKeyDown(TextView.java:4433)
02-13 22:20:50.002: E/AndroidRuntime(625):  at android.widget.TextView.onKeyDown(TextView.java:4267)
02-13 22:20:50.002: E/AndroidRuntime(625):  at android.view.KeyEvent.dispatch(KeyEvent.java:1256)
02-13 22:20:50.002: E/AndroidRuntime(625):  at android.view.View.dispatchKeyEvent(View.java:3855)
02-13 22:20:50.002: E/AndroidRuntime(625):  at android.view.ViewGroup.dispatchKeyEvent(ViewGroup.java:789)
02-13 22:20:50.002: E/AndroidRuntime(625):  at android.view.ViewGroup.dispatchKeyEvent(ViewGroup.java:789)
02-13 22:20:50.002: E/AndroidRuntime(625):  at android.view.ViewGroup.dispatchKeyEvent(ViewGroup.java:789)
02-13 22:20:50.002: E/AndroidRuntime(625):  at android.view.ViewGroup.dispatchKeyEvent(ViewGroup.java:789)
02-13 22:20:50.002: E/AndroidRuntime(625):  at com.android.internal.policy.impl.PhoneWindow$DecorView.superDispatchKeyEvent(PhoneWindow.java:1687)
02-13 22:20:50.002: E/AndroidRuntime(625):  at com.android.internal.policy.impl.PhoneWindow.superDispatchKeyEvent(PhoneWindow.java:1120)
02-13 22:20:50.002: E/AndroidRuntime(625):  at android.app.Activity.dispatchKeyEvent(Activity.java:2073)
02-13 22:20:50.002: E/AndroidRuntime(625):  at com.android.internal.policy.impl.PhoneWindow$DecorView.dispatchKeyEvent(PhoneWindow.java:1663)
02-13 22:20:50.002: E/AndroidRuntime(625):  at android.view.ViewRoot.deliverKeyEventToViewHierarchy(ViewRoot.java:2560)
02-13 22:20:50.002: E/AndroidRuntime(625):  at android.view.ViewRoot.handleFinishedEvent(ViewRoot.java:2535)
02-13 22:20:50.002: E/AndroidRuntime(625):  at android.view.ViewRoot.handleMessage(ViewRoot.java:1867)
02-13 22:20:50.002: E/AndroidRuntime(625):  at android.os.Handler.dispatchMessage(Handler.java:99)
02-13 22:20:50.002: E/AndroidRuntime(625):  at android.os.Looper.loop(Looper.java:123)
02-13 22:20:50.002: E/AndroidRuntime(625):  at android.app.ActivityThread.main(ActivityThread.java:3683)
02-13 22:20:50.002: E/AndroidRuntime(625):  at java.lang.reflect.Method.invokeNative(Native Method)
02-13 22:20:50.002: E/AndroidRuntime(625):  at java.lang.reflect.Method.invoke(Method.java:507)
02-13 22:20:50.002: E/AndroidRuntime(625):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839)
02-13 22:20:50.002: E/AndroidRuntime(625):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597)
02-13 22:20:50.002: E/AndroidRuntime(625):  at dalvik.system.NativeStart.main(Native Method)

2 个答案:

答案 0 :(得分:0)

现在你有了日志,很明显,对吗?你在计算中得到一个未处理的NumberFormatException。因此,处理它 - 可能是通过抱怨TextView,或者在Toast中抱怨,或者首先处理这个特定情况(前导十进制)并预先设置为零。

答案 1 :(得分:0)

使用try catch块包围解析方法,因为它生成空指针异常。

try{
 V = Double.parseDouble(editTextVoltage.getText().toString());
   }catch(Exception e ){
    V=0;
   }

同样遵循所有