Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Error on findViewById after upgrading to compile sdk version

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

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

like image 721
bond Avatar asked Jun 09 '17 15:06

bond


2 Answers

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

With either:

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

Or:

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

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);
}
like image 197
bond Avatar answered Oct 22 '22 06:10

bond


In pure Java you will have for example

TextView textView = findViewById(R.id.textview1);

In Kotlin you can go with this

val textView = findViewById<TextView>(R.id.textview1)
like image 25
MatPag Avatar answered Oct 22 '22 05:10

MatPag