1

I got a crash when I try to send an email using intent with kotlin

here's my function

/**
 * intentEmail is called when we need to send email
 *
 * @param price int
 */
fun intentEmail(price: Int) {


    var intent =  Intent(Intent.ACTION_SEND)

    //intent.putExtra(Intent.EXTRA_EMAIL, addressees)
    intent.data= Uri.parse("mailto:")
    intent.putExtra(Intent.EXTRA_SUBJECT, "Just Java order for $name")
    intent.putExtra(Intent.EXTRA_TEXT, createOrderSummary(price))



    if(intent.resolveActivity(packageManager) != null){
        startActivity(intent)
    }



}

and the crash happens when calling startActivity(intent)

and here's my LogCat enter image description here

tamersolieman
  • 411
  • 4
  • 7
  • Use LogCat to examine the Java stack trace associated with your crash: https://stackoverflow.com/questions/23353173/unfortunately-myapp-has-stopped-how-can-i-solve-this Also, please note that `ACTION_SEND` does not use the `Uri`, so you should remove the `intent.data= Uri.parse("mailto:")` line. – CommonsWare Aug 16 '17 at 23:54
  • have you added your permissions in your manifest file ? – Thorvald Aug 17 '17 at 00:07
  • @CommonsWare I attach the LogCat also comment intent.data line but there's no crash but the app doesn't do anything – tamersolieman Aug 17 '17 at 00:17
  • @SígvardrÓlavrsson which permissions do you mean? – tamersolieman Aug 17 '17 at 00:18
  • You are crashing because there is no activity that matches your Intent. – CommonsWare Aug 17 '17 at 00:31
  • @CommonsWare What do you mean? I'm newbie in Android and Kotlin – tamersolieman Aug 17 '17 at 00:34
  • 1
    1. I would suggest you to read the android docs (https://developer.android.com/guide/components/intents-common.html#Email). It states that for no attachments you should use "ACTION_SENDTO" as action. – lampenlampen Aug 18 '17 at 09:09
  • 2. It seems you have no Email-client installed on your phone, which can handle the intent. – lampenlampen Aug 18 '17 at 09:10
  • 3. look at this thread: https://stackoverflow.com/questions/22479211/intent-resolveactivity-null-but-launching-the-intent-throws-an-activitynotfou. It describes exactly your problem. – lampenlampen Aug 18 '17 at 09:16

2 Answers2

0

maybe your phone is not accept this intent action. you should use try catch the avoid this crash. you can also use the phone's other "send mail" app, so that you can find out what's the correct intent.

0

The issue was in

var intent = Intent(Intent.ACTION_SEND)

when I changed it to

var intent = Intent(Intent.ACTION_SENDTO)

it works fine thanks to @lampenlampen

tamersolieman
  • 411
  • 4
  • 7