我特别要求使用Gradle构建工具在最新版本的Android Studio中启用java断言。 linter建议在检测到潜在的NullPointerExceptions时使用它们,所以肯定是可能的。
以下代码在发生错误时起作用并抛出异常:
if (vehicleLocation == null) throw new IllegalStateException("vehicleLocation is null");
这不会抛出AssertionError,所以我假设它们没有打开。
assert vehicleLocation != null;