kotlin – Android data binding problem: "Cause: couldn't make a guess for"-ThrowExceptions

Exception or error:

i’m enabling data binding with adding

dataBinding {
enabled = true
}

and

kapt ‘com.android.databinding:compiler:3.1.4’

in app level build.gradle file.

apply plugin: ‘kotlin-kapt’

is added top of that. project is based on kotlin .

here is my model:

    package ir.app.myapplication;



data class cisclass(val equRevId:String)

main Activity :

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

        val user = cisclass("123")
        binding.setVariable(BR.cis, user)
        binding.executePendingBindings()

here is my layout:

<?xml version="1.0" encoding="utf-8"?>

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

    <data>

        <variable
            name="cis"
            type="ir.app.myapplication.cisclass" />
    </data>

    <androidx.core.widget.NestedScrollView


        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_margin="5dp"
        android:orientation="vertical"
        android:scrollbars="vertical">


        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:orientation="vertical">



                    <TextView
                        android:text="@{cis.equRevId}"
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:textStyle="bold"

                        />


        </LinearLayout>


    </androidx.core.widget.NestedScrollView>

</layout>

i will got this error:

java.lang.IllegalArgumentException: couldn't make a guess for ir.meedc.myapplication.cisclass
    at com.squareup.javapoet.Util.checkArgument(Util.java:64)
    at com.squareup.javapoet.ClassName.bestGuess(ClassName.java:171)
    at android.databinding.tool.ext.ExtKt.toTypeName(ext.kt:244)
    at android.databinding.tool.ext.ExtKt.toTypeName(ext.kt:192)
    at android.databinding.tool.ext.ExtKt.toTypeName(ext.kt:173)
    at android.databinding.tool.writer.BaseLayoutBinderWriter.createVariableFields(BaseLayoutBinderWriter.kt:229)
    at android.databinding.tool.writer.BaseLayoutBinderWriter.access$createVariableFields(BaseLayoutBinderWriter.kt:39)
    at android.databinding.tool.writer.BaseLayoutBinderWriter$createType$1.invoke(BaseLayoutBinderWriter.kt:67)
    at android.databinding.tool.writer.BaseLayoutBinderWriter$createType$1.invoke(BaseLayoutBinderWriter.kt:39)
    at android.databinding.tool.ext.Javapoet_extKt.classSpec(javapoet_ext.kt:39)
    at android.databinding.tool.writer.BaseLayoutBinderWriter.createType(BaseLayoutBinderWriter.kt:63)
    at android.databinding.tool.writer.BaseLayoutBinderWriter.write(BaseLayoutBinderWriter.kt:59)
    at android.databinding.tool.BaseDataBinder.generateAll(BaseDataBinder.kt:65)
    at com.android.build.gradle.internal.tasks.databinding.DataBindingGenBaseClassesTask$CodeGenerator.run(DataBindingGenBaseClassesTask.kt:212)
    at com.android.build.gradle.internal.tasks.databinding.DataBindingGenBaseClassesTask$writeBaseClasses$$inlined$recordTaskAction$1.invoke(AndroidVariantTask.kt:52)
    at com.android.build.gradle.internal.tasks.databinding.DataBindingGenBaseClassesTask$writeBaseClasses$$inlined$recordTaskAction$1.invoke(AndroidVariantTask.kt:31)
    at com.android.build.gradle.internal.tasks.Blocks.recordSpan(Blocks.java:91)

how can i fix that? whats the problem?

How to solve:

as mentioned in https://stackoverflow.com/a/50115802/7407809
your model class must be start with uppercase letter.

###

Try to use instead binding.setVariable(BR.cis, user) this binding.setCis(user)

Leave a Reply

Your email address will not be published. Required fields are marked *