繁体   English   中英

如何告诉Android Studio已完成空检查?

[英]How to tell Android Studio that the null check has been done?

如何在Android Studio中配置Assert / Check方法?

我使用Mosby MVP框架,在演示者中我经常使用这种模式:

if (isViewAttached()) {
    getView().someViewMethod();
}

getView()标记为@Nullable ,因此Android Studio向我显示警告method invocation 'someViewMethod' could produce NullPointerException 它不明白我以前检查过它。

我找到了关于配置断言/检查方法的精彩答案: https//stackoverflow.com/a/19319326/1263771

但是在新的Android Studio中无法做到这一点,因为它有不同的设置界面。 那么,如何在上一个工作室中做到这一点?

目前最简单的方法是使用

V view = getView();
if (view != null){ // instead of isViewAttached()
   ...
}

很有可能@Nullable注释将在下一个主要版本的Mosby 3.0中被删除

完全同意你@tse。 这是一个很好的答案,但过时了。

但我找到了一个有用的解决方案。 您必须将isViewAttached()方法设为静态并将视图作为参数发送。

protected static boolean isViewAttached(final View view) {
    return view != null && view.isAttached();
}

if (isViewAttached(getView())) {
   getView().someViewMethod();
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM