为什么不能通过单击我的按钮来实例化对象?

时间:2015-03-24 11:33:06

标签: java android button

操作按钮正确,它执行所有之前的行。但是,实例化email对象时会显示错误。

   public void send(View view){
    EditText text = (EditText) findViewById(R.id.editText);
    Switch iluminacao = (Switch) findViewById(R.id.switch1);
    Switch wifi = (Switch) findViewById(R.id.switch2);
    RatingBar mBar = (RatingBar) findViewById(R.id.ratingBar);
    System.out.println(text.getText().toString() + iluminacao.isChecked() +
                       wifi.isChecked() + mBar.getNumStars());

    Email email = new Email();
    email.sendEmail(text.getText().toString(), iluminacao.isChecked(),
                    wifi.isChecked(), mBar.getNumStars());
}

Email.Java

package com.morais.daniela.conectasp;
import android.os.Build;
import org.apache.commons.mail.DefaultAuthenticator;
import org.apache.commons.mail.EmailException;
import org.apache.commons.mail.SimpleEmail;

public class Email {
private SimpleEmail email = new SimpleEmail();
//FIXME
//Adicionar email correto para onde deverá ser enviado!
public Email() {
    email.setSSLOnConnect(true);
    email.setHostName("smtp.gmail.com");
    email.setSslSmtpPort("465");
    email.setAuthenticator(new DefaultAuthenticator("conectasaopaulo@gmail.com.br","senha"));
}

public void sendEmail(String comments, boolean getIluminacao, boolean getWifi, float getStars){
    String iluminacao = "Não.";
    String wifi = "Não.";
    if(getIluminacao==true) {
        iluminacao = "Sim.";
    }
    if(getWifi==true) {
        wifi = "Sim.";
    }
    try {
        email.setFrom("conectasaopaulo@gmail.com.br");
        email.setDebug(true);
        email.setSubject("Feedback - ConectaSP");
        email.setMsg("Houve problemas com a iluminação? " + iluminacao + "\n\n"
                     + "Houve problemas com o wifi? " + wifi + "\n\n"
                     + "Avaliação geral " + getStars + "\n\n"
                     + "Comentários:\n" + comments + "\n\n"
                     + "Informações gerais do aparelho\n" + Build.DEVICE + "\t" + Build.HARDWARE + "\t" + Build.MODEL);
        email.addTo("dani.m@hotmail.co.uk");
        email.send();
    } catch (EmailException e) {
        System.err.println("Erro ao enviar email.");
    }
}
}

错误

03-24 12:37:36.277    4037-4037/com.morais.daniela.conectasp E/AndroidRuntime﹕ FATAL EXCEPTION: main
    java.lang.IllegalStateException: Could not execute method of the activity
            at android.view.View$1.onClick(View.java:3103)
            at android.view.View.performClick(View.java:3574)
            at android.view.View$PerformClick.run(View.java:14293)
            at android.os.Handler.handleCallback(Handler.java:605)
            at android.os.Handler.dispatchMessage(Handler.java:92)
            at android.os.Looper.loop(Looper.java:137)
            at android.app.ActivityThread.main(ActivityThread.java:4448)
            at java.lang.reflect.Method.invokeNative(Native Method)
            at java.lang.reflect.Method.invoke(Method.java:511)
            at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:823)
            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:590)
            at dalvik.system.NativeStart.main(Native Method)
     Caused by: java.lang.reflect.InvocationTargetException
            at java.lang.reflect.Method.invokeNative(Native Method)
            at java.lang.reflect.Method.invoke(Method.java:511)
            at android.view.View$1.onClick(View.java:3098)
            at android.view.View.performClick(View.java:3574)
            at android.view.View$PerformClick.run(View.java:14293)
            at android.os.Handler.handleCallback(Handler.java:605)
            at android.os.Handler.dispatchMessage(Handler.java:92)
            at android.os.Looper.loop(Looper.java:137)
            at android.app.ActivityThread.main(ActivityThread.java:4448)
            at java.lang.reflect.Method.invokeNative(Native Method)
            at java.lang.reflect.Method.invoke(Method.java:511)
            at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:823)
            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:590)
            at dalvik.system.NativeStart.main(Native Method)
     Caused by: java.lang.VerifyError: com/morais/daniela/conectasp/Email
            at com.morais.daniela.conectasp.FeedbackActivity.send(FeedbackActivity.java:53)
            at java.lang.reflect.Method.invokeNative(Native Method)
            at java.lang.reflect.Method.invoke(Method.java:511)
            at android.view.View$1.onClick(View.java:3098)
            at android.view.View.performClick(View.java:3574)
            at android.view.View$PerformClick.run(View.java:14293)
            at android.os.Handler.handleCallback(Handler.java:605)
            at android.os.Handler.dispatchMessage(Handler.java:92)
            at android.os.Looper.loop(Looper.java:137)
            at android.app.ActivityThread.main(ActivityThread.java:4448)
            at java.lang.reflect.Method.invokeNative(Native Method)
            at java.lang.reflect.Method.invoke(Method.java:511)
            at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:823)
            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:590)
            at dalvik.system.NativeStart.main(Native Method)

1 个答案:

答案 0 :(得分:0)

请检查以下代码:

 private SimpleEmail email = new SimpleEmail();
    //FIXME
    //Adicionar email correto para onde deverá ser enviado!
    public Email() {

if(email==null)
email = new SimpleEmail();

email.setSSLOnConnect(true);
email.setHostName("smtp.gmail.com");
email.setSslSmtpPort("465");
email.setAuthenticator(new DefaultAuthenticator("conectasaopaulo@gmail.com.br","senha"));
}

并检查清单文件中有关电子邮件的权限。