2012-03-19 5 views
0

Dans mon application, je veux envoyer un e-mail à un compte.en un seul clic sur l'événement.je utilisé le code ci-dessous. cette exception ontEmail Composer dans android

public void onClick(View v) { 
      sendEmail(context, new String[]{"[email protected]"}, "Sending Email", "Test Email", "I am body"); 
     } 

     private void sendEmail(Context context, String[] recipientList, 
       String subject, String body, String title) { 
      Intent emailIntent = new Intent(android.content.Intent.ACTION_SEND); 
      emailIntent.setType("plain/text"); 
      emailIntent.putExtra(android.content.Intent.EXTRA_EMAIL, recipientList); 
      emailIntent.putExtra(android.content.Intent.EXTRA_SUBJECT, subject); 
      emailIntent.putExtra(android.content.Intent.EXTRA_TEXT, body); 
      emailIntent.addFlags(Intent.FLAG_ACTIVITY_MULTIPLE_TASK); 

      try 
      { 
      context.startActivity(Intent.createChooser(emailIntent, title)); 

      }catch(Exception e) 
      { 
       System.out.println(e); 
      } 
     } 

L'exception que je reçois comme

"03-19 19:13:19.553: I/System.out(2010): android.util.AndroidRuntimeException: Calling startActivity() from outside of an Activity context requires the FLAG_ACTIVITY_NEW_TASK flag. 
+0

L'exception est « 03-19 19: 13: 19,553: I/System.out (2010): android.util.AndroidRuntimeException: Appel startActivity() à l'extérieur d'un contexte d'activité exige le drapeau FLAG_ACTIVITY_NEW_TASK. Est-ce vraiment ce que tu veux? " – Palaniraja

+0

03-19 19: 13: 19.553: I/System.out (2010): android.util.AndroidRuntimeException: L'appel de startActivity() depuis l'extérieur d'un contexte d'activité requiert l'indicateur FLAG_ACTIVITY_NEW_TASK. Est-ce vraiment ce que tu veux? – Palaniraja

+0

J'ai cette exception .. comment le résoudre – Palaniraja

Répondre

1

Il fonctionne très bien. première chose à configurer un client de messagerie pour votre émulateur. enter code here

public void onClick(View v) { 

      // TODO Auto-generated method stub 

      Intent emailIntent = new Intent(android.content.Intent.ACTION_SEND); 

      String aEmailList[] = { "[email protected]" }; 
      //String aEmailCCList[] = { ""}; 
      //String aEmailBCCList[] = { "" }; 

      emailIntent.putExtra(android.content.Intent.EXTRA_EMAIL, aEmailList); 
      //emailIntent.putExtra(android.content.Intent.EXTRA_CC, aEmailCCList); 
      //emailIntent.putExtra(android.content.Intent.EXTRA_BCC, aEmailBCCList); 

      emailIntent.putExtra(android.content.Intent.EXTRA_SUBJECT, "My subject"); 

      emailIntent.setType("message/rfc822"); 
      emailIntent.putExtra(android.content.Intent.EXTRA_TEXT, "My message body."); 
       try 
       { 
      startActivity(emailIntent); 
       }catch (android.content.ActivityNotFoundException ex) { 
       Toast.makeText(about.this, "There are no email clients installed.", Toast.LENGTH_SHORT).show(); 
      } 


       catch(Exception e){ System.out.println(e);} 
     } 
1

Essayez cette

private void sendEmail(Context context, String[] recipientList, 
       String subject, String body, String title) { 
      Intent emailIntent = new Intent(android.content.Intent.ACTION_SEND); 
      emailIntent.setType("plain/text"); 
      emailIntent.putExtra(android.content.Intent.EXTRA_EMAIL, recipientList); 
      emailIntent.putExtra(android.content.Intent.EXTRA_SUBJECT, subject); 
      emailIntent.putExtra(android.content.Intent.EXTRA_TEXT, body); 
      emailIntent.addFlags(Intent.FLAG_ACTIVITY_MULTIPLE_TASK); 
      emailIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK); 
      try 
      { 
      context.startActivity(Intent.createChooser(emailIntent, title)); 

      }catch(Exception e) 
      { 
       System.out.println(e); 
      } 
     } 
+0

J'ai une même exception .. – Palaniraja