Android中的Async Task上的java.lang.NullPointerException

时间:2013-04-04 10:22:34

标签: android email android-asynctask

我想在不使用电子邮件客户端选项的情况下发送电子邮件。我有自己的自定义电子邮件视图。 我从here下载了这个想法。它给了os Network例外。所以对于这个我使用asynctask并运行它但我在Logcat中收到错误

EmailSend.java:

public class EmailSend extends Activity{


Mail m ;


@Override 
public void onCreate(Bundle icicle) { 
    super.onCreate(icicle); 
    setContentView(R.layout.activity_main); 

    Button addImage = (Button) findViewById(R.id.send_email); 



    addImage.setOnClickListener(new View.OnClickListener() { 
        public void onClick(View view) { 

            new ListViewContactsLoader().execute();

        } 
    }); 
} 



/** An AsyncTask class to retrieve and load listview with contacts */
private class ListViewContactsLoader extends AsyncTask<Void, Void, Boolean>{    

    public ListViewContactsLoader() {

    }

    @Override
    protected Boolean doInBackground(Void... params) {


        try { 
            // m.addAttachment("/sdcard/filelocation"); 

            if(m.send()) { 
                Toast.makeText(EmailSend .this, "Email was sent successfully.", Toast.LENGTH_LONG).show(); 
                return true;
            } else { 
                Toast.makeText(EmailSend .this, "Email was not sent.", Toast.LENGTH_LONG).show(); 

                return false;
            } 
        } 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); 

            return false;
        }
    }




    @Override
    protected void onPostExecute(Boolean result) {          
        // Setting the cursor containing contacts to listview



    }   

    public void onPreExecute() {

        Mail m = new Mail("user@gmail.com", ""); 

        String[] toArr = {"bla@bla.com", "lala@lala.com"}; 
        m.setTo(toArr); 
        m.setFrom("wooo@wooo.com"); 
        m.setSubject("This is an email sent using my Mail JavaMail wrapper from an Android device."); 
        m.setBody("Email body.");
    }


}

}

清单文件:

 <uses-permission android:name="android.permission.WAKE_LOCK" />

  <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />

  <uses-permission android:name="android.permission.ACCESS_WIFI_STATE" />

  <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />

<uses-permission android:name="android.permission.READ_PHONE_STATE" />

<uses-permission android:name="android.permission.READ_CONTACTS" />

logcat的:

   Could not send email

           java.lang.NullPointerException

at com.example.emailer.EmailSend$ListViewContactsLoader.doInBackground(EmailSend.java:54)

at com.example.emailer.EmailSend$ListViewContactsLoader.doInBackground(EmailSend.java:1)

at android.os.AsyncTask$2.call(AsyncTask.java:252)

at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:305)

at java.util.concurrent.FutureTask.run(FutureTask.java:137)

at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1081)

at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:574)

at java.lang.Thread.run(Thread.java:1020)

4 个答案:

答案 0 :(得分:1)

  

它正在提供os网络例外

因为目前您正在尝试显示来自Toast(来自UI线程)方法的doInBackground条消息。使用onPostExecute方法显示Toast消息,因为此方法在UI Thread上调用。试试看:

@Override
    protected void onPostExecute(Boolean result) {          
        // Setting the cursor containing contacts to listview
         if(result){
           // show Toast Message here
          }else{
           // show Toast Message here
           }
         //.....your code here...
    }  

编辑:您获得NullPointerException可能在使用之前忘记初始化mMail对象。在m内初始化onPreExecute对象,而不是再次创建新实例:

public void onPreExecute() {

    m = new Mail("user@gmail.com", ""); 
  ......

答案 1 :(得分:1)

public void onPreExecute() {

    this.m = new Mail("user@gmail.com", ""); 

    String[] toArr = {"bla@bla.com", "lala@lala.com"}; 
    m.setTo(toArr); 
    m.setFrom("wooo@wooo.com"); 
    m.setSubject("This is an email sent using my Mail JavaMail wrapper from an Android device."); 
    m.setBody("Email body.");
}


}

你无法制作anather实例!

答案 2 :(得分:0)

您已实例化本地变量Mail m = new Mail(“user@gmail.com”,“”);在onPreExecute内,但您在if(m.send()) {

中使用doInBackground

Mail m应该是班级ListViewContactsLoader的属性

类似的东西:

public void onPreExecute() {
        m = new Mail("user@gmail.com", ""); 

   private class ListViewContactsLoader extends AsyncTask<Void, Void, Boolean>{  
       private Mail m;

答案 3 :(得分:0)

  

它正在提供os网络例外

因为您正在尝试从doInBackground()进行更新/显示,而这是不可能的。

要解决此问题,您可以执行以下任一操作:

  1. 从onPostExecute()
  2. 显示Toast消息
  3. 或实施runOnUiThread(),如果您想从doInBackground()任何方式更新用户界面。
  4. 例如:

      runOnUiThread(new Runnable() {
    
            @Override
            public void run() {
                Toast.makeText(EmailSend .this, "Email was sent successfully.", Toast.LENGTH_LONG).show(); 
            }
        });