問題描述
我在網上看到了像 edittext.getText().toString()
這樣的例子.我沒有看到任何空檢查.在文檔中,我沒有看到任何聲明這永遠不會為空.
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.
不過,觀察結果說明了什么;這會返回 null 嗎?
Still, what does the observations say; does this ever return null?
推薦答案
getText()
不會返回 null
.所以在下面的方法中NPE是沒有機會的.如果沒有字符串,getText
將返回空字符串,這絕對不是 null
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();
但是如果沒有正確初始化edittext本身可能是null
,因此以下將觸發NPE
However the edittext itself can be null
if not initialized properly, Hence the following will trigger NPE
editText.getText().toString();
這篇關于EditText.getText() 是否曾經返回 null?的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!