我得到" java.lang.NullPointerException:尝试调用虚方法' java.lang.String java.lang.Object.toString()'在null对象引用"当我试图发送email时,这个错误。我在这个网站上看到了同样问题的几个答案,但我无法纠正我的错误。请帮帮我......下面是我的代码 SendMailActivity
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("krithiha24sesh@gmail.com",
"*****", 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 :(得分:0)
请使用以下内容更新您的发送按钮点击事件代码。
if(email!=null) {
List<String> toEmailList = Arrays.asList(email
.split("\\s*,\\s*"));
new SendMailTask(SendMailActivity.this).execute("krithiha24sesh@gmail.com",
"*****", 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后使用,所以它会发生。
答案 1 :(得分:0)
以下是使用AsyncTask
发送邮件的一个很好的示例。参考取自https://stackoverflow.com/a/14374639/4862126
代码段:
public void onClick(View v) {
final GMailSender sender = new GMailSender("username@gmail.com", "password");
new AsyncTask<Void, Void, Void>() {
@Override public Void doInBackground(Void... arg) {
try {
sender.sendMail("This is Subject",
"This is Body",
"user@gmail.com",
"user@yahoo.com");
} catch (Exception e) {
Log.e("SendMail", e.getMessage(), e);
}
}
}.execute();
}