问题描述
过去两个小时我一直在调试看似极不可能的事情.我已经将辅助 Android Activity 的方法剥离为:
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)
是的,我遇到了一个很好的 NullPointerException:
And yeah, I get a crash with the good ol' NullPointerException:
09-11 02:02:50.444:错误/AndroidRuntime(1588):未捕获的处理程序:线程主因未捕获的异常而退出09-11 02:02:50.464:错误/AndroidRuntime(1588):java.lang.NullPointerException
09-11 02:02:50.444: ERROR/AndroidRuntime(1588): Uncaught handler: thread main exiting due to uncaught exception09-11 02:02:50.464: ERROR/AndroidRuntime(1588): java.lang.NullPointerException
看起来(从其他测试中)Long.getLong(str) 返回 NULL,这让我很抓狂.我错过了什么?
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.
推荐答案
您忽略了一个事实,即 Long.getLong(String str)
不应该将 String 解析为 long,而是返回由该字符串表示的系统属性 的长值.正如其他人所建议的,您实际需要的是 Long.parseLong(String str)
.
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)
.
这篇关于Long.getLong() 失败,返回 null 到有效字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!