Reputation: 13
I am making an app in which i am allowing user to fill the form and sending those entered details to an email id
whenever i do click on Send Details button everytime i am getting : Unfortunately Message Launcher has Stopped
Java Code:
public class MainActivity extends Activity {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.form);
Button mImgViewCart = (Button) findViewById(R.id.ButtonSendFeedback);
mImgViewCart.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
final EditText nameField = (EditText) findViewById(R.id.EditTextName);
String name = nameField.getText().toString();
final EditText emailField = (EditText) findViewById(R.id.EditTextEmail);
String email = emailField.getText().toString();
final EditText feedbackField = (EditText) findViewById(R.id.EditTextFeedbackBody);
String feedback = feedbackField.getText().toString();
final Spinner feedbackSpinner = (Spinner) findViewById(R.id.SpinnerFeedbackType);
String feedbackType = feedbackSpinner.getSelectedItem().toString();
final CheckBox responseCheckbox = (CheckBox) findViewById(R.id.CheckBoxResponse);
boolean bRequiresResponse = responseCheckbox.isChecked();
// Take the fields and format the message contents
String subject = formatFeedbackSubject(feedbackType);
String message = formatFeedbackMessage(feedbackType, name,
email, feedback, bRequiresResponse);
// Create the message
sendFeedbackMessage(subject, message);
}
});
}
protected String formatFeedbackSubject(String feedbackType) {
String strFeedbackSubjectFormat = getResources().getString(
R.string.feedbackmessagesubject_format);
String strFeedbackSubject = String.format(strFeedbackSubjectFormat, feedbackType);
return strFeedbackSubject;
}
protected String formatFeedbackMessage(String feedbackType, String name,
String email, String feedback, boolean bRequiresResponse) {
String strFeedbackFormatMsg = getResources().getString(
R.string.feedbackmessagebody_format);
String strRequiresResponse = getResponseString(bRequiresResponse);
String strFeedbackMsg = String.format(strFeedbackFormatMsg,
feedbackType, feedback, name, email, strRequiresResponse);
return strFeedbackMsg;
}
protected String getResponseString(boolean bRequiresResponse)
{
if(bRequiresResponse==true)
{
return getResources().getString(R.string.feedbackmessagebody_responseyes);
} else {
return getResources().getString(R.string.feedbackmessagebody_responseno);
}
}
public void sendFeedbackMessage(String subject, String message) {
Intent messageIntent = new Intent(android.content.Intent.ACTION_SEND);
String aEmailList[] = { "####@gmail.com" };
messageIntent.putExtra(android.content.Intent.EXTRA_EMAIL, aEmailList);
messageIntent.putExtra(android.content.Intent.EXTRA_SUBJECT, subject);
messageIntent.setType("plain/text");
messageIntent.putExtra(android.content.Intent.EXTRA_TEXT, message);
startActivity(messageIntent);
}
}
form.xml:
<ScrollView
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/ScrollView01"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:scrollbars="vertical">
<LinearLayout
android:layout_width="fill_parent"
android:orientation="vertical"
android:layout_height="fill_parent">
<TextView
android:id="@+id/TextViewTitle"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/feedbacktitle"
android:textSize="10pt"></TextView>
<EditText
android:id="@+id/EditTextName"
android:layout_height="wrap_content"
android:hint="@string/feedbackname"
android:inputType="textPersonName"
android:layout_width="fill_parent"></EditText>
<EditText
android:id="@+id/EditTextEmail"
android:layout_height="wrap_content"
android:hint="@string/feedbackemail"
android:inputType="textEmailAddress"
android:layout_width="fill_parent"></EditText>
<Spinner
android:id="@+id/SpinnerFeedbackType"
android:layout_height="wrap_content"
android:prompt="@string/feedbacktype"
android:layout_width="fill_parent"
android:entries="@array/feedbacktypelist"></Spinner>
<EditText
android:id="@+id/EditTextFeedbackBody"
android:layout_height="wrap_content"
android:hint="@string/feedbackbody"
android:inputType="textMultiLine"
android:lines="5"
android:layout_width="fill_parent"></EditText>
<CheckBox
android:id="@+id/CheckBoxResponse"
android:layout_height="wrap_content"
android:text="@string/feedbackresponse"
android:layout_width="fill_parent"></CheckBox>
<Button
android:id="@+id/ButtonSendFeedback"
android:layout_height="wrap_content"
android:text="@string/feedbackbutton"
android:layout_width="fill_parent"></Button>
</LinearLayout>
</ScrollView>
Manifest.xml
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.jsrrestjjjb"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="8"
android:targetSdkVersion="17" />
<application
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<activity
android:name="com.example.jsrrestjjjb.MainActivity"
android:label="@string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
<uses-permission android:name="android.permission.SEND_SMS"/>
</manifest>
Logcat:
01-05 10:02:12.759: E/AndroidRuntime(920): FATAL EXCEPTION: main
01-05 10:02:12.759: E/AndroidRuntime(920): java.lang.IllegalStateException: Could not execute method of the activity
01-05 10:02:12.759: E/AndroidRuntime(920): at android.view.View$1.onClick(View.java:3597)
01-05 10:02:12.759: E/AndroidRuntime(920): at android.view.View.performClick(View.java:4202)
01-05 10:02:12.759: E/AndroidRuntime(920): at android.view.View$PerformClick.run(View.java:17340)
01-05 10:02:12.759: E/AndroidRuntime(920): at android.os.Handler.handleCallback(Handler.java:725)
01-05 10:02:12.759: E/AndroidRuntime(920): at android.os.Handler.dispatchMessage(Handler.java:92)
01-05 10:02:12.759: E/AndroidRuntime(920): at android.os.Looper.loop(Looper.java:137)
01-05 10:02:12.759: E/AndroidRuntime(920): at android.app.ActivityThread.main(ActivityThread.java:5039)
01-05 10:02:12.759: E/AndroidRuntime(920): at java.lang.reflect.Method.invokeNative(Native Method)
01-05 10:02:12.759: E/AndroidRuntime(920): at java.lang.reflect.Method.invoke(Method.java:511)
01-05 10:02:12.759: E/AndroidRuntime(920): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
01-05 10:02:12.759: E/AndroidRuntime(920): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)
01-05 10:02:12.759: E/AndroidRuntime(920): at dalvik.system.NativeStart.main(Native Method)
01-05 10:02:12.759: E/AndroidRuntime(920): Caused by: java.lang.reflect.InvocationTargetException
01-05 10:02:12.759: E/AndroidRuntime(920): at java.lang.reflect.Method.invokeNative(Native Method)
01-05 10:02:12.759: E/AndroidRuntime(920): at java.lang.reflect.Method.invoke(Method.java:511)
01-05 10:02:12.759: E/AndroidRuntime(920): at android.view.View$1.onClick(View.java:3592)
01-05 10:02:12.759: E/AndroidRuntime(920): ... 11 more
01-05 10:02:12.759: E/AndroidRuntime(920): Caused by: android.content.ActivityNotFoundException: No Activity found to handle Intent { act=android.intent.action.SEND typ=plain/text flg=0x1 (has clip) (has extras) }
01-05 10:02:12.759: E/AndroidRuntime(920): at android.app.Instrumentation.checkStartActivityResult(Instrumentation.java:1622)
01-05 10:02:12.759: E/AndroidRuntime(920): at android.app.Instrumentation.execStartActivity(Instrumentation.java:1417)
01-05 10:02:12.759: E/AndroidRuntime(920): at android.app.Activity.startActivityForResult(Activity.java:3370)
01-05 10:02:12.759: E/AndroidRuntime(920): at android.app.Activity.startActivityForResult(Activity.java:3331)
01-05 10:02:12.759: E/AndroidRuntime(920): at android.app.Activity.startActivity(Activity.java:3566)
01-05 10:02:12.759: E/AndroidRuntime(920): at android.app.Activity.startActivity(Activity.java:3534)
01-05 10:02:12.759: E/AndroidRuntime(920): at com.example.jsrrestjjjb.MainActivity.sendFeedbackMessage(MainActivity.java:102)
01-05 10:02:12.759: E/AndroidRuntime(920): at com.example.jsrrestjjjb.MainActivity.sendFeedback(MainActivity.java:48)
01-05 10:02:12.759: E/AndroidRuntime(920): ... 14 more
Upvotes: 1
Views: 1716
Reputation: 63303
Your initial problem is that the mime type is incorrect, it should be text/plain
, not plain/text
. However, this creates more of a generic share Intent
that will match all kinds of apps from SMS to Dropbox. If you are truly only wanting to limit the user's options to email clients, you should set up the Intent
with a different type like so:
Intent messageIntent = new Intent();
messageIntent.setAction(Intent.ACTION_SEND);
messageIntent.setType(“message/rfc822”);
Finally, more of a side note, since you are asking an external application to handle this action for you, good practice also dictates that your application should catch the ActivityNotFoundException
and notify the user of this for cases where the user actually doesn't have an app on their device that will handle this request.
Upvotes: 3
Reputation: 12672
This is how to have the user send an email:
Intent emailIntent = new Intent(android.content.Intent.ACTION_SEND);
emailIntent.setType("plain/text");
emailIntent.putExtra(android.content.Intent.EXTRA_SUBJECT, "YOUR SUBJECT");
emailIntent.putExtra(android.content.Intent.EXTRA_EMAIL, "[email protected]");
emailIntent.putExtra(android.content.Intent.EXTRA_TEXT, "Default email text");
startActivity(Intent.createChooser(emailIntent, "Send email via:"));
Upvotes: 0