我想用断言OBJ = NULL!对象不能为空
的Android设备。断言似乎并没有工作,所以我在网上搜索,我发现这个地方的解决方案:
I want to use assert obj != null : "object cannot be null"
on Android device. The assert doesn't seem to work, so i searched online and i found this local solution:
亚行外壳setprop Debug.Assert的1
adb shell setprop debug.assert 1
它在我的本地机器上运行。
it does work on my local machine.
我想用我的Eclipse项目来运行这个命令(所以会在源代码控制)。 我该怎么做呢?
I want to run this command using my eclipse project(so it would be in the source control). How do i do it?
谢谢!
因为大部分时间一个人是不是在调试模式下运行,而一些优化code在Android的断言将无法正常工作。因此,正确的解决方法是手动抛出一个异常,用code是这样的:
Assert won't work in Android because most of the time a person isn't running in debug mode, but rather some optimized code. Thus, the proper solution is to manually throw an exception, with code like this:
if (obj==null) throw new AssertionError("Object cannot be null");