此代码实现从Android客户端发送电子邮件,并且它不断崩溃。当我检查错误日志时,它显示:我已尝试进行故障排除,以查看发生此错误的原因,我无法看到。请帮忙。引发nullPointerException和应用程序崩溃
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("[email protected]", "[email protected]");
}
});
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("[email protected]"));
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();
}
}
}
shift progressDialog = new ProgressDialog(this);在onCreate ....我也会建议,而不是'这个'关键字使用'MainActivity.this' –
@ShadowDroid没有理由在匿名类外使用'MainActivity.this'语法 – maciekjanusz
@ShadowDroid你已经链接的答案是有点误导。 'OuterClass.this'语法在任何地方都可以工作,但只有在嵌套类中才有必要**,以访问对其外部类的引用。尽管如此,不需要在'onCreate'中使用这个语法,因为它会和你刚刚编写'this'一样。 PS。在之前的评论中,我的意思是**嵌套**类,而不仅仅是匿名类。对不起 – maciekjanusz