2013-08-01 40 views
4

我寫在我試圖用一些數據發送電子郵件的應用程序,但每當我做點擊提交按鈕發送一封電子郵件,越來越不幸的是應用程序已停止android.content.ActivityNotFoundException使用意向

錯誤:

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

代碼:

Intent emailIntent = new Intent(android.content.Intent.ACTION_SEND); 
emailIntent.setType("text/plain"); 
String aEmailList[] = { "[email protected]" }; 
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的:

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) 
+0

測試在模擬器或設備? –

+0

@TheReader在模擬器 – Sneha

+0

如果可能,嘗試在設備測試。 –

回答

6

幾個月前,我正面臨着同樣的問題,我發現了一個小的解決方案,請通過更換你的

Intent i = new Intent(Intent.ACTION_SEND); 
    i.setType("plain/text"); 
    i.putExtra(Intent.EXTRA_EMAIL, new String[]{"[email protected]"}); 
    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(); 
    } 
} 

如果你願意嘗試下面的代碼得到沒有安裝電子郵件應用程序。消息,這意味着你的工作已經完成,我會建議你在真實DEVICE上檢查它,但我也不知道如何通過EMULATOR發送它

+0

如果我必須通過模擬器發送電子郵件怎麼辦 – Sneha

+0

要在模擬器中配置電子郵件,請使用此鏈接:http: //www.androidaspect.com/2012/06/how-to-send-email-from-android-emulator.html – Android

+0

非常感謝...... MR。 KLAMITSURI @klamitsuri我無法接受兩次回答,我的運氣不好,現在開心:) – Sneha

4

Android模擬器似乎缺少配置的電子郵件帳戶。這就是爲什麼你的代碼崩潰。我建議嘗試發送時捕獲的ActivityNotFoundException:

Intent emailIntent = new Intent(android.content.Intent.ACTION_SEND); 
emailIntent.setType("text/plain"); 
String aEmailList[] = { "[email protected]" }; 
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 
} 

你可以在這個問題上here更多信息。

尤其是這一段有趣的是:

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.

+0

從你的logcat,看來你是使用android.intent.action.SENDTO。這一個你需要通過一個URL方案指定的收件人和主題,而android.content.Intent.ACTION_SEND(如你在發佈代碼中使用它?!)採用這些參數作爲臨時演員。更多信息請參閱本:http://stackoverflow.com/a/8284804/1691231 –

+0

它會打開新的電子郵件使用,你使用的是哪個動作你'Intent'空白電子郵件 – Sneha

+0

? –

1

而不是使用Try-Catch塊,您可以使用resolveActivity()方法在你的Intent對象上。如果結果非空,那麼至少有一個應用程序可以處理該意圖,並且可以安全地調用startActivity()。如果結果爲空,則不應使用該意圖,並且如果可能的話,應該禁用發佈該意圖的功能。

您可以使用以下解決方案:

Intent emailIntent = new Intent(android.content.Intent.ACTION_SEND); 
emailIntent.setType("text/plain"); 
String aEmailList[] = { "[email protected]" }; 
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(); 
} 

你可以找到更多的細節here

我希望它有幫助。

相關問題