电子邮件意图代码无法正常工作

时间:2016-02-24 14:26:30

标签: java android android-intent

单击按钮时,将调用方法sendemail()。当我在棒棒糖上运行应用程序时,单击该按钮时应用程序关闭,并且看不到意图效果。

      protected void sendEmail() {
    EditText email = (EditText) findViewById(R.id.email);
    EditText customer = (EditText) findViewById(R.id.customer_name);
    String mail_body = "Customer name=" + customer + " Cost" + cost + "\n             thank you :)\n have a great day ahead";
    Log.i("Send email", "");
    String to = email.getText().toString();

    Intent i = new Intent(Intent.ACTION_SEND);
    i.setData(Uri.parse("mailto:"));
    i.putExtra(Intent.EXTRA_EMAIL, to);
    i.putExtra(Intent.EXTRA_SUBJECT, "ORDER SUMMARY FROM COFFEE SHOP");
    i.putExtra(Intent.EXTRA_TEXT, "this is just a test");
    i.setType("message/rfc822");

        startActivity(Intent.createChooser(i,"Send mail..."));
  }

这是我得到的错误日志

02-24 20:59:31.140 13717-13717/example.com.coffee E/AndroidRuntime: FATAL EXCEPTION: main
Process: example.com.coffee, PID: 13717
java.lang.IllegalStateException: Could not find method send email(View) in a parent or ancestor Context for android:onClick attribute defined on view class android.support.v7.widget.AppCompatButton
at android.support.v7.app.AppCompatViewInflater$DeclaredOnClickListener.resolveMethod(AppCompatViewInflater.java:307)
at android.support.v7.app.AppCompatViewInflater$DeclaredOnClickListener.onClick(AppCompatViewInflater.java:266)
at android.view.View.performClick(View.java:4757)
at android.view.View$PerformClick.run(View.java:19757)
at android.os.Handler.handleCallback(Handler.java:739)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:135)
at android.app.ActivityThread.main(ActivityThread.java:5235)
at java.lang.reflect.Method.invoke(Native Method)
at java.lang.reflect.Method.invoke(Method.java:372)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:898)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:693)

2 个答案:

答案 0 :(得分:0)

将sendEmail功能更改为

protected void sendEmail(View v) {
    EditText email = (EditText) findViewById(R.id.email);
    EditText customer = (EditText) findViewById(R.id.customer_name);
    String mail_body = "Customer name=" + customer + " Cost" + cost + "\n             thank you :)\n have a great day ahead";
    Log.i("Send email", "");
    String to = email.getText().toString();

    Intent intent = new Intent(Intent.ACTION_SEND)
    intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
    intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
    intent.setType("message/rfc822");
    intent.putExtra(Intent.EXTRA_EMAIL, new String[]{to});
    intent.putExtra(Intent.EXTRA_CC, new String[]{""});
    intent.putExtra(Intent.EXTRA_SUBJECT, "ORDER SUMMARY FROM COFFEE SHOP");
    intent.putExtra(Intent.EXTRA_TEXT, "this is just a test");
    startActivity(Intent.createChooser(intent, "Send mail..."));
}

并在xml中添加值为“sendEmail”的onClick元素

android:onClick="sendEmail"

答案 1 :(得分:0)

我有两个建议:

  1. 将方法签名更改为源文件中的void sendEmail(View v)

  2. 编辑您为该按钮指定了onClick属性的布局文件。我猜你在你的xml中已经将方法定义为: android:onClick="send email" 在src文件中,您已使用名称sendEmail()定义了方法。