2014-03-31 38 views
0

我試圖發送一封電子郵件,從按鈕點擊,到存儲在sqlite數據庫中的所有電子郵件。我已經成功地選擇了一封電子郵件,但現在我正嘗試使用光標繼續將電子郵件發送到所有存儲的電子郵件地址。我遇到的問題是我無法弄清楚null值是從哪裏來的。我使用的是以前從未使用過的代碼,並且在檢查代碼本身時有點麻煩。下面是按鈕調用和從數據庫中檢索地址數組的方法。來自sqlite數據庫的電子郵件按鈕。混淆空值調用

view.findViewById(R.id.btn_save).setOnClickListener(new OnClickListener() { 
     public void onClick(View view) { 
      Mail m = new Mail("[email protected]", "password"); 



      String[] usereMail = getEmailsFromDB().split(","); 
      m.setTo(usereMail); 
      m.setFrom("[email protected]"); 
      m.setSubject("subject"); 
      m.setBody("Body"); 

      try { 

      if(m.send()) { 
       Toast.makeText(getActivity(), "Email was sent successfully.", Toast.LENGTH_LONG).show(); 
      } else { 
       Toast.makeText(getActivity(), "Email was not sent.", Toast.LENGTH_LONG).show(); 
      } 
      } catch(Exception e) { 
      //Toast.makeText(MailApp.this, "There was a problem sending the email.", Toast.LENGTH_LONG).show(); 
      Log.e("MailApp", "Could not send email", e); 
      } 
     } 

     private String getEmailsFromDB() { 
      // TODO Auto-generated method stub 
      dataBase = mHelper.getReadableDatabase(); 
      Cursor Cursor = dataBase.rawQuery("SELECT " + DbHelper.KEY_EMAIL + " FROM " 
        + DbHelper.TABLE_NAME, null); 


      ArrayList<String> array = new ArrayList<String>(); 

      while(Cursor.moveToNext()) { 

       String usereMail = Cursor.getString(Cursor.getColumnIndex(DbHelper.KEY_EMAIL)); 
       array.add(usereMail); 

      } 

      Cursor.close(); 
      return null; 

     } 
     }); 

下面是捕獲的NullPointerException。

03-31 11:30:15.344: E/AndroidRuntime(15802): FATAL EXCEPTION: main 
03-31 11:30:15.344: E/AndroidRuntime(15802): java.lang.NullPointerException 
03-31 11:30:15.344: E/AndroidRuntime(15802): at app.norman.tennis.FoursFragment$1.onClick(FoursFragment.java:61) 
03-31 11:30:15.344: E/AndroidRuntime(15802): at android.view.View.performClick(View.java:4231) 
03-31 11:30:15.344: E/AndroidRuntime(15802): at android.view.View$PerformClick.run(View.java:17537) 
03-31 11:30:15.344: E/AndroidRuntime(15802): at android.os.Handler.handleCallback(Handler.java:725) 
03-31 11:30:15.344: E/AndroidRuntime(15802): at android.os.Handler.dispatchMessage(Handler.java:92) 
03-31 11:30:15.344: E/AndroidRuntime(15802): at android.os.Looper.loop(Looper.java:158) 
03-31 11:30:15.344: E/AndroidRuntime(15802): at android.app.ActivityThread.main(ActivityThread.java:5751) 
03-31 11:30:15.344: E/AndroidRuntime(15802): at java.lang.reflect.Method.invokeNative(Native Method) 
03-31 11:30:15.344: E/AndroidRuntime(15802): at java.lang.reflect.Method.invoke(Method.java:511) 
03-31 11:30:15.344: E/AndroidRuntime(15802): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1083) 
03-31 11:30:15.344: E/AndroidRuntime(15802): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:850) 
03-31 11:30:15.344: E/AndroidRuntime(15802): at dalvik.system.NativeStart.main(Native Method) 

電子郵件地址數組是否被錯誤地創建,或者是電子郵件調用編碼不正確,或者是完全不同的東西?

+0

什麼是行號61 FoursFragment.java – Hariharan

+0

m.setTo(USEREMAIL)返回null。 – RunningWalks

+1

可能是m是空的檢查它。 – Hariharan

回答

1

你從字面上方法getEmailsFromDB()

檢查線return null;

+0

你可以建議我應該使用什麼樣的回報,以便正確地完成呼叫嗎? – RunningWalks

+0

返回數組。 '返回數組;' – JoelFernandes

+0

類型不匹配:無法從ArrayList 轉換爲字符串 – RunningWalks

相關問題