开发者

Stuck using string.length() function?

I just began in Java and Android. This website already helped me a lot.

Here I'm stuck with something very easy. In other language I never had problem but there..

If I try the following, i have no error on eclipse but once running in the emulator, the software crash at the "if" li开发者_JS百科ne.

String message = mOutEditText.getText().toString();

        if (message.length() > 4) {

If I use if (8 > 4) { then I have no problem.

I also tried if (message.toString.length() > 4) { without any success.

Thanks for your help.


It's hard to say without knowing the error but just about the only thing that could possibly go wrong with that line is a NullPointerException i.e. when you have this problem, it is because:

String message = mOutEditText.getText().toString();

is effectively resolving to:

String message = null;

Most likely this is because your EditText does not have any text in it (from the name of your variable I'm guessing you are using an EditText).

You can account for this case by checking that message is not null. Here is one way of doing it:

if (message != null && message.length() > 4) {


For instance, you initialize message as a static string like "Stack overflow" and try

message.length();

if it works, then there is no problem with toString() function.

Best of luck..!!


In String There are two way of getting the length of String:-this is working properly

String message=""; protected void onCreate(Bundle savedInstanceState){ s.length();}

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