2014-04-15 86 views
0

代碼將運行併發送我的電子郵件,但每當選中一個框時,由於未調用方法setSubject()而未更改主題。得到它了。如果遇到問題,我會在哪裏調用/放置此方法,以便我可以在我的doInBackground()方法中使用該主題。如果我嘗試在doInBackground之前或之後的任何地方嘗試撥打setSubject(),我在同一線程上運行併發任務時出現錯誤。我試圖在onPreExecute()塊中調用setSubject(),但仍然出現錯誤。任何人有任何想法?這裏是代碼和LogCat。將變量從AsyncTask傳遞到doInBackground Android

public class lastpage extends Activity { 
@Override 
protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.lastpage); 


} 

//method to verify email format using regex pattern 
public boolean validateEmail(String email) { 
    Pattern pattern; 
    Matcher matcher; 
    //set variable to regex email pattern 
    final String EMAIL_PATTERN = "^[_A-Za-z0-9-]+(\\.[_A-Za-z0-9-]+)*@[A-Za-z0-9]+(\\.[A-Za-z0-9]+)*(\\.[A-Za-z]{2,})$"; 
    pattern = Pattern.compile(EMAIL_PATTERN); 
    matcher = pattern.matcher(email); 
    if (matcher.matches()) { 
     return true; 
    } 
    return false; 

} 


public void sendEmails() { 

AsyncTask<String, Void, Void> myTask = new AsyncTask<String, Void, Void>() { 
     private String sub = "Maintenance Request"; 
      String s = ""; 
     CheckBox main = (CheckBox) findViewById(R.id.checkBox1); 
     CheckBox kitchen = (CheckBox) findViewById(R.id.checkBox2); 
     CheckBox bathroom = (CheckBox) findViewById(R.id.checkBox3); 
     CheckBox other = (CheckBox) findViewById(R.id.checkBox4); 

    public String setSubject(String subject) { 
     sub = subject; 
     if (main.isChecked()) subject = subject + " - Main Room"; 
     if (kitchen.isChecked()) subject = subject + " - Kitchen"; 
     if (bathroom.isChecked()) subject = subject + " - Bathroom"; 
     if (other.isChecked()) subject = subject + " - Other"; 
      return subject; 
    } 

       s = setSubject(sub); 
     protected Void doInBackground(String... params) { 

    String host = "smtp.gmail.com"; 
    String username = "[email protected]"; 
    String password = "pwd"; 
    Properties props = new Properties(); 
    props.put("mail.smtp.ssl.enable", "true"); 
    Session session = Session.getInstance(props); 

    session.setDebug(true); 
     EditText e = (EditText) findViewById(R.id.enterEmail); 
     EditText g = (EditText) findViewById(R.id.whichApt); 

     String f = e.toString().replace("\\s", ""); 
     String to = "[email protected]"; 
     String manager = "[email protected]"; 
     String subject1 = "Maintenance Confirmation"; 


     MimeMessage msg = new MimeMessage(session); 
     MimeMessage msg1 = new MimeMessage(session); 


     try { 

      msg.setFrom(new InternetAddress(username)); 
      msg1.setFrom(new InternetAddress(username)); 
      msg.setRecipient(MimeMessage.RecipientType.TO, new InternetAddress(to)); 
      msg1.setRecipient(MimeMessage.RecipientType.TO, new InternetAddress(manager)); 
      msg.setSubject(subject1); 
      msg1.setSubject(s); 
      msg.setText("Some really important stuff. Confirmed."); 
      msg1.setText("Really important stuff needs attention"); 
      props.put("mail.smtps.auth", "true"); 
      props.put("mail.smtp.quitwait", "false"); 
      Transport t = session.getTransport("smtps"); 
      try { 
       t.connect(host, username, password); 
       t.sendMessage(msg, msg.getAllRecipients()); 
       t.sendMessage(msg1, msg1.getAllRecipients()); 
      } 
      finally { 
       t.close(); 
     } } 
     catch(Exception exc){ 
      exc.printStackTrace(); 
     } 
     return null; 
     }}; 

     myTask.execute(); } 

