0

此代码实现从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(); 
     } 

    } 
} 
+1

shift progressDialog = new ProgressDialog(this);在onCreate ....我也会建议,而不是'这个'关键字使用'MainActivity.this' –

+0

@ShadowDroid没有理由在匿名类外使用'MainActivity.this'语法 – maciekjanusz

+1

@ShadowDroid你已经链接的答案是有点误导。 'OuterClass.this'语法在任何地方都可以工作,但只有在嵌套类中才有必要**,以访问对其外部类的引用。尽管如此,不需要在'onCreate'中使用这个语法,因为它会和你刚刚编写'this'一样。 PS。在之前的评论中,我的意思是**嵌套**类,而不仅仅是匿名类。对不起 – maciekjanusz

回答

0

您必须将活动添加到清单文件中。这可能是一个问题。

0

你有一个愚蠢的错误。

最初,你已经宣布

ProgressDialog progressDialog = new ProgressDialog(this); 

和你又在这里初始化

progressDialog = ProgressDialog.show(context, "", "Sending email", true); 

所以刚刚从

ProgressDialog progressDialog = new ProgressDialog(this); 

删除这

ProgressDialog progressDialog; 
+0

谁降了?只要告诉我 – Piyush

+0

我已重新编辑代码以反映来自论坛的建议,并且错误仍然存​​在。我检查了清单文件,并且它看起来完好无损,因为没有创建比Main更多的其他活动。 –

1

需要正确初始化ProgressDialog progressDialog ;并致电progressDialog您的oncreate()

其实你的要价较高抛出

产生的原因:在android.view.ContextThemeWrapper.getTheme在android.content.ContextWrapper.getApplicationInfo(ContextWrapper.java:152) 显示java.lang.NullPointerException ( 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); 
} 
+2

对此的一点解释:发生这种情况是因为ProgressDialog在类准备实例化之前被初始化*。初始化任何GUI对象的正确位置是在onCreate()期间(或之后)。 – HappyCactus

+0

@HappyCactus是的。谢谢 –

+1

@HappyCactus你是对的,这是因为直到调用'onCreate',活动对象不是一个有效的'Context'。 – maciekjanusz

相关问题