升级到编译sdk版本后findViewById出错

bon*_*ond 8 android kotlin android-8.0-oreo

升级到编译SDK版本26后,所有findViewById显示错误:

没有足够的信息来推断有趣的findViewById(id:Int)中的参数T:T!

bon*_*ond 15

这是因为从Android O开始,我们不需要投射它.有几个选择.更换:

val textInput = findViewById(R.id.edit_text) as TextInputLayout
Run Code Online (Sandbox Code Playgroud)

有两个:

val textInput:TextInputLayout = findViewById(R.id.edit_text)
Run Code Online (Sandbox Code Playgroud)

要么:

val textInput = findViewById<TextInputLayout>(R.id.edit_text)
Run Code Online (Sandbox Code Playgroud)

如果你想知道封面下发生了什么,从O底层方法改为

public <T extends View> T findViewById(@IdRes int id) {
    return this.getDelegate().findViewById(id);
}
Run Code Online (Sandbox Code Playgroud)