2014-09-21 49 views
-1

我的應用程序崩潰,雖然代碼似乎是正確的:-O,如果我不插入任何東西editText1應用程序崩潰檢查(numberDecimal)的EditText是空/空

<EditText 
     android:id="@+id/editText1" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_alignParentLeft="true" 
     android:layout_below="@+id/textView1" 
     android:layout_marginTop="18dp" 
     android:ems="10" 
     android:inputType="numberDecimal" > 

     String tmp1 = editText1.getText().toString().trim(); 
       if(tmp1.isEmpty() || tmp1.length() == 0 || tmp1.equals("") || tmp1 == null){ 
        editText1.setError("error"); 
       } 
       else 
+1

究竟是你想怎麼辦? – mou 2014-09-21 10:49:39

+0

如果editext爲空,則添加錯誤消息:setError(「error」); – 2014-09-21 10:50:23

回答

2

先檢查null。也就是說

if(tmp1==null || tmp1.length() == 0 || tmp1.equals("") ||){ editText1.setError("error"); }

2

你需要檢查是否有任何這樣寫在你的editText中

if(!editText1.getText().toString().equals("")) { 
String tmp1 = editText1.getText().toString().trim(); 
       if(tmp1 == null || tmp1.isEmpty() || tmp1.length() == 0 || tmp1.equals("")){ 
        editText1.setError("error"); 
       } 
}else { 
//editText empty 
} 
+0

也與您的代碼,應用程序崩潰:java.lang.NumberFormatException:無效的雙:「」 – 2014-09-21 10:53:24

+1

粘貼您的所有代碼plz – 2014-09-21 10:53:39

+0

Nadir我的代碼工作,問題是在代碼的另一部分 – 2014-09-21 11:14:37