Cannot send email from class

Put your problem here if it does not fit any of the other categories.

Cannot send email from class

Postby zookie » Wed Jul 07, 2010 5:44 am

I have two classes. One is the main class for the application and I created another just to separate the code to send email.


Here is the email class:

Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1. import android.app.Activity;
  2. import android.content.Intent;
  3.  
  4. public class Email extends Activity {
  5.         protected void sendEmail(String emailTo, String emailSubject, String emailBody) {
  6.                 // Setup the recipient in a String array
  7.                 String mailto = (emailTo);
  8.                 //String[] ccto = { "somecc@somedomain.com" };
  9.                 // Create a new Intent to send messages
  10.                 Intent sendIntent = new Intent(Intent.ACTION_SEND);
  11.                 // Add attributes to the intent
  12.                 sendIntent.putExtra(Intent.EXTRA_EMAIL, mailto);
  13.                 //sendIntent.putExtra(Intent.EXTRA_CC, ccto);
  14.                 sendIntent.putExtra(Intent.EXTRA_SUBJECT, emailSubject);
  15.                 sendIntent.putExtra(Intent.EXTRA_TEXT, emailBody);
  16.                 // sendIntent.setType("message/rfc822");
  17.                 sendIntent.setType("text/plain");
  18.  
  19.                 startActivity(Intent.createChooser(sendIntent, "Send Email"));
  20.         }
  21. }
  22.  
Parsed in 0.033 seconds, using GeSHi 1.0.8.4


Here is the code to call the Email class:
Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1.         OnClickListener EmailListener = new OnClickListener() {
  2.            public void onClick(View v) {
  3. try {
  4.                    StringBuilder sb = new StringBuilder();
  5.                    for (String s : callList)
  6.                    {
  7.                        sb.append(s);
  8.                        sb.append("\t");
  9.                    }
  10.                    
  11.                    Email se = new Email();
  12.                    se.sendEmail(email, "Log", sb.toString());
  13. }catch (Exception ex)
  14. {
  15.         ex.printStackTrace();
  16. }
  17.  
  18.  
  19.            }
  20.        };
Parsed in 0.038 seconds, using GeSHi 1.0.8.4



Ignore the StringBuilder, that is just me converting an ArrayList to a String.

If I execute this code, I get a NullPointerException on the last line (startActivity).

If I take the code out of the sendEmail method and just put it in the onClick for the email button, it works perfect.

Can anyone tell me how I can make this work with my Email class?

Thanks!
zookie
Freshman
Freshman
 
Posts: 9
Joined: Mon Jun 28, 2010 4:12 am

Top

Re: Cannot send email from class

Postby anarche » Wed Jul 07, 2010 7:08 am

Can you post the logcat, then we'll get down to business.

Check my sig if you don't know what that is.
anarche
Master Developer
Master Developer
 
Posts: 369
Joined: Sun Apr 11, 2010 5:21 am

Re: Cannot send email from class

Postby zookie » Wed Jul 07, 2010 7:19 am

Here is what I've got in LogCat:


