Marcos Maliki
Marcos Maliki

Reputation: 600

How to use Intent.ACTION_DIAL in Kotlin

I've been using Java for the longest but I recently switched to Kotlin. Here's my problem: I want to start a dialer intent from my app but the compiler returns this error

Type mismatch: inferred type is Intent but Context was expected

This is what I tried:

val num = "tel:54646"
startActivity(Intent(Intent.ACTION_DIAL, Uri.parse(num)))

in java this works:

String num = "tel:54646";
startActivity(new Intent(Intent.ACTION_DIAL, Uri.parse(num)));

What am I missing here?

Upvotes: 4

Views: 2470

Answers (1)

IntelliJ Amiya
IntelliJ Amiya

Reputation: 75798

Type mismatch: inferred type is Intent but Context was expected

You should add activity!! before startActivity

try {
        val intent = Intent(Intent.ACTION_DIAL, Uri.parse(num))
        activity!!.startActivity(intent)
     } catch (e: Exception) {
        e.printStackTrace()

     }

Upvotes: 1

Related Questions