質問

The code runs and my emails send, but anytime I check a box the subject is not altered because the method setSubject() is not called. Got it. Where I'm having trouble is where would I call/place this method so I can use the subject in my doInBackground() method. If I try calling setSubject() right before or anywhere in doInBackground I get an error for running concurrent tasks on the same thread. I tried calling setSubject() in a onPreExecute() block and still got the error. Anybody have any ideas? Here is the code and LogCat.

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 = "user@gmail.com";
    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 = "to@yahoo.com";
      String manager = "manager@gmail.com";
      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();
    }

    }
    }

Here is the LogCat I get when I try calling setSubject() anywhere.

 - 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)
役に立ちましたか?

解決

Don't try to find checkboxes on a different activity. Instead, send the information needed in the Intent used to start the next activity.

ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top