android : 从 onResume() 调用 class
android : calling class from onResume()
我正在尝试从 activity 的 onResume() 调用 class。但是每次我都收到 class not found 错误。
这是 onResume() 的代码:
@Override
protected void onResume() {
super.onResume();
try {
sendHiddenMail sender = new sendHiddenMail("sidh*****@gmail.com", "pass");
sender.sendMail("This is Subject",
"This is Body",
"sid*****@gmail.com",
"sid*****@gmail.com");
} catch (Exception e) {
Log.e("SendMailsdfsdfsd", e.getMessage(), e);
}
}
这里是class我想给谁打电话:
public class sendHiddenMail extends javax.mail.Authenticator{
private String mailhost = "smtp.gmail.com";
private String user;
private String password;
private Session session;
static {
Security.addProvider(new com.provider.JSSEProvider());
}
public sendHiddenMail(String user, String password) {
this.user = user;
this.password = password;
Properties props = new Properties();
props.setProperty("mail.transport.protocol", "smtp");
props.setProperty("mail.host", mailhost);
props.put("mail.smtp.auth", "true");
props.put("mail.smtp.port", "465");
props.put("mail.smtp.socketFactory.port", "465");
props.put("mail.smtp.socketFactory.class",
"javax.net.ssl.SSLSocketFactory");
props.put("mail.smtp.socketFactory.fallback", "false");
props.setProperty("mail.smtp.quitwait", "false");
session = Session.getDefaultInstance(props, this);
}
protected PasswordAuthentication getPasswordAuthentication() {
return new PasswordAuthentication(user, password);
}
public synchronized void sendMail(String subject, String body, String sender, String recipients) throws Exception {
try{
MimeMessage message = new MimeMessage(session);
DataHandler handler = new DataHandler(new ByteArrayDataSource(body.getBytes(), "text/plain"));
message.setSender(new InternetAddress(sender));
message.setSubject(subject);
message.setDataHandler(handler);
if (recipients.indexOf(',') > 0)
message.setRecipients(Message.RecipientType.TO, InternetAddress.parse(recipients));
else
message.setRecipient(Message.RecipientType.TO, new InternetAddress(recipients));
Transport.send(message);
}catch(Exception e){
}
}
public class ByteArrayDataSource implements DataSource {
private byte[] data;
private String type;
public ByteArrayDataSource(byte[] data, String type) {
super();
this.data = data;
this.type = type;
}
public ByteArrayDataSource(byte[] data) {
super();
this.data = data;
}
public void setType(String type) {
this.type = type;
}
public String getContentType() {
if (type == null)
return "application/octet-stream";
else
return type;
}
public InputStream getInputStream() throws IOException {
return new ByteArrayInputStream(data);
}
public String getName() {
return "ByteArrayDataSource";
}
public OutputStream getOutputStream() throws IOException {
throw new IOException("Not Supported");
}
}
}
这里是 logcat:
06-03 03:22:57.331: W/dalvikvm(4934): Unable to resolve superclass of Lcom/example/calllist/sendHiddenMail; (67)
06-03 03:22:57.331: W/dalvikvm(4934): Link of class 'Lcom/example/calllist/sendHiddenMail;' failed
06-03 03:22:57.331: E/dalvikvm(4934): Could not find class 'com.example.calllist.sendHiddenMail', referenced from method com.example.calllist.MainActivity.sendMail
06-03 03:22:57.331: W/dalvikvm(4934): VFY: unable to resolve new-instance 34 (Lcom/example/calllist/sendHiddenMail;) in Lcom/example/calllist/MainActivity;
06-03 03:22:57.331: D/dalvikvm(4934): VFY: replacing opcode 0x22 at 0x0000
06-03 03:22:57.341: W/dalvikvm(4934): Unable to resolve superclass of Lcom/example/calllist/sendHiddenMail; (67)
06-03 03:22:57.341: W/dalvikvm(4934): Link of class 'Lcom/example/calllist/sendHiddenMail;' failed
06-03 03:22:57.401: D/dalvikvm(4934): DexOpt: unable to opt direct call 0x0039 at 0x06 in Lcom/example/calllist/MainActivity;.sendMail
06-03 03:22:57.791: D/AndroidRuntime(4934): Shutting down VM
06-03 03:22:57.791: W/dalvikvm(4934): threadid=1: thread exiting with uncaught exception (group=0xb1af6b90)
06-03 03:22:57.811: E/AndroidRuntime(4934): FATAL EXCEPTION: main
06-03 03:22:57.811: E/AndroidRuntime(4934): Process: com.example.calllist, PID: 4934
06-03 03:22:57.811: E/AndroidRuntime(4934): java.lang.NoClassDefFoundError: com.example.calllist.sendHiddenMail
06-03 03:22:57.811: E/AndroidRuntime(4934): at com.example.calllist.MainActivity.sendMail(MainActivity.java:102)
06-03 03:22:57.811: E/AndroidRuntime(4934): at com.example.calllist.MainActivity.getCallDetails(MainActivity.java:81)
06-03 03:22:57.811: E/AndroidRuntime(4934): at com.example.calllist.MainActivity.onResume(MainActivity.java:35)
06-03 03:22:57.811: E/AndroidRuntime(4934): at android.app.Instrumentation.callActivityOnResume(Instrumentation.java:1192)
06-03 03:22:57.811: E/AndroidRuntime(4934): at android.app.Activity.performResume(Activity.java:5322)
06-03 03:22:57.811: E/AndroidRuntime(4934): at android.app.ActivityThread.performResumeActivity(ActivityThread.java:2759)
06-03 03:22:57.811: E/AndroidRuntime(4934): at android.app.ActivityThread.handleResumeActivity(ActivityThread.java:2798)
06-03 03:22:57.811: E/AndroidRuntime(4934): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2231)
06-03 03:22:57.811: E/AndroidRuntime(4934): at android.app.ActivityThread.access0(ActivityThread.java:135)
06-03 03:22:57.811: E/AndroidRuntime(4934): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1397)
06-03 03:22:57.811: E/AndroidRuntime(4934): at android.os.Handler.dispatchMessage(Handler.java:102)
06-03 03:22:57.811: E/AndroidRuntime(4934): at android.os.Looper.loop(Looper.java:137)
06-03 03:22:57.811: E/AndroidRuntime(4934): at android.app.ActivityThread.main(ActivityThread.java:4998)
06-03 03:22:57.811: E/AndroidRuntime(4934): at java.lang.reflect.Method.invokeNative(Native Method)
06-03 03:22:57.811: E/AndroidRuntime(4934): at java.lang.reflect.Method.invoke(Method.java:515)
06-03 03:22:57.811: E/AndroidRuntime(4934): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:777)
06-03 03:22:57.811: E/AndroidRuntime(4934): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:593)
06-03 03:22:57.811: E/AndroidRuntime(4934): at dalvik.system.NativeStart.main(Native Method)
06-03 03:27:58.391: I/Process(4934): Sending signal. PID: 4934 SIG: 9
我还没有在 mainfest.xml
中添加任何关于 sendHiddenMail class 的信息
我不知道我哪里错了。
当您正确导入 javax.mail.Authenticator
时,在 "compile" 时没有错误,应用程序在运行时崩溃,解决方案可能是 this link。
是的,如果您删除 class 关系,您的代码可能会工作,但不能解决您的问题。尝试清理并重建项目。
我正在尝试从 activity 的 onResume() 调用 class。但是每次我都收到 class not found 错误。
这是 onResume() 的代码:
@Override
protected void onResume() {
super.onResume();
try {
sendHiddenMail sender = new sendHiddenMail("sidh*****@gmail.com", "pass");
sender.sendMail("This is Subject",
"This is Body",
"sid*****@gmail.com",
"sid*****@gmail.com");
} catch (Exception e) {
Log.e("SendMailsdfsdfsd", e.getMessage(), e);
}
}
这里是class我想给谁打电话:
public class sendHiddenMail extends javax.mail.Authenticator{
private String mailhost = "smtp.gmail.com";
private String user;
private String password;
private Session session;
static {
Security.addProvider(new com.provider.JSSEProvider());
}
public sendHiddenMail(String user, String password) {
this.user = user;
this.password = password;
Properties props = new Properties();
props.setProperty("mail.transport.protocol", "smtp");
props.setProperty("mail.host", mailhost);
props.put("mail.smtp.auth", "true");
props.put("mail.smtp.port", "465");
props.put("mail.smtp.socketFactory.port", "465");
props.put("mail.smtp.socketFactory.class",
"javax.net.ssl.SSLSocketFactory");
props.put("mail.smtp.socketFactory.fallback", "false");
props.setProperty("mail.smtp.quitwait", "false");
session = Session.getDefaultInstance(props, this);
}
protected PasswordAuthentication getPasswordAuthentication() {
return new PasswordAuthentication(user, password);
}
public synchronized void sendMail(String subject, String body, String sender, String recipients) throws Exception {
try{
MimeMessage message = new MimeMessage(session);
DataHandler handler = new DataHandler(new ByteArrayDataSource(body.getBytes(), "text/plain"));
message.setSender(new InternetAddress(sender));
message.setSubject(subject);
message.setDataHandler(handler);
if (recipients.indexOf(',') > 0)
message.setRecipients(Message.RecipientType.TO, InternetAddress.parse(recipients));
else
message.setRecipient(Message.RecipientType.TO, new InternetAddress(recipients));
Transport.send(message);
}catch(Exception e){
}
}
public class ByteArrayDataSource implements DataSource {
private byte[] data;
private String type;
public ByteArrayDataSource(byte[] data, String type) {
super();
this.data = data;
this.type = type;
}
public ByteArrayDataSource(byte[] data) {
super();
this.data = data;
}
public void setType(String type) {
this.type = type;
}
public String getContentType() {
if (type == null)
return "application/octet-stream";
else
return type;
}
public InputStream getInputStream() throws IOException {
return new ByteArrayInputStream(data);
}
public String getName() {
return "ByteArrayDataSource";
}
public OutputStream getOutputStream() throws IOException {
throw new IOException("Not Supported");
}
}
}
这里是 logcat:
06-03 03:22:57.331: W/dalvikvm(4934): Unable to resolve superclass of Lcom/example/calllist/sendHiddenMail; (67)
06-03 03:22:57.331: W/dalvikvm(4934): Link of class 'Lcom/example/calllist/sendHiddenMail;' failed
06-03 03:22:57.331: E/dalvikvm(4934): Could not find class 'com.example.calllist.sendHiddenMail', referenced from method com.example.calllist.MainActivity.sendMail
06-03 03:22:57.331: W/dalvikvm(4934): VFY: unable to resolve new-instance 34 (Lcom/example/calllist/sendHiddenMail;) in Lcom/example/calllist/MainActivity;
06-03 03:22:57.331: D/dalvikvm(4934): VFY: replacing opcode 0x22 at 0x0000
06-03 03:22:57.341: W/dalvikvm(4934): Unable to resolve superclass of Lcom/example/calllist/sendHiddenMail; (67)
06-03 03:22:57.341: W/dalvikvm(4934): Link of class 'Lcom/example/calllist/sendHiddenMail;' failed
06-03 03:22:57.401: D/dalvikvm(4934): DexOpt: unable to opt direct call 0x0039 at 0x06 in Lcom/example/calllist/MainActivity;.sendMail
06-03 03:22:57.791: D/AndroidRuntime(4934): Shutting down VM
06-03 03:22:57.791: W/dalvikvm(4934): threadid=1: thread exiting with uncaught exception (group=0xb1af6b90)
06-03 03:22:57.811: E/AndroidRuntime(4934): FATAL EXCEPTION: main
06-03 03:22:57.811: E/AndroidRuntime(4934): Process: com.example.calllist, PID: 4934
06-03 03:22:57.811: E/AndroidRuntime(4934): java.lang.NoClassDefFoundError: com.example.calllist.sendHiddenMail
06-03 03:22:57.811: E/AndroidRuntime(4934): at com.example.calllist.MainActivity.sendMail(MainActivity.java:102)
06-03 03:22:57.811: E/AndroidRuntime(4934): at com.example.calllist.MainActivity.getCallDetails(MainActivity.java:81)
06-03 03:22:57.811: E/AndroidRuntime(4934): at com.example.calllist.MainActivity.onResume(MainActivity.java:35)
06-03 03:22:57.811: E/AndroidRuntime(4934): at android.app.Instrumentation.callActivityOnResume(Instrumentation.java:1192)
06-03 03:22:57.811: E/AndroidRuntime(4934): at android.app.Activity.performResume(Activity.java:5322)
06-03 03:22:57.811: E/AndroidRuntime(4934): at android.app.ActivityThread.performResumeActivity(ActivityThread.java:2759)
06-03 03:22:57.811: E/AndroidRuntime(4934): at android.app.ActivityThread.handleResumeActivity(ActivityThread.java:2798)
06-03 03:22:57.811: E/AndroidRuntime(4934): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2231)
06-03 03:22:57.811: E/AndroidRuntime(4934): at android.app.ActivityThread.access0(ActivityThread.java:135)
06-03 03:22:57.811: E/AndroidRuntime(4934): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1397)
06-03 03:22:57.811: E/AndroidRuntime(4934): at android.os.Handler.dispatchMessage(Handler.java:102)
06-03 03:22:57.811: E/AndroidRuntime(4934): at android.os.Looper.loop(Looper.java:137)
06-03 03:22:57.811: E/AndroidRuntime(4934): at android.app.ActivityThread.main(ActivityThread.java:4998)
06-03 03:22:57.811: E/AndroidRuntime(4934): at java.lang.reflect.Method.invokeNative(Native Method)
06-03 03:22:57.811: E/AndroidRuntime(4934): at java.lang.reflect.Method.invoke(Method.java:515)
06-03 03:22:57.811: E/AndroidRuntime(4934): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:777)
06-03 03:22:57.811: E/AndroidRuntime(4934): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:593)
06-03 03:22:57.811: E/AndroidRuntime(4934): at dalvik.system.NativeStart.main(Native Method)
06-03 03:27:58.391: I/Process(4934): Sending signal. PID: 4934 SIG: 9
我还没有在 mainfest.xml
中添加任何关于 sendHiddenMail class 的信息我不知道我哪里错了。
当您正确导入 javax.mail.Authenticator
时,在 "compile" 时没有错误,应用程序在运行时崩溃,解决方案可能是 this link。
是的,如果您删除 class 关系,您的代码可能会工作,但不能解决您的问题。尝试清理并重建项目。