代码会运行并且我的电子邮件会发送,但是每当我选中一个复选框时,主题就不会更改,因为未调用方法setSubject()
。得到它了。我遇到麻烦的地方是在哪里调用/放置此方法,以便可以在doInBackground()
方法中使用该主题。如果尝试在setSubject()
之前或任何地方调用doInBackground
,则会在同一线程上运行并发任务时出错。我尝试在setSubject()
块中调用onPreExecute()
,但仍然收到错误。有人有什么想法吗?这是代码和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 = "[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)
最佳答案
不要试图在其他活动上找到复选框。而是发送用于启动下一个活动的Intent中所需的信息。