In my trying AsyncTask I get email address from my server. In onPostExecute() I have to check is email address empty or null. I used following code to check it:
在我嘗試AsyncTask時,我從服務器獲取電子郵件地址。在onPostExecute()中,我必須檢查電子郵件地址是空還是空。我使用以下代碼來檢查它:
if (userEmail != null && !userEmail.isEmpty()) {
Toast.makeText(getActivity(), userEmail, Toast.LENGTH_LONG).show();
UserEmailLabel.setText(userEmail);
}
But in my Toast I see null is printed. My full code:
但在我的Toast中,我看到null被打印出來。我的完整代碼:
private class LoadPersonalData extends AsyncTask {
@Override
protected void onPreExecute() {
super.onPreExecute();
}
protected Void doInBackground(String... res) {
List params = new ArrayList();
params.add(new BasicNameValuePair("user_id", PrefUserName));
params.add(new BasicNameValuePair("type", type_data));
JSONObject json = jsonParser.makeHttpRequest(Url, "POST", params);
String result = "";
try {
result = json.getString("message");
} catch (JSONException e) {
e.printStackTrace();
}
if (result.equals("success")) {
try {
userEmail = json.getString("email");
} catch (JSONException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
return null;
}
@Override
protected void onPostExecute(Void result) {
// TODO Auto-generated method stub
super.onPostExecute(result);
if (userEmail != null && !userEmail.isEmpty()) {
Toast.makeText(getActivity(), userEmail, Toast.LENGTH_LONG).show();
UserEmailLabel.setText(userEmail);
}
}
How can I check for null and empty string? Thanks in advance.
如何檢查null和空字符串?提前致謝。
8 个解决方案
#1
97
String myString = null;
if (TextUtils.isEmpty(myString)) {
return; // or break, continue, throw
}
// myString is neither null nor empty if this point is reached
Log.i("TAG", myString);
Notes
筆記
The documentation states that both null and zero length are checked for. No need to reinvent the wheel here.
文檔指出檢查null和zero長度。無需在這里重新發明輪子。
A good practice to follow is early return.
一個好的做法是早日回歸。
#2
39
From @Jon Skeet comment, really the String value is "null". Following code solved it
從@Jon Skeet注釋,字符串值實際上是“null”。以下代碼解決了它
if (userEmail != null && !userEmail.isEmpty() && !userEmail.equals("null"))
#3
11
Checking for empty string if it is equal to null, length is zero or containing "null" string
如果空字符串等於null,則檢查空字符串,長度為零或包含“null”字符串
public static boolean isEmptyString(String text) {
return (text == null || text.trim().equals("null") || text.trim()
.length() <= 0);
}
#4
3
Yo can check it with this:
喲可以用這個來檢查:
if(userEmail != null && !userEmail .isEmpty())
And remember you must use from exact above code with that order. Because that ensuring you will not get a null pointer exception from userEmail.isEmpty() if userEmail is null.
Above description, it's only available since Java SE 1.6. Check userEmail.length() == 0 on previous versions.
請記住,您必須使用上述完全相同的代碼。因為這確保如果userEmail為null,則不會從userEmail.isEmpty()獲得空指針異常。以上描述,它僅在Java SE 1.6之后可用。檢查先前版本上的userEmail.length()== 0。
#5
1
You can check it with utility method "isEmpty" from TextUtils,
您可以使用TextUtils中的實用方法“isEmpty”進行檢查,
isEmpty(CharSequence str) method check both condition, for null and length.
isEmpty(CharSequence str)方法檢查兩個條件,為null和length。
public static boolean isEmpty(CharSequence str) {
if (str == null || str.length() == 0)
return true;
else
return false;
}
#6
1
if you check null or empty String so you can try this
如果你檢查null或空String,你可以試試這個
if (createReminderRequest.getDate() == null && createReminderRequest.getDate().trim().equals("")){
DialogUtility.showToast(this, ProjectUtils.getString(R.string.please_select_date_n_time));
}
#7
-1
This worked for me
這對我有用
EditText etname = (EditText) findViewById(R.id.etname);
if(etname.length() == 0){
etname.setError("Required field");
}else{
Toast.makeText(getApplicationContext(),"Saved",Toast.LENGTH_LONG).show();
}
or Use this for strings
或者將其用於字符串
String DEP_DATE;
if (DEP_DATE.equals("") || DEP_DATE.length() == 0 || DEP_DATE.isEmpty() || DEP_DATE == null){
}
#8
-1
WORKING !!!!
工作!!!!
String text = mEmailTextView.getText().toString();
if (text.matches("")&& text.trim().equals("null")){
return false;
}
else{
return true;
}