Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
453 views
in Technique[技术] by (71.8m points)

android - Long.getLong() failing, returning null to valid string

I've spent the past two hours debugging what seems extremely unlikely. I've stripped the method of a secondary Android Activity to exactly this:

public void onClick(View v) {
        String str = "25";
        long my_long = Long.getLong(str);
} // onClick (v)

And yeah, I get a crash with the good ol' NullPointerException:

09-11 02:02:50.444: ERROR/AndroidRuntime(1588): Uncaught handler: thread main exiting due to uncaught exception 09-11 02:02:50.464: ERROR/AndroidRuntime(1588): java.lang.NullPointerException

It looks like (from other tests) that Long.getLong(str) returns NULL, which is driving me bonkers. WHAT AM I MISSING?

Thanks in advance. I'm okay with stupidly missing the obvious, but my sanity is on the line.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

You are missing the fact that Long.getLong(String str) is not supposed to parse a String to a long, but rather to return a long value of a system property represented by that string. As others have suggested, what you actually need is Long.parseLong(String str).


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

2.1m questions

2.1m answers

60 comments

56.8k users

...