2014-01-16 200 views
-2

當我嘗試從Android應用程序發送電子郵件到用戶的電子郵件發送電子郵件從Android應用程序到用戶的電子郵件

我的應用程序顯示例外:

java.lang.NullPointerException 

我的代碼是:

 Mail m = new Mail("[email protected]", "*******************"); 
     String[] toArr = { em }; 
     m.setTo(toArr); 
     m.setFrom("[email protected]"); 
     m.setSubject("[MBRM Orion] Current location of phone: " + pn); 
     m.setBody(text); 
     try { 
      if (m.send()) { 
       Toast.makeText(getApplication(), 
         "Email was sent successfully.", Toast.LENGTH_LONG);// .show(); 
      } else { 
       Toast.makeText(getApplication(), "Email was not sent.", 
         Toast.LENGTH_LONG).show(); 
      } 
     } catch (Exception e) { 
      Toast.makeText(getApplication(), e.toString(), Toast.LENGTH_LONG).show(); 
      Log.e("MailApp", "Could not send email", e); 
     } 

任何一個可以幫助我,請

logcat的是:

01-16 13:22:55.955: E/MailApp(6423): Could not send email 
01-16 13:22:55.955: E/MailApp(6423): java.lang.NullPointerException: value == null 
01-16 13:22:55.955: E/MailApp(6423): at java.util.Hashtable.put(Hashtable.java:367) 
01-16 13:22:55.955: E/MailApp(6423): at com.example.MBRMOrion.Mail._setProperties(Mail.java:148) 
01-16 13:22:55.955: E/MailApp(6423): at com.example.MBRMOrion.Mail.send(Mail.java:89) 
01-16 13:22:55.955: E/MailApp(6423): at com.example.MBRMOrion.GPS.SendingEmail(GPS.java:637) 
01-16 13:22:55.955: E/MailApp(6423): at com.example.MBRMOrion.GPS$8$1.run(GPS.java:604) 
01-16 13:22:55.955: E/MailApp(6423): at android.os.Handler.handleCallback(Handler.java:725) 
01-16 13:22:55.955: E/MailApp(6423): at android.os.Handler.dispatchMessage(Handler.java:92) 
01-16 13:22:55.955: E/MailApp(6423): at android.os.Looper.loop(Looper.java:137) 
01-16 13:22:55.955: E/MailApp(6423): at android.app.ActivityThread.main(ActivityThread.java:5041) 
01-16 13:22:55.955: E/MailApp(6423): at java.lang.reflect.Method.invokeNative(Native Method) 
01-16 13:22:55.955: E/MailApp(6423): at java.lang.reflect.Method.invoke(Method.java:511) 
01-16 13:22:55.955: E/MailApp(6423): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793) 
01-16 13:22:55.955: E/MailApp(6423): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560) 
01-16 13:22:55.955: E/MailApp(6423): at dalvik.system.NativeStart.main(Native Method) 

在此先感謝...

+1

閱讀您的堆棧跟蹤。 – njzk2

+0

發佈您的logcat – R9J

+0

...並使用您的調試器。空指針異常很容易解決你自己。設置一個斷點,逐步瀏覽並隨時觀察變量。 – NigelK

回答

1

可能變量「時間」或「文本」或「」的pn不initialized.Have你存儲在其中的值或者爲空? 您還可以使用默認的電子郵件應用發送電子郵件的意圖:

   Intent i=new Intent(Intent.ACTION_SEND); 
       i.setType("message/rfc822"); 
       i.putExtra(Intent.EXTRA_SUBJECT, "subject"); 
       i.putExtra(Intent.EXTRA_EMAIL,new String[]{"[email protected]"}); 
       startActivity(Intent.createChooser(i, "Choose Messaging app:")); 
相關問題