简体   繁体   English

如果edittext为空,如何读取

[英]How to read if edittext is empty

I have an edittext . 我有一个edittext If the edit text is not empty, as shown in the code, everything works fine. 如果编辑文本不为空,如代码所示,一切正常。 However, if the edittext is empty, the app crashes when the button is pressed. 但是,如果edittext为空,则按下按钮时应用程序会崩溃。 It seems as if the else{} block doesn't work as intended. 似乎else{}块不能按预期工作。 i have tried many different other ways to see if edittext is empty or not, such as contains() , length()>0, etc.. 我已经尝试了许多不同的方法来查看edittext是否为空,例如contains() ,length()> 0等。

b.setOnClickListener(new OnClickListener(){
        public void onClick(View v){

//////////////////////////////////// Row 1 Start /////////////////////////////
        String credit1s = credit1.getText().toString();

        if (!TextUtils.isEmpty(credit1s)) {
        // call parseDouble in here
        double credit_1 = Double.parseDouble(credit1s);
        double CE1 = 0;
            if(grade1.getText().toString().toUpperCase().equals("A")) 
                CE1 = 4.0*credit_1;
            else if(grade1.getText().toString().toUpperCase().equals("B+")) 
                CE1 = 3.5*credit_1;
            else if(grade1.getText().toString().toUpperCase().equals("B")) 
                CE1 = 3.0*credit_1;
            else if(grade1.getText().toString().toUpperCase().equals("C+")) 
                CE1 = 2.5*credit_1;
            else if(grade1.getText().toString().toUpperCase().equals("C")) 
                CE1 = 2.0*credit_1;
            else if(grade1.getText().toString().toUpperCase().equals("D")) 
                CE1 = 1.0*credit_1;
            else if(grade1.getText().toString().toUpperCase().equals("F")) 
                CE1 = 0.0*credit_1;
            else if(grade1.getText().toString().toUpperCase().equals(""))
                CE1 = 0;
            ce.setText(Double.toString(CE1));
        }


 else{
            double CE1;
            CE1=0;
                    ce.setText(Double.toString(CE1));
        }

LogCat: logcat的:

07-26 19:21:18.640: W/asset(11731): Copying FileAsset 0x779b8390 (zip:/data/app/com.example.rutgersgpacalculator-2.apk:/resources.arsc) to buffer size 108076 to make it aligned.
07-26 19:21:18.950: I/Adreno-EGL(11731): <qeglDrvAPI_eglInitialize:385>: EGL 1.4 QUALCOMM build: AU_LINUX_ANDROID_KK_3.5_RB1.04.04.02.006.066_msm8974_refs/tags/AU_LINUX_ANDROID_KK_3.5_RB1.04.04.02.006.066__release_AU ()
07-26 19:21:18.950: I/Adreno-EGL(11731): OpenGL ES Shader Compiler Version: E031.24.00.06
07-26 19:21:18.950: I/Adreno-EGL(11731): Build Date: 02/06/14 Thu
07-26 19:21:18.950: I/Adreno-EGL(11731): Local Branch: 
07-26 19:21:18.950: I/Adreno-EGL(11731): Remote Branch: refs/tags/AU_LINUX_ANDROID_KK_3.5_RB1.04.04.02.006.066
07-26 19:21:18.950: I/Adreno-EGL(11731): Local Patches: NONE
07-26 19:21:18.950: I/Adreno-EGL(11731): Reconstruct Branch: NOTHING
07-26 19:21:19.030: I/InputMethodManager(11731): [startInputInner] EditorInfo { packageName=com.example.rutgersgpacalculator, inputType=0x20001, imeOptions=0x48000005, privateImeOptions=null }, windowGainingFocus=android.view.ViewRootImpl$W@41ae1e58, mServedView=android.widget.EditText{41a8e9b8 VFED..CL .F....ID 0,193-270,311 #7f05003f app:id/Course1}
07-26 19:21:23.740: I/InputMethodManager(11731): [startInputInner] EditorInfo { packageName=com.example.rutgersgpacalculator, inputType=0x2002, imeOptions=0x8004005, privateImeOptions=null }, windowGainingFocus=null, mServedView=android.widget.EditText{41aa6d78 VFED..CL .F.P..ID 270,193-420,311 #7f050046 app:id/editText7}
07-26 19:21:26.580: I/InputMethodManager(11731): [startInputInner] EditorInfo { packageName=com.example.rutgersgpacalculator, inputType=0x20001, imeOptions=0x48000005, privateImeOptions=null }, windowGainingFocus=null, mServedView=android.widget.EditText{41a9e298 VFED..CL .F.P..ID 451,193-594,311 #7f05004b app:id/editText13}
07-26 19:21:28.600: W/dalvikvm(11731): threadid=1: thread exiting with uncaught exception (group=0x41624e18)
07-26 19:21:28.610: E/AndroidRuntime(11731): FATAL EXCEPTION: main
07-26 19:21:28.610: E/AndroidRuntime(11731): Process: com.example.rutgersgpacalculator, PID: 11731
07-26 19:21:28.610: E/AndroidRuntime(11731): java.lang.NumberFormatException: Invalid double: ""
07-26 19:21:28.610: E/AndroidRuntime(11731):    at java.lang.StringToReal.invalidReal(StringToReal.java:63)
07-26 19:21:28.610: E/AndroidRuntime(11731):    at java.lang.StringToReal.parseDouble(StringToReal.java:248)
07-26 19:21:28.610: E/AndroidRuntime(11731):    at java.lang.Double.parseDouble(Double.java:295)
07-26 19:21:28.610: E/AndroidRuntime(11731):    at com.example.rutgersgpacalculator.MainActivity$1.onClick(MainActivity.java:423)
07-26 19:21:28.610: E/AndroidRuntime(11731):    at android.view.View.performClick(View.java:4480)
07-26 19:21:28.610: E/AndroidRuntime(11731):    at android.view.View$PerformClick.run(View.java:18686)
07-26 19:21:28.610: E/AndroidRuntime(11731):    at android.os.Handler.handleCallback(Handler.java:733)
07-26 19:21:28.610: E/AndroidRuntime(11731):    at android.os.Handler.dispatchMessage(Handler.java:95)
07-26 19:21:28.610: E/AndroidRuntime(11731):    at android.os.Looper.loop(Looper.java:157)
07-26 19:21:28.610: E/AndroidRuntime(11731):    at android.app.ActivityThread.main(ActivityThread.java:5872)
07-26 19:21:28.610: E/AndroidRuntime(11731):    at java.lang.reflect.Method.invokeNative(Native Method)
07-26 19:21:28.610: E/AndroidRuntime(11731):    at java.lang.reflect.Method.invoke(Method.java:515)
07-26 19:21:28.610: E/AndroidRuntime(11731):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:852)
07-26 19:21:28.610: E/AndroidRuntime(11731):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:668)
07-26 19:21:28.610: E/AndroidRuntime(11731):    at dalvik.system.NativeStart.main(Native Method)

It sounds like either credit1 , the EditText object or the CharSequence returned by getText() is null. 听起来像credit1 ,EditText对象或getText()返回的CharSequence为null。 Without a Logcat posting, I can't be sure which. 没有Logcat帖子,我无法确定哪个。

You should first make sure credit1s is not null and neither is credit1s.getText(): 您应该首先确保credit1s不为null,并且credit1s.getText()都不是:

String credit1s = "";

if (credit1 != null && credit1.getText() != null) {
    credit1s = credit1.getText().toString();
}

if (!TextUtils.isEmpty(credit1s)) {

....

YOu can simply check the length of edittext by this code 你可以通过这段代码简单地检查edittext的长度

 EditText ed = (EditText) findViewById(R.id.age);

String ed_text = ed.getText().toString().trim();

if(ed_text.isEmpty() || ed_text.length() == 0 || ed_text.equals("") || ed_text == null)
{
    //EditText is empty
}
else
{
    //EditText is not empty
}

or simply by 或者简单地说

if(ed_text.length()>0){
// EMPTY
}else{
//NOT EMPTY
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM