Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Not able to "findViewById" in Kotlin. Getting error "Type inference failed"

I am getting the following error when I try to find a RecycleView by id.

Error:- Type inference failed: Not enough information to infer parameter T

Error

Code:

class FirstRecycleViewExample : AppCompatActivity() {     val data = arrayListOf<String>()     override fun onCreate(savedInstanceState: Bundle?) {         super.onCreate(savedInstanceState)         setContentView(R.layout.first_recycleview)          val recycler_view =  findViewById(R.id.recycler_view) as RecyclerView ///IN THIS LINE I AM GETTING THE ERROR          data.add("First Data")         data.add("Second Data")         data.add("Third Data")         data.add("Forth Data")         data.add("Fifth Data")          //creating our adapter         val adapter = CustomRecycleAdapter(data)          //now adding the adapter to recyclerview         recycler_view.adapter = adapter     } } 
like image 905
Ravindra Kushwaha Avatar asked Jul 06 '17 13:07

Ravindra Kushwaha


People also ask

How do I replace findViewById?

New in Android Studio 3.6, view binding gives you the ability to replace findViewById with generated binding objects to simplify code, remove bugs, and avoid all the boilerplate of findViewById .

How do I remove findViewById from kotlin?

Kotlin Android Extensions is a great way to avoid writing findViewById in the activity or fragment. Simply, add the kotlin-android-extensions plugin to the module level build. gradle file.

What does findViewById return?

findViewById returns an instance of View , which is then cast to the target class. All good so far. To setup the view, findViewById constructs an AttributeSet from the parameters in the associated XML declaration which it passes to the constructor of View . We then cast the View instance to Button .


2 Answers

Try something like:

val recyclerView = findViewById<RecyclerView>(R.id.recycler_view) 

You can use Kotlin Android Extensions too for that. Check the doc here.
With it, you can call recycler_view directly in your code.

Kotlin Android Extensions:

  • In your app gradle.build add apply plugin: 'kotlin-android-extensions'
  • In your class add import for import kotlinx.android.synthetic.main.<layout>.* where <layout> is the filename of your layout.
  • That's it, you can call recycler_view directly in your code.

How does it work? The first time that you call recycler_view, a call to findViewById is done and cached.

like image 123
Kevin Robatel Avatar answered Sep 18 '22 15:09

Kevin Robatel


You're on API level 26, where the return type of findViewById is now a generic T instead of View and can therefore be inferred. You can see the relevant changelog here.

So you should be able to do this:

val recycler_view = findViewById<RecyclerView>(R.id.recycler_view) 

Or this:

val recycler_view: RecyclerView = findViewById(R.id.recycler_view) 
like image 36
zsmb13 Avatar answered Sep 17 '22 15:09

zsmb13