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

标签 android kotlin android-8.0-oreo

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

not enough information to infer parameter T in fun findViewById(id: Int): T!

最佳答案

这是因为从 Android O 开始,我们不需要强制转换它。有几个选项。替换:

val textInput = findViewById(R.id.edit_text) as TextInputLayout

任一个:

val textInput:TextInputLayout = findViewById(R.id.edit_text)

或者:

val textInput = findViewById<TextInputLayout>(R.id.edit_text)

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

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

关于android - 升级到编译 sdk 版本后 findViewById 出错,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44461571/

相关文章:

android - setFastScrollEnabled 在我的所有手机上都能正常工作,但在 Honeycomb 平板电脑上会崩溃

nullpointerexception - NPE使用glfwWindowShouldClose(Kotlin)时

android - 在 android 8(oreo) 上使用 altbeacon 库,后台信标扫描不工作

android - 在 Oreo 设备的聚焦 View 上禁用自动突出显示

android - 如何使用 Android 的 MediaProjection API 来录制在 android 设备中输出的系统音频?

android - 为什么我的 android Activity 总是开始滚动到底部?

generics - Kotlin 反射/解析泛型模板参数的原始类型

android - relativelayout 中的 Textview 和按钮在 android oreo 和 pie 中不可见,但在其他 android 版本中完全可见

java - 有时必须等待很长时间,直到 HttpClient.execute() 抛出异常

Kotlin/Native 无法导入 io.ktor.network.selector.ActorSelectorManager