简体   繁体   English

使用Intent的android.content.ActivityNotFoundException

[英]android.content.ActivityNotFoundException using Intent

I am writing an app in which i am trying to send an email with some data, but whenever i do click on Submit button to send an email, getting : Unfortunately App has Stopped 我正在编写一个应用程序,其中我正在尝试发送包含一些数据的电子邮件,但每当我点击“提交”按钮发送电子邮件时, 获取不幸的是应用程序已停止

Error: 错误:

android.content.ActivityNotFoundException: No Activity found to handle Intent { act=android.intent.action.SEND typ=text/plain (has extras) }

Code: 码:

Intent emailIntent = new Intent(android.content.Intent.ACTION_SEND); 
emailIntent.setType("text/plain");
String aEmailList[] = { "myaccount@gmail.com" };  
emailIntent.putExtra(android.content.Intent.EXTRA_EMAIL, aEmailList); 
emailIntent.putExtra(android.content.Intent.EXTRA_SUBJECT, subject);   
emailIntent.putExtra(android.content.Intent.EXTRA_TEXT, Html.fromHtml(body.toString()));
startActivity(emailIntent);

Logcat: logcat的:

08-01 08:34:22.518: E/AndroidRuntime(1043): FATAL EXCEPTION: main
08-01 08:34:22.518: E/AndroidRuntime(1043): android.content.ActivityNotFoundException: No Activity found to handle Intent { act=android.intent.action.SENDTO typ=text/plain (has extras) }
08-01 08:34:22.518: E/AndroidRuntime(1043):     at android.app.Instrumentation.checkStartActivityResult(Instrumentation.java:1622)
08-01 08:34:22.518: E/AndroidRuntime(1043):     at android.app.Instrumentation.execStartActivity(Instrumentation.java:1417)
08-01 08:34:22.518: E/AndroidRuntime(1043):     at android.app.Activity.startActivityForResult(Activity.java:3370)
08-01 08:34:22.518: E/AndroidRuntime(1043):     at android.app.Activity.startActivityForResult(Activity.java:3331)
08-01 08:34:22.518: E/AndroidRuntime(1043):     at android.app.Activity.startActivity(Activity.java:3566)
08-01 08:34:22.518: E/AndroidRuntime(1043):     at android.app.Activity.startActivity(Activity.java:3534)
08-01 08:34:22.518: E/AndroidRuntime(1043):     at app.my.BookAppointmentActivity$6.onClick(BookAppointmentActivity.java:206)
08-01 08:34:22.518: E/AndroidRuntime(1043):     at android.view.View.performClick(View.java:4204)
08-01 08:34:22.518: E/AndroidRuntime(1043):     at android.view.View$PerformClick.run(View.java:17355)
08-01 08:34:22.518: E/AndroidRuntime(1043):     at android.os.Handler.handleCallback(Handler.java:725)
08-01 08:34:22.518: E/AndroidRuntime(1043):     at android.os.Handler.dispatchMessage(Handler.java:92)
08-01 08:34:22.518: E/AndroidRuntime(1043):     at android.os.Looper.loop(Looper.java:137)
08-01 08:34:22.518: E/AndroidRuntime(1043):     at android.app.ActivityThread.main(ActivityThread.java:5041)
08-01 08:34:22.518: E/AndroidRuntime(1043):     at java.lang.reflect.Method.invokeNative(Native Method)
08-01 08:34:22.518: E/AndroidRuntime(1043):     at java.lang.reflect.Method.invoke(Method.java:511)
08-01 08:34:22.518: E/AndroidRuntime(1043):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
08-01 08:34:22.518: E/AndroidRuntime(1043):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)
08-01 08:34:22.518: E/AndroidRuntime(1043):     at dalvik.system.NativeStart.main(Native Method)

Few months ago i was facing same problem, and i found a small solution, please try below code by replacing yours 几个月前我遇到了同样的问题,我找到了一个小解决方案,请尝试替换以下代码

Intent i = new Intent(Intent.ACTION_SEND);
    i.setType("plain/text");  
    i.putExtra(Intent.EXTRA_EMAIL, new String[]{"recepient@gmail.com"});
    i.putExtra(Intent.EXTRA_SUBJECT, subject);
    i.putExtra(Intent.EXTRA_TEXT, Html.fromHtml(body.toString()));
    try {
     startActivity(i);
    } catch (android.content.ActivityNotFoundException ex) {
     Toast.makeText(Activity.this, "There are no email applications installed.", Toast.LENGTH_SHORT).show();
    }
}

