简体   繁体   中英

error in event handing in data binding android

I am getting the error:

****/ data binding error ****msg:Cannot find method handler on ViewDataBinding app/src/main/res/layout/activity_main.xml loc:55:27 - 55:76 ****\ data binding error ****



<data class="ActivityMainBinding">
        <variable
            name="login"
            type="***.loginViewModel" />

        <variable
            name="handler"
            type="***.LoginHandler" />
    </data>

<Button
        android:onClick="@{() -> handler(context).onLoginButtonClicked(login)}"
        android:id="@+id/submit_button"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_marginEnd="16dp"
        android:layout_marginStart="16dp"
        android:layout_marginTop="24dp"
        android:text="Sign In"
        app:layout_constraintEnd_toEndOf="@+id/user_password"
        app:layout_constraintStart_toStartOf="@+id/user_password"
        app:layout_constraintTop_toBottomOf="@+id/user_password"/>

ANd the login handler class:

class LoginHandler(mContext: Context) {

    var mContext=mContext as Activity

    fun onLoginButtonClicked(userInfo: loginViewModel){

        Log.i("Email is ",userInfo.userEmailAddress)
        Log.i("Password is ",userInfo.userPassword)

       userInfo.login(userInfo.userEmailAddress, userInfo.userPassword, mContext)

    }

}

Why am I getting this error? I am rechecked the function name and syntax on click.

Solution tried: 1. Clean and rebuild the project.

handler is defined as a variable, so handler(context) makes no sense.

You will need to just use handler and initialize the object in your code.

handler.onLoginButtonClicked(login)

I'm not familiarized with data binding, but I read you should declare import on top of your layout.

<data>
    <import type="***.LoginHandler"/>
</data>

Info: https://developer.android.com/topic/libraries/data-binding/index.html#layout_details

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM