简体   繁体   中英

E-mail intent code not working

when a button is clicked the method sendemail() is called. when I run the app on lollipop and when the button is clicked the app closes and no effect of intent is seen.

      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..."));
  }

this is error log that I got

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)

Change sendEmail function to

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..."));
}

and in xml add onClick element with value "sendEmail"

android:onClick="sendEmail"

I have two suggestion :

  1. change the method signature as void sendEmail(View v) in your source file.

  2. edit your layout file where you have assigned the onClick attribute for the button. I can guess that in your xml you have defined the method as : android:onClick="send email" and in the src file you have defined the method with name sendEmail() .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM