升级到编译SDK版本后,findViewById错误 [英] Error on findViewById after upgrading to compile sdk version

查看:109
本文介绍了升级到编译SDK版本后,findViewById错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

After upgrading to compile SDK version 26, all findViewById showing error:

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

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

推荐答案

这是因为从Android O开始,我们不需要强制转换.有一些选择.替换:

This is because as of Android O, we don't need to cast it. There are a few options. Replace:

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开始,底层方法已更改为

If you want to know what happened under the covers, as of O underlying method changed to

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

这篇关于升级到编译SDK版本后,findViewById错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