2017-03-06 73 views
-3

我得到了「java.lang.NullPointerException:嘗試調用虛擬方法'java.lang.String java.lang.Object.toString()'在一個空對象引用「這個錯誤,當我試圖發送email.i已經看到這個網站上的這個相同的問題的幾個答案,但我不能糾正我error.Please做幫我......下面是我的代碼 SendMailActivity錯誤:java.lang.NullPointerException:試圖調用虛擬方法'java.lang.String java.lang.Object.toString()'null對象引用

import android.app.Activity; 
import android.content.Context; 
import android.content.Intent; 
import android.os.Bundle; 
import android.view.View; 
import android.widget.Button; 

import java.util.Arrays; 
import java.util.List; 

public class SendMailActivity extends Activity { 

/** Called when the activity is first created. */ 
String body,number,email; 
final Context context = this; 
@Override 
public void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.activity_send_mail); 
    Bundle b = getIntent().getExtras(); 
    if(b!=null) { 
     number = b.getString("number"); 
     email = b.getString("email"); 
     body = b.getString("otp"); 
    } 
    final Button send = (Button) this.findViewById(R.id.send); 
    send.setOnClickListener(new View.OnClickListener() { 

     public void onClick(View v) { 
      // TODO Auto-generated method stub 
      List<String> toEmailList = Arrays.asList(email 
        .split("\\s*,\\s*")); 
      if(email!=null) { 
       new SendMailTask(SendMailActivity.this).execute("[email protected]", 
         "*****", toEmailList, "test", body); 

       //Intent intent2 = new Intent(context, Testotp.class); 
       //intent2.putExtra("otp", body); 
       Intent intent = new Intent(context, Testotp.class); 
       Bundle b = new Bundle(); 

       //Inserts a String value into the mapping of this Bundle 
       b.putString("number", number); 
       b.putString("otp", body); 
       startActivity(intent); 
      } 

     } 
    }); 

} 

}

SendMailTask​​(這裏是錯誤)

import android.app.Activity; 
import android.app.ProgressDialog; 
import android.os.AsyncTask; 
import android.util.Log; 

import java.util.List; 

public class SendMailTask extends AsyncTask { 

private ProgressDialog statusDialog; 
private Activity sendMailActivity; 

public SendMailTask(Activity activity) { 
    sendMailActivity = activity; 

} 

protected void onPreExecute() { 
    statusDialog = new ProgressDialog(sendMailActivity); 
    statusDialog.setMessage("Getting ready..."); 
    statusDialog.setIndeterminate(false); 
    statusDialog.setCancelable(false); 
    statusDialog.show(); 
} 

@Override 
protected Object doInBackground(Object... args) { 
    try { 
     Log.i("SendMailTask", "About to instantiate GMail..."); 
     publishProgress("Processing input...."); 
     if(args!=null) { 
      GMail androidEmail = new GMail(args[0].toString(), 
         args[1].toString(), (List) args[2], args[3].toString(), args[4].toString()); 

      publishProgress("Preparing mail message...."); 
      androidEmail.createEmailMessage(); 
      publishProgress("Sending email...."); 
      androidEmail.sendEmail(); 
      publishProgress("Email Sent."); 
      Log.i("SendMailTask", "Mail Sent."); 
     } 
    } catch (Exception e) { 
     publishProgress(e.getMessage()); 
     Log.e("SendMailTask", e.getMessage(), e); 
    } 
    return null; 
} 

@Override 
public void onProgressUpdate(Object... values) { 
    statusDialog.setMessage(values[0].toString()); 

} 

@Override 
public void onPostExecute(Object result) { 
    statusDialog.dismiss(); 
} 
} 

logcat的

Attempt to invoke virtual method 'java.lang.String java.lang.Object.toString()' on a null object reference 
                         java.lang.NullPointerException: Attempt to invoke virtual method 'java.lang.String java.lang.Object.toString()' on a null object reference 
                       at com.example.krithihaseshadri.proj.SendMailTask.doInBackground(SendMailTask.java:39) 
                       at android.os.AsyncTask$2.call(AsyncTask.java:295) 
                       at java.util.concurrent.FutureTask.run(FutureTask.java:237) 
                       at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:234) 
                       at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1113) 
                       at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:588) 
                       at java.lang.Thread.run(Thread.java:818) 
+0

什麼是39號線? –

+0

不要在您的問題中包含您的電子郵件密碼!我已經爲你刪除它。 –

+0

謝謝,我只是忘了刪除它 – Krithi

回答

0

這裏是很好的例子,使用從https://stackoverflow.com/a/14374639/4862126

Code Snippet:

public void onClick(View v) { 
    final GMailSender sender = new GMailSender("[email protected]", "password"); 
    new AsyncTask<Void, Void, Void>() { 
     @Override public Void doInBackground(Void... arg) { 
      try { 
       sender.sendMail("This is Subject", 
        "This is Body", 
        "[email protected]", 
        "[email protected]"); 
      } catch (Exception e) { 
       Log.e("SendMail", e.getMessage(), e); 
      } 
     } 
    }.execute(); 

} 

高興再次幫助,如果你需要更多的幫助,接受/給予好評的答案採取AsyncTask參考發送郵件,如果幫你不會費你一分錢! :)

+0

雅我會嘗試它!謝謝 – Krithi

+0

Ohk @krith,讓我知道你是否需要進一步的幫助。如果它有幫助,請接受/ Upvote。 –

+0

我早些時候試過這個代碼,我得到了一些錯誤,這就是爲什麼我使用了我發佈的代碼。 – Krithi

0

請更新您的以下發送按鈕點擊事件代碼。

if(email!=null) { 
     List<String> toEmailList = Arrays.asList(email 
        .split("\\s*,\\s*")); 

       new SendMailTask(SendMailActivity.this).execute("[email protected]", 
         "*****", toEmailList, "test", body); 

       //Intent intent2 = new Intent(context, Testotp.class); 
       //intent2.putExtra("otp", body); 
       Intent intent = new Intent(context, Testotp.class); 
       Bundle b = new Bundle(); 

       //Inserts a String value into the mapping of this Bundle 
       b.putString("number", number); 
       b.putString("otp", body); 
       startActivity(intent); 
      } 

我沒有更多的事情做,但你使用後檢查電子郵件!= null,所以它會發生。

+0

我仍然在SendMailTask​​線沒有39錯誤.... – Krithi

相關問題