//method to run when button is clicked 
public void buttonclick3(View v) { 
    //first extract text for EditText and convert to String 
    EditText e = (EditText) findViewById(R.id.enterEmail); 
    String email = e.getText().toString(); 
    //run validateEmail on String and show alert if format is invalid 
    if (validateEmail(email) == false) { 
     AlertDialog.Builder builder = new AlertDialog.Builder(this); 
     builder.setMessage("Please enter a valid Email address."); 
     builder.setTitle("Invalid Input!"); 
     builder.setNeutralButton("OK", new DialogInterface.OnClickListener() { 

       @Override 
      public void onClick(DialogInterface dialog, int id) { 
        dialog.dismiss(); 
       } 
      }); 
    builder.show(); 
    } 
    else { 

     sendEmails(); 
    } 

    } 
    } 

這裏是我嘗試在任何地方撥打setSubject()時得到的LogCat。

- 04-14 17:52:09.091: W/dalvikvm(1510): threadid=1: thread exiting with 
    uncaught exception (group=0x41bbaa08) 
- 04-14 17:52:09.101: E/AndroidRuntime(1510): FATAL EXCEPTION: main 
- 04-14 17:52:09.101: E/AndroidRuntime(1510): java.lang.IllegalStateException: Could not execute method of the activity 
- 04-14 17:52:09.101: E/AndroidRuntime(1510): at android.view.View$1.onClick(View.java:3626) 
- 04-14 17:52:09.101: E/AndroidRuntime(1510): at android.view.View.performClick(View.java:4231) 
- 04-14 17:52:09.101: E/AndroidRuntime(1510): at android.view.View$PerformClick.run(View.java:17537) 
- 04-14 17:52:09.101: E/AndroidRuntime(1510): at android.os.Handler.handleCallback(Handler.java:725) 
- 04-14 17:52:09.101: E/AndroidRuntime(1510): at android.os.Handler.dispatchMessage(Handler.java:92) 
- 04-14 17:52:09.101: E/AndroidRuntime(1510): at android.os.Looper.loop(Looper.java:158) 
- 04-14 17:52:09.101: E/AndroidRuntime(1510): at android.app.ActivityThread.main(ActivityThread.java:5751) 
- 04-14 17:52:09.101: E/AndroidRuntime(1510): at java.lang.reflect.Method.invokeNative(Native Method) 
- 04-14 17:52:09.101: E/AndroidRuntime(1510): at java.lang.reflect.Method.invoke(Method.java:511) 
- 04-14 17:52:09.101: E/AndroidRuntime(1510): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1083) 
- 04-14 17:52:09.101: E/AndroidRuntime(1510): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:850) 
- 04-14 17:52:09.101: E/AndroidRuntime(1510): at dalvik.system.NativeStart.main(Native Method) 
- 04-14 17:52:09.101: E/AndroidRuntime(1510): Caused by: java.lang.reflect.InvocationTargetException 
- 04-14 17:52:09.101: E/AndroidRuntime(1510): at java.lang.reflect.Method.invokeNative(Native Method) 
- 04-14 17:52:09.101: E/AndroidRuntime(1510): at java.lang.reflect.Method.invoke(Method.java:511) 
- 04-14 17:52:09.101: E/AndroidRuntime(1510): at android.view.View$1.onClick(View.java:3621) 
- 04-14 17:52:09.101: E/AndroidRuntime(1510): ... 11 more 
- 04-14 17:52:09.101: E/AndroidRuntime(1510): Caused by: java.lang.NullPointerException 
- 04-14 17:52:09.101: E/AndroidRuntime(1510): at com.example.maintenanceapp.lastpage$1.setSubject(lastpage.java:81) 
- 04-14 17:52:09.101: E/AndroidRuntime(1510): at com.example.maintenanceapp.lastpage$1.<init>(lastpage.java:87) 
- 04-14 17:52:09.101: E/AndroidRuntime(1510): at com.example.maintenanceapp.lastpage.sendEmails(lastpage.java:72) 
+0

此代碼不會出現此錯誤。你不會調用'setSubject'方法。請至少編碼一致的代碼/錯誤日誌。 (同樣,格式化錯誤並指出哪一行是哪個) – njzk2

+0

這是導致錯誤的代碼。正上方doInBackground。 – STLCards77

+0

你在setSubject()的第81行有一個NullPointerException:可能是'main.isChecked()'? – naXa

回答

1

不要試圖找到不同的活動複選框。相反,發送用於啓動下一個活動的Intent中所需的信息。

+0

你是對的。今晚早些時候瞭解到這一點。 – STLCards77