此代码用于从Android客户端发送电子邮件,并且不断崩溃。当我检查错误日志时,它显示:我已经尝试过故障排除,看看这个错误来自哪里,我无法看到。请帮助。
10-02 16:15:14.536 7493-7493/? E/AndroidRuntime﹕ FATAL EXCEPTION: main
Process: com.example.ken4ward.emailsender, PID: 7493
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.ken4ward.emailsender/com.example.ken4ward.emailsender.MainActivity}: java.lang.NullPointerException
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2195)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2245)
at android.app.ActivityThread.access$800(ActivityThread.java:135)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1196)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:136)
at android.app.ActivityThread.main(ActivityThread.java:5021)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:515)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:827)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:643)
at dalvik.system.NativeStart.main(Native Method)
Caused by: java.lang.NullPointerException
at com.example.ken4ward.emailsender.MainActivity.onCreate(MainActivity.java:46)
at android.app.Activity.performCreate(Activity.java:5231)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1090)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2159)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2245)
at android.app.ActivityThread.access$800(ActivityThread.java:135)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1196)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:136)
at android.app.ActivityThread.main(ActivityThread.java:5021)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:515)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:827)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:643)
at dalvik.system.NativeStart.main(Native Method)
这是代码。
import android.app.Activity;
import android.app.ProgressDialog;
import android.content.Context;
import android.os.AsyncTask;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;
import java.util.Properties;
import javax.mail.Authenticator;
import javax.mail.Message;
import javax.mail.MessagingException;
import javax.mail.PasswordAuthentication;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;
//import android.support.v7.app.AppCompatActivity;
public class MainActivity extends Activity implements View.OnClickListener{
EditText fieldEmail, emailSubject, emailBody;
String strFieldEmail, strEmailSubject, strEmailBody;
Button buttonLogin;
Context context = null ;
Session session = null;
ProgressDialog progressDialog;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
progressDialog = new ProgressDialog(MainActivity.this);
context = this;
fieldEmail = (EditText)this.findViewById(R.id.editEmail);
emailSubject = (EditText)this.findViewById(R.id.editSubject);
emailBody = (EditText)this.findViewById(R.id.editBody);
buttonLogin.setOnClickListener(this);
}
@Override
public void onClick(View v) {
strFieldEmail = fieldEmail.getText().toString();
strEmailSubject = emailSubject.getText().toString();
strEmailBody = emailBody.getText().toString();
Properties properties = new Properties();
properties.put("mail.smtp.host", "smtp.gmail.com");
properties.put("mail.smtp.socketFactory.port", "465");
properties.put("mail.smtp.socketFactory.class", "javax.net.ssl.SSLSocketFactory");
properties.put("mail.smtp.auth", "true");
properties.put("mail.smtp.port", "465");
session = Session.getDefaultInstance(properties, new Authenticator() {
@Override
protected PasswordAuthentication getPasswordAuthentication() {
return new PasswordAuthentication("kehindeadeoya@gmail.com", "321g201115@...Ademnew");
}
});
progressDialog = ProgressDialog.show(context, "", "Sending email", true);
ReceivedFeedTask receivedFeedTask = new ReceivedFeedTask();
receivedFeedTask.execute();
}
class ReceivedFeedTask extends AsyncTask<String, Void, String>
{
@Override
protected String doInBackground(String... params) {
try
{
Message message = new MimeMessage(session);
message.setFrom(new InternetAddress("kehindeadeoya@gmail.com"));
message.setRecipients(Message.RecipientType.TO, InternetAddress.parse(strFieldEmail));
message.setSubject(strEmailSubject);
message.setContent(strEmailBody, "text/html; charset=utf-8");
Transport.send(message);
}catch (MessagingException messagingException)
{
messagingException.printStackTrace();
}catch (Exception exception)
{
exception.printStackTrace();
}
return null;
}
@Override
protected void onPostExecute(String result)
{
progressDialog.dismiss();
fieldEmail.setText("");
emailSubject.setText("");
emailBody.setText("");
Toast.makeText(getApplicationContext(), "Message sent", Toast.LENGTH_LONG).show();
}
}
}
答案 0 :(得分:1)
需要在 oncreate() 中正确初始化ProgressDialog progressDialog ;
并致电progressDialog
。
实际上您的Locat会抛出
引起:java.lang.NullPointerException 在android.content.ContextWrapper.getApplicationInfo(ContextWrapper.java:152) 在android.view.ContextThemeWrapper.getTheme(ContextThemeWrapper.java:103) 在android.app.AlertDialog.resolveDialogTheme(AlertDialog.java:143) 在android.app.AlertDialog。(AlertDialog.java:98) 在android.app.ProgressDialog。(ProgressDialog.java:77)
试试这种方式,我希望它会对你有帮助。
public class MainActivity extends Activity implements View.OnClickListener{
EditText fieldEmail, emailSubject, emailBody;
String strFieldEmail, strEmailSubject, strEmailBody;
Button buttonLogin;
Context context = null ;
Session session = null;
ProgressDialog progressDialog ;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
progressDialog = new ProgressDialog(MainActivity.this);
fieldEmail = (EditText)findViewById(R.id.editEmail);
emailSubject = (EditText)findViewById(R.id.editSubject);
emailBody = (EditText)findViewById(R.id.editBody);
buttonLogin.setOnClickListener(this);
}
答案 1 :(得分:0)
您必须在清单文件中添加活动。这可能是个问题。
答案 2 :(得分:0)
你犯了一个愚蠢的错误。
最初你宣布了
ProgressDialog progressDialog = new ProgressDialog(this);
你又在这里初步化了
progressDialog = ProgressDialog.show(context, "", "Sending email", true);
所以只需从
中删除它ProgressDialog progressDialog = new ProgressDialog(this);
到
ProgressDialog progressDialog;