Guy*_*Guy 5 android kotlin kotlin-android-extensions
我注意到当使用Kotlin的合成绑定时,返回的视图为非null(Kotlin将返回View!).但这对我来说没有多大意义,因为findCachedViewById实际上可以返回null结果,这意味着视图实际上可以为null.
public View _$_findCachedViewById(int var1) {
if(this._$_findViewCache == null) {
this._$_findViewCache = new HashMap();
}
View var2 = (View)this._$_findViewCache.get(Integer.valueOf(var1));
if(var2 == null) {
View var10000 = this.getView();
if(var10000 == null) {
return null;
}
var2 = var10000.findViewById(var1);
this._$_findViewCache.put(Integer.valueOf(var1), var2);
}
return var2;
}
Run Code Online (Sandbox Code Playgroud)
那么为什么在这种情况下它们不是可选的呢?为什么Kotlin View?在使用合成绑定时只返回,因此开发人员在处理视图时会被迫检查可空性?
也许这只是因为我是Kotlin的新手,但我认为这有点反直觉,因为变量不是可选的,但我们仍然应该检查View是否实际上不是null.
那么在这种情况下,做下面的代码是否有意义?
view?.let {
// handle non null view here
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
1538 次 |
| 最近记录: |