Android EditText字符串返回“”

时间:2013-10-20 00:56:38

标签: android double android-edittext

我很难在我的Android应用程序中使用EditText的输入。我正在输入十进制值,并且我已经处理了小数点,但是当我删除EditText的所有内容时,我的应用程序崩溃了。我在我的onTextChanged函数中为EditText小部件检查了NULL或“”的值,但这已被超越。以下是我的相关功能:

private void initializeEditTextListener(){
    final TextView resultView = (TextView)findViewById(R.id.resultView);
    final EditText userInput = (EditText)findViewById(R.id.haveTextField);
    userInput.addTextChangedListener(new TextWatcher()
    {
        @Override
        public void afterTextChanged(Editable s) {

        }

        @Override
        public void beforeTextChanged(CharSequence s, int start, int count, int after) {

        }

        @Override
        public void onTextChanged(CharSequence s, int start, int before, int count) {
            String inputString = userInput.getText().toString();
            if(inputString != null && inputString != "")
            {
                double input = Double.parseDouble(inputString); //<-line causing error
                double result = input * getFactorFromTable();   //specified in logcat
                resultView.setText(Double.toString(result));
            }
            else
            {
                resultView.setText("");
            }
        }
    }
    );
}

我的logcat:

  

10-19 17:53:59.313 16225-16225 / khandy.application.convertible E / AndroidRuntime:FATAL EXCEPTION:main       java.lang.NumberFormatException:无效的double:“”               at java.lang.StringToReal.invalidReal(StringToReal.java:63)               在java.lang.StringToReal.parseDouble(StringToReal.java:248)               在java.lang.Double.parseDouble(Double.java:295)               at khandy.application.convertible.EntryActivity $ 1.onTextChanged(EntryActivity.java:237)               在android.widget.TextView.sendOnTextChanged(TextView.java:7405)               在android.widget.TextView.handleTextChanged(TextView.java:7467)               在android.widget.TextView $ ChangeWatcher.onTextChanged(TextView.java:9218)               在android.text.SpannableStringBuilder.sendTextChanged(SpannableStringBuilder.java:962)               在android.text.SpannableStringBuilder.replace(SpannableStringBuilder.java:496)               在android.text.SpannableStringBuilder.delete(SpannableStringBuilder.java:212)               在android.text.SpannableStringBuilder.delete(SpannableStringBuilder.java:30)               在android.text.method.BaseKeyListener.backspaceOrForwardDelete(BaseKeyListener.java:94)               在android.text.method.BaseKeyListener.backspace(BaseKeyListener.java:49)               在android.text.method.BaseKeyListener.onKeyDown(BaseKeyListener.java:155)               在android.text.method.NumberKeyListener.onKeyDown(NumberKeyListener.java:138)               在android.widget.TextView.doKeyDown(TextView.java:5544)               在android.widget.TextView.onKeyDown(TextView.java:5356)               在android.view.KeyEvent.dispatch(KeyEvent.java:2705)               在android.view.View.dispatchKeyEvent(View.java:7205)               在android.view.ViewGroup.dispatchKeyEvent(ViewGroup.java:1363)               在android.view.ViewGroup.dispatchKeyEvent(ViewGroup.java:1363)               在android.view.ViewGroup.dispatchKeyEvent(ViewGroup.java:1363)               在android.view.ViewGroup.dispatchKeyEvent(ViewGroup.java:1363)               在com.android.internal.policy.impl.PhoneWindow $ DecorView.superDispatchKeyEvent(PhoneWindow.java:2083)               在com.android.internal.policy.impl.PhoneWindow.superDispatchKeyEvent(PhoneWindow.java:1459)               在android.app.Activity.dispatchKeyEvent(Activity.java:2416)               在com.android.internal.policy.impl.PhoneWindow $ DecorView.dispatchKeyEvent(PhoneWindow.java:2010)               在android.view.ViewRootImpl.deliverKeyEventPostIme(ViewRootImpl.java:3894)               在android.view.ViewRootImpl.deliverKeyEvent(ViewRootImpl.java:3828)               在android.view.ViewRootImpl.deliverInputEvent(ViewRootImpl.java:3385)               在android.view.ViewRootImpl.doProcessInputEvents(ViewRootImpl.java:4483)               在android.view.ViewRootImpl.enqueueInputEvent(ViewRootImpl.java:4461)               在android.view.ViewRootImpl $ ViewRootHandler.handleMessage(ViewRootImpl.java:3146)               在android.os.Handler.dispatchMessage(Handler.java:99)               在android.os.Looper.loop(Looper.java:137)               在android.app.ActivityThread.main(ActivityThread.java:4950)               at java.lang.reflect.Method.invokeNative(Native Method)               在java.lang.reflect.Method.invoke(Method.java:511)               在com.android.internal.os.ZygoteInit $ MethodAndArgsCaller.run(ZygoteInit.java:1004)               在com.android.internal.os.ZygoteInit.main(ZygoteInit.java:771)               在dalvik.system.NativeStart.main(本地方法)

我担心的是,我被告知无效的双人是“”,但是我正在检查这样的事件......

感谢您的帮助!

3 个答案:

答案 0 :(得分:1)

inputString != ""

应该是

!inputString.equals("")

当您比较数字或布尔值时,您可以进行比较。但是在使用String时,您需要equals来比较字符串。

答案 1 :(得分:1)

为方便起见,创建一个方法来检查字符串是空的还是空的

创建一个实用程序类(Say AppUtility .java),并定义一个方法。所以现在你可以在程序的任何地方使用这个方法。

public class AppUtility{

public static boolean isStringEmpty(String input){
        if(input != null && input.length() == 0){
            return true;
        }
        return false;
    }
}

并检查类似

的条件
String inputString = userInput.getText().toString();
            if(AppUtility.isStringEmpty(inputString)){
            // Do YOUR WORK
    }

希望这会对你有所帮助。

答案 2 :(得分:1)

String inputString = userInput.getText().toString();

这不是从EditText onTextChanged内获取文字的首选方法。 您的onTextChanged功能应该是这样的

@Override
        public void onTextChanged(CharSequence s, int start, int before, int count) {
            String inputString = s.toString();
            if(inputString != null && inputString != "")
            {
                double input = Double.parseDouble(inputString); //<-line causing error
                double result = input * getFactorFromTable();   //specified in logcat
                resultView.setText(Double.toString(result));
            }
            else
            {
                resultView.setText("");
            }
        }

onTextChanged函数CharSequence s中,参数只是来自EditText

的文字

你应该捕获由空字符串和包含字母

的字符串产生的异常
@Override
        public void onTextChanged(CharSequence s, int start, int before, int count) {
            String inputString = s.toString();
           try
            {
                double input = Double.parseDouble(inputString); //<-line causing error
                double result = input * getFactorFromTable();   //specified in logcat
                resultView.setText(Double.toString(result));
            }
            catch(NumberFormatException e)
            {
                resultView.setText("");
            }


           catch(Exception e)
           {
                resultView.setText("");
           }
  }