Does EditText.getText() ever returns null?

2019-01-07 20:58发布

问题:

All over the net I see examples like edittext.getText().toString(). I do not see any null check. In docs I do not see any statement that would say that this will never be null.

Still, what does the observations say; does this ever return null?

回答1:

getText() will not return null. So there is no chance for NPE in following method. the getText will return empty string if there is no string, which is definitely not null

getText().toString();

However the edittext itself can be null if not initialized properly, Hence the following will trigger NPE

editText.getText().toString();


回答2:

UPDATE:

It does appear as of Jan 18, 2018 this is now possible.

OLD ANSWER:

No, EditText.getText() never returns null. One way to verify this is to check the Android source code for EditText.getText():

EditText.java shows:

public Editable getText() {
    return (Editable) super.getText();
}

Since EditText extends TextView, the call to super.getText() must be TextView.getText(). Now we move on to TextView.getText() to see what it returns:

TextView.java shows:

public CharSequence getText() {
    return mText;
}

Now we need to know if mText could ever be null.

Digging deeper into the TextView.java source we see that mText is initialized as an empty string in the TextView constructor:

public TextView(Context context, AttributeSet attrs, int defStyle) {
    super(context, attrs, defStyle);
    mText = "";
    …
}

Once we see that the EditText constructor calls the TextView constructor:

public EditText(Context context, AttributeSet attrs, int defStyle) {
    super(context, attrs, defStyle);
}

we can safely conclude that EditText.getText() can never return null, because as soon as an EditText is constructed, mText is given a value of an empty string.

However, as StinePike pointed out, EditText.getText() can possibly cause an NPE if your EditText is null when it makes the call to getText().



回答3:

With Android P SDK it is annotated as nullable in the AppCompatEditText class so it can return null.

And from the docs:

Return the text that the view is displaying. If an editable text has not been set yet, this will return null.

@Override
@Nullable
public Editable getText() {
    if (Build.VERSION.SDK_INT >= 28) {
        return super.getText();
    }
    // A bug pre-P makes getText() crash if called before the first setText due to a cast, so
    // retrieve the editable text.
    return super.getEditableText();
}


回答4:

I dont think so it will ever return null.

But if you want to check whether the returned text is empty or not might I suggest using TextUtils.isEmpty() method

Edit:- The documentation doesn't states anything regarding the returned value. And from what I've seen in the source code is that when you initialize a EditText, the default text value is set to "". So it will never return null



回答5:

it will return null because when apps runs its empty and it returns null, use .getText.toString inside a button click listener, now when you click button it will get the text which you have entered on editText.



回答6:

try in this way

String edittext = edittext.getText().toString();
if(edittext.length==0){ Log.d("null","the valueis null")};