Rizqie Miftah
Rizqie Miftah

Reputation: 3

Type mismatch: inferred type is Unit but Boolean was expected

i have an error for gesture, this my code error, error in startActivity(intent) and Toast.makeText

R.id.menu_share -> {
    val intent = Intent(Intent.ACTION_VIEW, Uri.parse("https://web.whatsapp.com"))
    startActivity(intent)
}
R.id.menu_info -> {
    Toast.makeText(this,"Ada Toast", Toast.LENGTH_LONG).show()
}
else -> false

Upvotes: 0

Views: 9684

Answers (2)

Gil G
Gil G

Reputation: 1869

We need more code in order to give you a full answer but I can try to assume and give you the closest answer.

when can be used in two ways

  1. Standalone
  2. As an expression

If you use it as a switch i.e do different actions based on specific cases You don't need to return a value and you also don't need an else statement

For example:

when (menuItem.id) { /** I guess you're trying to perform differet actions based on menu item click */
    R.id.menu_share -> {
        val intent = Intent(Intent.ACTION_VIEW, Uri.parse("https://web.whatsapp.com"))
        startActivity(intent)
    } /** returns Unit */

    R.id.menu_info -> {
        Toast.makeText(this,"Ada Toast", Toast.LENGTH_LONG).show()
    } /** returns Unit */

} /** result ignores / Unit */

The other way you use a when is as an expression and that's when you want the statement to return a value.

In that case, you must fill all possible cases for the type you provided or else if the type is something that you can't validate all other options for example Int, String

For example:

val result = when (menuItem.id) { /** I guess you're trying to perform different actions based on menu item click */
    R.id.menu_share -> {
        val intent = Intent(Intent.ACTION_VIEW, Uri.parse("https://web.whatsapp.com"))
        startActivity(intent)
    } /** Type mismatch: inferred type is Unit but Boolean was expected */

    R.id.menu_info -> {
        Toast.makeText(this,"Ada Toast", Toast.LENGTH_LONG).show()
    } /** Type mismatch: inferred type is Unit but Boolean was expected */

    else -> false /** returns Boolean */

} /** Type mismatch: inferred type is Unit but Boolean was expected */

To fix the issue you need to return the same type on all cases like this

val result = when (menuItem.id) { /** I guess you're trying to perform different actions based on menu item click */
    R.id.menu_share -> {
        val intent = Intent(Intent.ACTION_VIEW, Uri.parse("https://web.whatsapp.com"))
        startActivity(intent)
        true
    } /** returns Boolean */

    R.id.menu_info -> {
        Toast.makeText(this,"Ada Toast", Toast.LENGTH_LONG).show()
        true
    } /** returns Boolean */

    else -> false /** returns Boolean */

} /** returns Boolean */

I hope my explanation answers your question, if not you are more than welcome to comment.

Upvotes: 1

Mirjalal
Mirjalal

Reputation: 1352

R.id.menu_share -> {
    val intent = Intent(Intent.ACTION_VIEW, Uri.parse("https://web.whatsapp.com"))
    startActivity(intent)
}
R.id.menu_info -> {
    Toast.makeText(this,"Ada Toast", Toast.LENGTH_LONG).show()
}
else -> {
    // you can either left this block empty or just put `return`
}

Upvotes: 0

Related Questions