1

我正在制作一个应用程序,允许用户填写表格并将这些输入的详细信息发送到电子邮件 ID

每当我每次点击发送详细信息按钮时:不幸的是消息启动器已停止

Java 代码:

公共类 MainActivity 扩展 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);
}

   }

表单.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>

清单.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>

日志猫:

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
4

2 回答 2

3

您最初的问题是 mime 类型不正确,应该是text/plain,而不是plain/text。但是,这会创建更多通用共享Intent,以匹配从 SMS 到 Dropbox 的所有类型的应用程序。如果您真的只想将用户的选项限制为电子邮件客户端,您应该Intent使用不同的类型设置,如下所示:

Intent messageIntent = new Intent();
messageIntent.setAction(Intent.ACTION_SEND);
messageIntent.setType(“message/rfc822”);

最后,更多的附注,由于您要求外部应用程序为您处理此操作,因此良好做法还规定您的应用程序应该捕获ActivityNotFoundException并通知用户,以防用户实际上没有应用程序在他们将处理此请求的设备上。

于 2013-01-05T05:05:19.977 回答
0

这是让用户发送电子邮件的方法:

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@domain.com");
emailIntent.putExtra(android.content.Intent.EXTRA_TEXT, "Default email text");
startActivity(Intent.createChooser(emailIntent, "Send email via:"));
于 2013-01-05T04:57:08.080 回答