Anubhav R Dasgupta
Anubhav R Dasgupta

Reputation: 1

Error: "Type mismatch: inferred type is Unit but Boolean was expected" Please help me to solve this error below i've also given the code

package com

import android.content.Intent import android.os.Build import androidx.appcompat.app.AppCompatActivity import android.os.Bundle import androidx.annotation.RequiresApi import com.example.sendit.MainActivity import com.example.sendit.R import kotlinx.android.synthetic.main.activity_sign_in.*

class SignInActivity : AppCompatActivity() { @RequiresApi(Build.VERSION_CODES.M) override fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState) setContentView(R.layout.activity_sign_in) btnSignUp.setOnContextClickListener { val intent = Intent(this,MainActivity::class.java) startActivity(intent)}

btnLogin.setOnContextClickListener {
    val regIntent = Intent(this,Log_inActivity::class.java)
    startActivity(regIntent)
}

} }

Upvotes: 0

Views: 1358

Answers (1)

Artem Viter
Artem Viter

Reputation: 854

Definition of OnContextClickListener:

/**
     * Interface definition for a callback to be invoked when a view is context clicked.
     */
    public interface OnContextClickListener {
        /**
         * Called when a view is context clicked.
         *
         * @param v The view that has been context clicked.
         * @return true if the callback consumed the context click, false otherwise.
         */
        boolean onContextClick(View v);
    }

The method onContextClick must return boolean value by signature. In your code snippet last line of lambda passed to setOnContextClickListener returns Unit type because startActivity return void by declaration. That's why just return true (lambda in kotlin uses value retuned by last instruction as return value when her return type not a Unit or void in java terms):

btnLogin.setOnContextClickListener {
    val regIntent = Intent(this,Log_inActivity::class.java)
    startActivity(regIntent)
    true
}

Upvotes: 0

Related Questions