07-07 06:13:19.703: INFO/ActivityManager(52): Displayed activity com.test.call2at/.Call2AT: 12228 ms (total 12228 ms)
07-07 06:13:24.823: DEBUG/dalvikvm(791): GC freed 2515 objects / 134368 bytes in 80ms
07-07 06:13:35.517: DEBUG/AndroidRuntime(873): Shutting down VM
07-07 06:13:35.517: WARN/dalvikvm(873): threadid=3: thread exiting with uncaught exception (group=0x4001b188)
07-07 06:13:35.522: ERROR/AndroidRuntime(873): Uncaught handler: thread main exiting due to uncaught exception
07-07 06:13:35.693: ERROR/AndroidRuntime(873): java.lang.NullPointerException
07-07 06:13:35.693: ERROR/AndroidRuntime(873): at android.app.Activity.startActivityForResult(Activity.java:2749)
07-07 06:13:35.693: ERROR/AndroidRuntime(873): at android.app.Activity.startActivity(Activity.java:2855)
07-07 06:13:35.693: ERROR/AndroidRuntime(873): at com.test.call2at.Email.sendEmail(Email.java:21)
07-07 06:13:35.693: ERROR/AndroidRuntime(873): at com.test.call2at.Call2AT$2.onClick(Call2AT.java:207)
07-07 06:13:35.693: ERROR/AndroidRuntime(873): at android.view.View.performClick(View.java:2364)
07-07 06:13:35.693: ERROR/AndroidRuntime(873): at android.view.View.onTouchEvent(View.java:4179)
07-07 06:13:35.693: ERROR/AndroidRuntime(873): at android.widget.TextView.onTouchEvent(TextView.java:6541)
07-07 06:13:35.693: ERROR/AndroidRuntime(873): at android.view.View.dispatchTouchEvent(View.java:3709)
07-07 06:13:35.693: ERROR/AndroidRuntime(873): at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:884)
07-07 06:13:35.693: ERROR/AndroidRuntime(873): at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:884)
07-07 06:13:35.693: ERROR/AndroidRuntime(873): at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:884)
07-07 06:13:35.693: ERROR/AndroidRuntime(873): at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:884)
07-07 06:13:35.693: ERROR/AndroidRuntime(873): at com.android.internal.policy.impl.PhoneWindow$DecorView.superDispatchTouchEvent(PhoneWindow.java:1659)
07-07 06:13:35.693: ERROR/AndroidRuntime(873): at com.android.internal.policy.impl.PhoneWindow.superDispatchTouchEvent(PhoneWindow.java:1107)
07-07 06:13:35.693: ERROR/AndroidRuntime(873): at android.app.Activity.dispatchTouchEvent(Activity.java:2061)
07-07 06:13:35.693: ERROR/AndroidRuntime(873): at com.android.internal.policy.impl.PhoneWindow$DecorView.dispatchTouchEvent(PhoneWindow.java:1643)
07-07 06:13:35.693: ERROR/AndroidRuntime(873): at android.view.ViewRoot.handleMessage(ViewRoot.java:1691)
07-07 06:13:35.693: ERROR/AndroidRuntime(873): at android.os.Handler.dispatchMessage(Handler.java:99)
07-07 06:13:35.693: ERROR/AndroidRuntime(873): at android.os.Looper.loop(Looper.java:123)
07-07 06:13:35.693: ERROR/AndroidRuntime(873): at android.app.ActivityThread.main(ActivityThread.java:4363)
07-07 06:13:35.693: ERROR/AndroidRuntime(873): at java.lang.reflect.Method.invokeNative(Native Method)
07-07 06:13:35.693: ERROR/AndroidRuntime(873): at java.lang.reflect.Method.invoke(Method.java:521)
07-07 06:13:35.693: ERROR/AndroidRuntime(873): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:860)
07-07 06:13:35.693: ERROR/AndroidRuntime(873): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:618)
07-07 06:13:35.693: ERROR/AndroidRuntime(873): at dalvik.system.NativeStart.main(Native Method)
07-07 06:13:35.793: INFO/Process(52): Sending signal. PID: 873 SIG: 3
07-07 06:13:35.793: INFO/dalvikvm(873): threadid=7: reacting to signal 3
07-07 06:13:35.793: ERROR/dalvikvm(873): Unable to open stack trace file '/data/anr/traces.txt': Permission denied
zookie
Freshman
Freshman
 
Posts: 9
Joined: Mon Jun 28, 2010 4:12 am

Re: Cannot send email from class

Postby anarche » Wed Jul 07, 2010 7:47 am

righto! You can't use Intent.createChooser with sendIntent (which is a send-intent). createChooser (Read the API!) creates a chooser intent.

Give your sendIntent straight to startActivity().
anarche
Master Developer
Master Developer
 
Posts: 369
Joined: Sun Apr 11, 2010 5:21 am

Re: Cannot send email from class

Postby zookie » Wed Jul 07, 2010 4:12 pm

Ok, that makes sense, but I'm a little too new to understand what I'm missing. I read the API though I don't have my head completely wrapped around it yet.

If I use this line just like I had it originally, but I put it in my main Activity, it works. But if I do it from a separate class it fails. So I tried what I think you said and changed:
Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1. startActivity(Intent.createChooser(sendIntent, "Send Email"));
Parsed in 0.035 seconds, using GeSHi 1.0.8.4


to:
Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1. startActivity(sendIntent);
Parsed in 0.040 seconds, using GeSHi 1.0.8.4


I end up with a NullPointerException and TargetInvocationException. Here is the LogCat:

