0

转换是其他类,我正在工作MainActivity

当我清除edittext然后输入“-”然后它会产生错误,但是当我输入一些值然后在edittext的第一个位置然后它不会产生错误,请帮助我附上我的错误日志。

Conversion conversion;


conversion = new Conversion();


@Override
        public void onTextChanged(CharSequence s, int start, int before,
                int count) {
if(edittext.length()!=0){
String Result =conversion.temp(spinner1.getSelectedItemPosition(),
        spinner2.getSelectedItemPosition(),edittext.getText().toString());
 textView.setText(Result);
 }

 }

这是我的功能

public String temp(int ddfrom,int ddto,String edittextvalue) {

float s;
    float inputValue = Float.parseFloat(edittextvalue);
    s = rate*inputValue;
return String.valueOf(s);

}
    02-04 12:56:33.029: E/AndroidRuntime(317): Uncaught handler: thread main exiting due to uncaught exception
      02-04 12:56:33.119: E/AndroidRuntime(317): java.lang.NumberFormatException: -
      02-04 12:56:33.119: E/AndroidRuntime(317):    at org.apache.harmony.luni.util.FloatingPointParser.initialParse(FloatingPointParser.java:149)
      02-04 12:56:33.119: E/AndroidRuntime(317):    at org.apache.harmony.luni.util.FloatingPointParser.parseFloat(FloatingPointParser.java:319)
      02-04 12:56:33.119: E/AndroidRuntime(317):    at java.lang.Float.parseFloat(Float.java:288)
      02-04 12:56:33.119: E/AndroidRuntime(317):    at com.sample.unitconverter.Conversion.timee(Conversion.java:651)
      02-04 12:56:33.119: E/AndroidRuntime(317):    at com.sample.unitconverter.UnitConverter$6.onTextChanged(UnitConverter.java:397)
      02-04 12:56:33.119: E/AndroidRuntime(317):    at android.widget.TextView.sendOnTextChanged(TextView.java:6131)
      02-04 12:56:33.119: E/AndroidRuntime(317):    at android.widget.TextView.handleTextChanged(TextView.java:6172)
      02-04 12:56:33.119: E/AndroidRuntime(317):    at android.widget.TextView$ChangeWatcher.onTextChanged(TextView.java:6316)
      02-04 12:56:33.119: E/AndroidRuntime(317):    at android.text.SpannableStringBuilder.sendTextChange(SpannableStringBuilder.java:889)
      02-04 12:56:33.119: E/AndroidRuntime(317):    at android.text.SpannableStringBuilder.change(SpannableStringBuilder.java:352)
      02-04 12:56:33.119: E/AndroidRuntime(317):    at android.text.SpannableStringBuilder.change(SpannableStringBuilder.java:269)
      02-04 12:56:33.119: E/AndroidRuntime(317):    at android.text.SpannableStringBuilder.replace(SpannableStringBuilder.java:432)
      02-04 12:56:33.119: E/AndroidRuntime(317):    at android.text.SpannableStringBuilder.replace(SpannableStringBuilder.java:409)
      02-04 12:56:33.119: E/AndroidRuntime(317):    at android.text.SpannableStringBuilder.replace(SpannableStringBuilder.java:28)
      02-04 12:56:33.119: E/AndroidRuntime(317):    at android.view.inputmethod.BaseInputConnection.replaceText(BaseInputConnection.java:578)
      02-04 12:56:33.119: E/AndroidRuntime(317):    at android.view.inputmethod.BaseInputConnection.commitText(BaseInputConnection.java:174)
      02-04 12:56:33.119: E/AndroidRuntime(317):    at com.android.internal.widget.EditableInputConnection.commitText(EditableInputConnection.java:120)
      02-04 12:56:33.119: E/AndroidRuntime(317):    at com.android.internal.view.IInputConnectionWrapper.executeMessage(IInputConnectionWrapper.java:231)
      02-04 12:56:33.119: E/AndroidRuntime(317):    at      com.android.internal.view.IInputConnectionWrapper$MyHandler.handleMessage(IInputConnectionWrapper.java:57)
      02-04 12:56:33.119: E/AndroidRuntime(317):    at android.os.Handler.dispatchMessage(Handler.java:99)
      02-04 12:56:33.119: E/AndroidRuntime(317):    at android.os.Looper.loop(Looper.java:123)
      02-04 12:56:33.119: E/AndroidRuntime(317):    at android.app.ActivityThread.main(ActivityThread.java:4363)
      02-04 12:56:33.119: E/AndroidRuntime(317):    at java.lang.reflect.Method.invokeNative(Native Method)
      02-04 12:56:33.119: E/AndroidRuntime(317):    at java.lang.reflect.Method.invoke(Method.java:521)
      02-04 12:56:33.119: E/AndroidRuntime(317):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:860)
      02-04 12:56:33.119: E/AndroidRuntime(317):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:618)
      02-04 12:56:33.119: E/AndroidRuntime(317):    at dalvik.system.NativeStart.main(Native Method)
4

4 回答 4

1

这是因为“-”无法解析为浮点数,并且您收到 NumberFormatException。

public String temp(int ddfrom,int ddto,String edittextvalue) {
    float s;
    try {
        float inputValue = Float.parseFloat(edittextvalue);
        s = rate*inputValue;
        return String.valueOf(s);
    }
    catch (NumberFormatException nfe) {
           //Error handling.
    }
    return null;
}
于 2013-02-04T13:21:59.537 回答
1

一开始你的方法应该是

public String temp(int ddfrom,int ddto,String edittextvalue) {
float s;
try {
    float inputValue = Float.parseFloat(edittextvalue);
    s = rate*inputValue;
    return String.valueOf(s);
catch (NumberFormatException nfe) {
       //Error handling.
}
return "";

}

并将侦听器代码更新为

@Override
        public void onTextChanged(CharSequence s, int start, int before,
                int count) {
if(edittext.length()!=0){
     if(edittext.length()==1 && "-".equals(edittext.getText().toString())) {
         //Nothing to do
     } else {

         String Result =conversion.temp(spinner1.getSelectedItemPosition(),
        spinner2.getSelectedItemPosition(),edittext.getText().toString());
        textView.setText(Result);
     }
 }
于 2013-02-04T13:31:15.807 回答
0

如果您正在使用android:inputType="numberDecimal"不允许负数。在这种情况下,您应该添加:android:inputType="numberDecimal|numberSigned" 这将适用于您的情况

于 2013-02-04T13:21:14.097 回答
0

我不确定您在做什么,但是每次在onTextChanged函数中更改文本时您都在解析一个浮点数(调用函数temp

修复函数temp以在例如出现解析错误时返回 0。这是因为字符串“-”不会被解析为浮点数。

public String temp(int ddfrom,int ddto,String edittextvalue) {
        try {
        float s;
        float inputValue = Float.parseFloat(edittextvalue);
        s = rate*inputValue;
        return s;
    } catch(Exception ex) {
        return "0";
    }

}
于 2013-02-04T13:19:07.780 回答