If you will get There are no email applications installed. 如果您将获得没有安装电子邮件应用程序。 message, it means your work is done and i will suggest you to check it on real DEVICE, but i also don't know how to send it via EMULATOR 消息,这意味着你的工作已经完成,我建议你在真实的设备上查看它,但我也不知道如何通过EMULATOR发送它

The Android emulator seems to lack a configured email account. Android模拟器似乎缺少已配置的电子邮件帐户。 That's why your code crashes. 这就是你的代码崩溃的原因。 I'd suggest catching an ActivityNotFoundException when trying to send: 我建议在尝试发送时捕获ActivityNotFoundException:

Intent emailIntent = new Intent(android.content.Intent.ACTION_SEND); 
emailIntent.setType("text/plain");
String aEmailList[] = { "myaccount@gmail.com" };  
emailIntent.putExtra(android.content.Intent.EXTRA_EMAIL, aEmailList); 
emailIntent.putExtra(android.content.Intent.EXTRA_SUBJECT, subject);   
emailIntent.putExtra(android.content.Intent.EXTRA_TEXT, Html.fromHtml(body.toString()));
try{    
  startActivity(emailIntent);
} catch (ActivityNotFoundException ex){
  Toast.makeText(this, "No activity found", Toast.LENGTH_LONG).show(); //Display an error message
}

You can get more information on this issue here . 您可以在此处获取有关此问题的更多信息。

Especially this paragraph is interesting: 特别是这一段很有意思:

If you are using an emulator, you'll need to configure the email client. 如果您使用的是模拟器,则需要配置电子邮件客户端。 If the email client is not configured, it will not respond to the Intent we'll be discussing. 如果未配置电子邮件客户端,则不会响应我们将要讨论的意图。 If you want to see the chooser in action, you'll need to configure a device using multiple messaging applications, such as the Gmail application and the Email application. 如果您希望查看选择器的运行情况,则需要使用多个邮件应用程序配置设备,例如Gmail应用程序和电子邮件应用程序。

Instead of using Try-Catch block you can use resolveActivity() method on your Intent object. 您可以在Intent对象上使用resolveActivity()方法,而不是使用Try-Catch块。 If the result is non-null, then there is at least one app that can handle the intent and it's safe to call startActivity() . 如果结果为非null,则至少有一个应用程序可以处理意图,并且可以安全地调用startActivity() If the result is null, you should not use the intent and, if possible, you should disable the feature that issues the intent. 如果结果为null,则不应使用intent,如果可能,应禁用发出intent的功能。

You can use below solution: 您可以使用以下解决方案:

Intent emailIntent = new Intent(android.content.Intent.ACTION_SEND); 
emailIntent.setType("text/plain");
String aEmailList[] = { "myaccount@gmail.com" };  
emailIntent.putExtra(android.content.Intent.EXTRA_EMAIL, aEmailList); 
emailIntent.putExtra(android.content.Intent.EXTRA_SUBJECT, subject);   
emailIntent.putExtra(android.content.Intent.EXTRA_TEXT, Html.fromHtml(body.toString()));

// Verify that the intent will resolve to an activity
if (emailIntent.resolveActivity(getPackageManager()) != null) {
    startActivity(sendIntent);
}else{
    Toast.makeText(this, "No activity found to send mail.", Toast.LENGTH_LONG).show(); 
}

You can find more details here . 你可以在这里找到更多细节。

I hope it helps. 我希望它有所帮助。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 android.content.ActivityNotFoundException:将url传递给intent - android.content.ActivityNotFoundException: passing url to the intent android.content.ActivityNotFoundException: - android.content.ActivityNotFoundException: android.content.activitynotfoundexception - android.content.activitynotfoundexception Android错误:“ android.content.ActivityNotFoundException:未找到用于处理Intent的活动” - Android error : “android.content.ActivityNotFoundException: No Activity found to handle Intent” Android:android.content.ActivityNotFoundException:找不到处理 Intent 的活动 - Android: android.content.ActivityNotFoundException: No Activity found to handle Intent android.content.ActivityNotFoundException:没有找到处理意图的活动 - googleMaps - android.content.ActivityNotFoundException: No Activity found to handle Intent - googleMaps android.content.ActivityNotFoundException:未找到处理意图错误的活动 - android.content.ActivityNotFoundException: No Activity found to handle Intent error android.content.ActivityNotFoundException:未找到用于处理Intent的活动 - android.content.ActivityNotFoundException: No Activity found to handle Intent Intent.ACTION_VIEW因android.content.ActivityNotFoundException而崩溃 - Intent.ACTION_VIEW crashes with android.content.ActivityNotFoundException android.content.ActivityNotFoundException:找不到处理Intent启动画面的Activity - android.content.ActivityNotFoundException: No Activity found to handle Intent splash screen
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM