Android – Kotlin view with synthetic binding and air

I noticed that when using Kotlin’s synthetic binding, the returned view is non-null (Kotlin will return View!). But it doesn’t make much sense to me because findCachedViewById actually The above can return a null result, which means that the view can actually be 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;
}

So why are they not optional in this case? Why doesn’t Kotlin return to View? When using synthetic bindings, developers are forced to check whether they are empty when dealing with views;

Maybe this is just because I am new to Kotlin, but I think this is a bit counter-intuitive because variables are not available. Selected, but we should still check if the View is actually not null.

So in this case, does the following code make sense?

view?.let {
// handle non null view here
}

I figured it out, I can always find the correct SO question after posting:)

The single exclamation mark after the View is actually not It does not mean that the view cannot be null as I expected.

This answer to another question basically answers my exact question. When using synthetic binding, the View can actually be null, but we Not sure, so it’s a single exclamation mark.

So it’s safe to assume the code I posted above-use? .let {…} is a completely acceptable way to deal with views, when you are not sure whether they are already being initialized when accessing them.

It is very rare that the view may be null, but it may happen.

I noticed that when using Kotlin’s synthetic binding, the returned view is non-null (Kotlin will return View!). But it doesn’t make much sense to me , Because findCachedViewById can actually return a null result, which means that the view can actually be 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;
}

So why are they not optional in this case? Why doesn’t Kotlin return to View? When using synthetic bindings, developers are forced to check whether they are empty when dealing with views;

Maybe this is just because I am new to Kotlin, but I think this is a bit counter-intuitive because variables are not available. Selected, but we should still check if the View is actually not null.

So in this case, does the following code make sense?

view?.let {
// handle non null view here
}

I figured it out, I can always find the right SO question after posting:)

The single exclamation mark after the View does not actually mean that the view cannot be null as I expected .

This answer to another question basically answers my exact question. When using synthetic binding, View can actually be null, but we can’t be sure, so it’s a single exclamation point.

So it is safe to assume the code I posted above-use? .let {…} is a completely acceptable way to deal with views, when you are not sure whether they are already being initialized when accessing them.

It is very rare that the view may be null, but it may happen.

Leave a Comment

Your email address will not be published.