07-07 15:07:22.654: WARN/dalvikvm(1133): threadid=3: thread exiting with uncaught exception (group=0x4001b188)
07-07 15:07:22.654: ERROR/AndroidRuntime(1133): Uncaught handler: thread main exiting due to uncaught exception
07-07 15:07:22.793: ERROR/AndroidRuntime(1133): java.lang.NullPointerException
07-07 15:07:22.793: ERROR/AndroidRuntime(1133): at android.app.Activity.startActivityForResult(Activity.java:2749)
07-07 15:07:22.793: ERROR/AndroidRuntime(1133): at android.app.Activity.startActivity(Activity.java:2855)
07-07 15:07:22.793: ERROR/AndroidRuntime(1133): at com.test.call2at.Email.sendEmail(Email.java:22)
07-07 15:07:22.793: ERROR/AndroidRuntime(1133): at com.test.call2at.Call2AT$3.onClick(Call2AT.java:263)
07-07 15:07:22.793: ERROR/AndroidRuntime(1133): at android.view.View.performClick(View.java:2364)
07-07 15:07:22.793: ERROR/AndroidRuntime(1133): at android.view.View.onTouchEvent(View.java:4179)
07-07 15:07:22.793: ERROR/AndroidRuntime(1133): at android.widget.TextView.onTouchEvent(TextView.java:6541)
07-07 15:07:22.793: ERROR/AndroidRuntime(1133): at android.view.View.dispatchTouchEvent(View.java:3709)
07-07 15:07:22.793: ERROR/AndroidRuntime(1133): at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:884)
07-07 15:07:22.793: ERROR/AndroidRuntime(1133): at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:884)
07-07 15:07:22.793: ERROR/AndroidRuntime(1133): at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:884)
07-07 15:07:22.793: ERROR/AndroidRuntime(1133): at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:884)
07-07 15:07:22.793: ERROR/AndroidRuntime(1133): at com.android.internal.policy.impl.PhoneWindow$DecorView.superDispatchTouchEvent(PhoneWindow.java:1659)
07-07 15:07:22.793: ERROR/AndroidRuntime(1133): at com.android.internal.policy.impl.PhoneWindow.superDispatchTouchEvent(PhoneWindow.java:1107)
07-07 15:07:22.793: ERROR/AndroidRuntime(1133): at android.app.Activity.dispatchTouchEvent(Activity.java:2061)
07-07 15:07:22.793: ERROR/AndroidRuntime(1133): at com.android.internal.policy.impl.PhoneWindow$DecorView.dispatchTouchEvent(PhoneWindow.java:1643)
07-07 15:07:22.793: ERROR/AndroidRuntime(1133): at android.view.ViewRoot.handleMessage(ViewRoot.java:1691)
07-07 15:07:22.793: ERROR/AndroidRuntime(1133): at android.os.Handler.dispatchMessage(Handler.java:99)
07-07 15:07:22.793: ERROR/AndroidRuntime(1133): at android.os.Looper.loop(Looper.java:123)
07-07 15:07:22.793: ERROR/AndroidRuntime(1133): at android.app.ActivityThread.main(ActivityThread.java:4363)
07-07 15:07:22.793: ERROR/AndroidRuntime(1133): at java.lang.reflect.Method.invokeNative(Native Method)
07-07 15:07:22.793: ERROR/AndroidRuntime(1133): at java.lang.reflect.Method.invoke(Method.java:521)
07-07 15:07:22.793: ERROR/AndroidRuntime(1133): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:860)
07-07 15:07:22.793: ERROR/AndroidRuntime(1133): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:618)
07-07 15:07:22.793: ERROR/AndroidRuntime(1133): at dalvik.system.NativeStart.main(Native Method)
07-07 15:07:22.893: INFO/Process(52): Sending signal. PID: 1133 SIG: 3
07-07 15:07:22.893: INFO/dalvikvm(1133): threadid=7: reacting to signal 3


Thanks!
zookie
Freshman
Freshman
 
Posts: 9
Joined: Mon Jun 28, 2010 4:12 am

Re: Cannot send email from class

Postby anarche » Thu Jul 08, 2010 12:17 am

okies, I am actually wrong about the chooser. What the chooser does tho' is create a popup asking if you want the data sent using EMAIL/MMS/FACEBOOK whatever - have you been getting that popup??

Otherwise look at this
anarche
Master Developer
Master Developer
 
Posts: 369
Joined: Sun Apr 11, 2010 5:21 am

Top

Re: Cannot send email from class

Postby zookie » Fri Jul 09, 2010 4:52 am

If I put the code in the main activity, I do get the popup asking me if I want to send the data with the available options (email, message, etc).

If I use this same code in the Email class, it does not popup anything. It is that line, startActivity, that causes the error. Just not sure why it causes the error. Works fine if the code is in the main activity.
zookie
Freshman
Freshman
 
Posts: 9
Joined: Mon Jun 28, 2010 4:12 am

Top

Return to Other Coding-Problems

Who is online

Users browsing this forum: Google [Bot] and 17 guests