Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

kotlin - resolve error in DataBindingUtil.setContentView()

I am facing problem in DataBindingUtil.setContentView(). It is showing the following error.

[Type inference failed: Not enough information to infer parameter T in fun setContentView(p0: Activity, p1: Int): T! Please specify it explicitly.

MY Code :

override fun onCreate(savedInstanceState: Bundle?) { 
    super.onCreate(savedInstanceState) 
    // setContentView(R.layout.activity_home) var binding = 
    DataBindingUtil.setContentView(this, R.layout.activity_home) 
} 

Anyone help me resolve this error. I have done clean and Rebuild(Invalidate and Restart) also.

Please let me know any other suggestion.

like image 760
DINESH KUMAR Avatar asked Feb 06 '19 06:02

DINESH KUMAR


People also ask

What is the purpose of SetContentView?

This method sets the activity content to an explicit view. SetContentView() method is 3 types. This view is placed directly into the activity's view hierarchy. Calling this function “locks in” various characteristics of the window that can not, from this point forward, be changed.


2 Answers

Use:

var binding : ActivityHomeBinding = DataBindingUtil.setContentView(this, R.layout.activity_home)

DataBindingUtil.setContentView is returning the binding of the particular layout file passed in as parameter.

like image 78
Pratik Butani Avatar answered Sep 30 '22 18:09

Pratik Butani


Create a binding object like this.

 val binding: ActivityMainBinding = DataBindingUtil.setContentView(
        this, R.layout.activity_main)

You have to mention the Activity Binding type. I have the Main activity so the binding type is ActivityMainBinding. This is what you have missed.

It should be like this:

override fun onCreate(savedInstanceState: Bundle?) { 
  super.onCreate(savedInstanceState) 
  setContentView(R.layout.activity_home)
  var binding : ActivityMainBinding = 
  DataBindingUtil.setContentView(this, R.layout.activity_home) 
} 
like image 39
Jaymin Avatar answered Sep 30 '22 18:09

Jaymin