我得到了「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)
什麼是39號線? –
不要在您的問題中包含您的電子郵件密碼!我已經爲你刪除它。 –
謝謝,我只是忘了刪除它 – Krithi