如何使用断言在Android的?断言、如何使用、Android

2023-09-05 08:23:26 作者:哈里波特大ヾ

我想用断言OBJ = NULL!对象不能为空的Andr​​oid设备。断言似乎并没有工作,所以我在网上搜索,我发现这个地方的解决方案:

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");
 
精彩推荐
图片推荐