9
votes

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.

7
Please post your code. - Jaymin
override fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState) // setContentView(R.layout.activity_home) var binding = DataBindingUtil.setContentView(this, R.layout.activity_home) ...... } - DINESH KUMAR
@Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); //set contentView ActivityMainBinding binding = DataBindingUtil.setContentView(this, R.layout.activity_main); - Android Geek

7 Answers

6
votes

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.

1
votes

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) 
} 
0
votes

by the way Clean build , invalid and restart is not the only solution in Android studio #JustKidding :)

Regarding the error that you are facing, you actually need to specify the View:

val listViewEmployees = findViewById(R.id.listViewEmployees)

as ListView to

val listViewEmployees = findViewById<ListView>(R.id.listViewEmployees)

0
votes

Modify your code to :

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

Then make "File -> Invalidate Caches / Restart"

0
votes

You have to enable binding in view's XML file as well, by simply wrapping all the ui in layout tag.

Just go to the file: R.layout.activity_home

It should look like the following code-

<?xml version="1.0" encoding="utf-8"?>
<layout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools">

    <data>

    </data>

    <androidx.constraintlayout.widget.ConstraintLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        tools:context=".activities.SurveyListActivity">

    </androidx.constraintlayout.widget.ConstraintLayout>
</layout>
0
votes

In the layout file of your project i.e. ativity_home.xml make sure that you have enclosed your layout may it be a constraint, linear, relative, etc. in the <layout> tag. Also, add all the namespaces to the layout tag.

For example, change

<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".ui.auth.SignUpActivity">

to

<layout 
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
>

<data>
.
.
.
<data/>

<Scroll View
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".ui.auth.SignUpActivity">
-1
votes
private lateinit var binding: ActivityMainBinding
override fun onCreate(savedInstanceState: Bundle?) {
   super.onCreate(savedInstanceState)
   binding = DataBindingUtil.setContentView(this@MainActivity, R.layout.activity_main)
}

In Kotlin everything seems weird initially. Refer the above code to resolve the issue, even then if the error doesn't resolve, checkout the type casting below.

binding = DataBindingUtil.setContentView<ActivityMainBinding>(this@MainActivity, R.layout.activity_main)

FYI: The name ActivityMainBinding comes from the name of your layout xml. If it's named "activity_main" it will be "ActivityMainBinding"