我在尝试使用javaMail android发送电子邮件时收到此错误:
01-23 10:25:23.493: E/AndroidRuntime(20707): Caused by: java.lang.NoClassDefFoundError: tk.yteditors.london2013.ConfirmActivity$1
01-23 10:25:23.493: E/AndroidRuntime(20707): at tk.yteditors.london2013.ConfirmActivity.sendMail(ConfirmActivity.java:124)
01-23 10:25:23.493: E/AndroidRuntime(20707): at tk.yteditors.london2013.ConfirmActivity.send(ConfirmActivity.java:97)
在出现错误之前,还会抛出此错误:
01-23 10:25:20.753: E/dalvikvm(20707): Could not find class 'tk.yteditors.london2013.ConfirmActivity$1', referenced from method tk.yteditors.london2013.ConfirmActivity.sendMail
有人能解释发生了什么吗?
请注意,我在错误日志放置错误的两行上放置了注释。另请注意,我已使用无效数据替换了电子邮件服务器信息。
和另一个错误日志:
01-23 10:55:36.232: E/AndroidRuntime(21404): FATAL EXCEPTION: main
01-23 10:55:36.232: E/AndroidRuntime(21404): java.lang.IllegalStateException: Could not execute method of the activity
01-23 10:55:36.232: E/AndroidRuntime(21404): at android.view.View$1.onClick(View.java:3599)
01-23 10:55:36.232: E/AndroidRuntime(21404): at android.view.View.performClick(View.java:4204)
01-23 10:55:36.232: E/AndroidRuntime(21404): at android.view.View$PerformClick.run(View.java:17355)
01-23 10:55:36.232: E/AndroidRuntime(21404): at android.os.Handler.handleCallback(Handler.java:725)
01-23 10:55:36.232: E/AndroidRuntime(21404): at android.os.Handler.dispatchMessage(Handler.java:92)
01-23 10:55:36.232: E/AndroidRuntime(21404): at android.os.Looper.loop(Looper.java:137)
01-23 10:55:36.232: E/AndroidRuntime(21404): at android.app.ActivityThread.main(ActivityThread.java:5041)
01-23 10:55:36.232: E/AndroidRuntime(21404): at java.lang.reflect.Method.invokeNative(Native Method)
01-23 10:55:36.232: E/AndroidRuntime(21404): at java.lang.reflect.Method.invoke(Method.java:511)
01-23 10:55:36.232: E/AndroidRuntime(21404): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
01-23 10:55:36.232: E/AndroidRuntime(21404): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)
01-23 10:55:36.232: E/AndroidRuntime(21404): at dalvik.system.NativeStart.main(Native Method)
01-23 10:55:36.232: E/AndroidRuntime(21404): Caused by: java.lang.reflect.InvocationTargetException
01-23 10:55:36.232: E/AndroidRuntime(21404): at java.lang.reflect.Method.invokeNative(Native Method)
01-23 10:55:36.232: E/AndroidRuntime(21404): at java.lang.reflect.Method.invoke(Method.java:511)
01-23 10:55:36.232: E/AndroidRuntime(21404): at android.view.View$1.onClick(View.java:3594)
01-23 10:55:36.232: E/AndroidRuntime(21404): ... 11 more
01-23 10:55:36.232: E/AndroidRuntime(21404): Caused by: java.lang.NoClassDefFoundError: tk.yteditors.london2013.ConfirmActivity$1
01-23 10:55:36.232: E/AndroidRuntime(21404): at tk.yteditors.london2013.ConfirmActivity.sendMail(ConfirmActivity.java:118)
01-23 10:55:36.232: E/AndroidRuntime(21404): at tk.yteditors.london2013.ConfirmActivity.send(ConfirmActivity.java:90)
01-23 10:55:36.232: E/AndroidRuntime(21404): ... 14 more
代码:
package tk.yteditors.london2013;
import java.net.InetAddress;
import java.util.Properties;
import javax.activation.DataHandler;
import javax.mail.Address;
import javax.mail.Message;
import javax.mail.Multipart;
import javax.mail.PasswordAuthentication;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;
import javax.mail.internet.MimeMultipart;
import javax.mail.util.ByteArrayDataSource;
import tk.yteditors.london2013.app.App;
import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.webkit.WebSettings.LayoutAlgorithm;
import android.widget.TextView;
import android.widget.Toast;
import android.annotation.TargetApi;
import android.content.Intent;
import android.graphics.Color;
import android.os.Build;
public class ConfirmActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_confirm);
setupActionBar();
WebView webView = (WebView)findViewById(R.id.confirmWebView);
Bundle extras = getIntent().getExtras();
webView.loadUrl(String.valueOf(extras.getString("localAdress")));
webView.setBackgroundColor(Color.BLUE);
webView.setPadding(0, 0, 0, 0);
webView.getSettings().setLayoutAlgorithm(LayoutAlgorithm.SINGLE_COLUMN);
webView.getSettings();
webView.setWebViewClient(new WebViewClient());
webView.setHorizontalScrollBarEnabled(false);
webView.getSettings().setBuiltInZoomControls(true);
if(extras.getBoolean("zoomClose", false)){
webView.getSettings().setDefaultZoom(WebSettings.ZoomDensity.CLOSE);
webView.getSettings().setUseWideViewPort(true);
}
webView.getSettings().setSupportZoom(true);
TextView view = (TextView) findViewById(R.id.confirmAdress);
view.setText(extras.getString("webAdress"));
}
@TargetApi(Build.VERSION_CODES.HONEYCOMB)
private void setupActionBar() {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB) {
getActionBar().setDisplayHomeAsUpEnabled(true);
}
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case R.id.MenuSettings:
startActivity(new Intent(this, SettingActivity.class));
return true;
}
return super.onOptionsItemSelected(item);
}
public void delete(View view){
deleteFile(getIntent().getExtras().getString("fileName"));
Toast t = Toast.makeText(App.context, "Deleting file...", Toast.LENGTH_SHORT);
startActivity(new Intent(this, SettingActivity.class));
}
public void send(View view){
try{
sendMail(); //line 97
} catch (Exception e) {
e.printStackTrace();
}finally{
delete(view);
}
}
public void sendMail() throws Exception{
String host = "smtp.server.com";
String emailAddress = "noEmail@email.com";
final String from = emailAddress;
final String pass = "IncorrectPassword";
String to = emailAddress;
Multipart multiPart;
String finalString = "";
Properties props = System.getProperties();
props.put("mail.smtp.starttls.enable", "true");
props.put("mail.smtp.host", host);
props.put("mail.smtp.user", emailAddress);
props.put("mail.smtp.password", pass);
props.put("mail.smtp.port", "587");
props.put("mail.smtp.auth", "true");
Session s = Session.getDefaultInstance(props, new javax.mail.Authenticator(){ //line 124
protected PasswordAuthentication getPasswordAuthentication(){
return new PasswordAuthentication(from, pass);
}
});
DataHandler handler=new DataHandler(new ByteArrayDataSource(finalString.getBytes(),"text/plain" ));
MimeMessage message = new MimeMessage(s);
message.setFrom(new InternetAddress(from));
message.setDataHandler(handler);
multiPart = new MimeMultipart();
InternetAddress toAddress = new InternetAddress(to);
message.addRecipient(Message.RecipientType.TO, toAddress);
message.setSubject("Antwoord van: " +getIntent().getExtras().getString("name"));
message.setContent(multiPart);
message.setText("Check bijlage");
Transport t = s.getTransport("smtp");
t.connect(host, emailAddress, pass);
t.sendMessage(message, message.getAllRecipients());
t.close();
}
}
非常感谢!
我也尝试过使用getInstance(props)和getDefaultInstance(props)和getDefaultInstance(props,null);他们不工作。
PS。不要介意我非常糟糕的英语...
答案 0 :(得分:0)
ClassNotFoundException
。 NoClassDefFoundError' happens when class cannot be instantiated by
new`因为jvm无法初始化类。它可能是构造函数抛出异常,或静态块抛出,或版本不匹配。
答案 1 :(得分:0)
这个getPasswordAuthentication()
的定义不正确:
public PasswordAuthentication getPasswordAuthentication() {
return new PasswordAuthentication(username, password);
}
即public
可见度?这就是我用它的方式。
您的IDE是否在Anonymous Authenticator类中显示任何错误? $1
中的tk.yteditors.london2013.ConfirmActivity$1
表示该类无法初始化。