Why can not I instantiate an object when I click my button?

0

Button operation is correct, it executes all previous rows. However, instantiating an object Email displays the error.

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());
}

Class Email :

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;
/**
 * Created by daniela on 23/03/15.
 */
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("[email protected]", "senha"));
    }
    /**
     * Recebe feedbacks enviados pelo usuario e encaminha por email
     * @param comments
     * @param getIluminacao
     * @param getWifi
     * @param getStars
     */
    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("[email protected]");
            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("[email protected]");
            email.send();
        } catch (EmailException e) {
            System.err.println("Erro ao enviar email.");
        }
    }
}

Error

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)
    
asked by anonymous 24.03.2015 / 12:30

1 answer

1

Daniela,

I ran your code to test and I realized that you have problems when it's time to authenticate.

1 - Verify that the email has really is "[email protected]" because I believe it should be "[email protected]", without the ".br" at the end.

2 - Access your google account from the connects module and create an authorization for less secure applications to access your email, because by default your google accounts are blocked for access by other applications.

To allow less secure apps to access your account, sign in to your account and visit the link link. then click on Activate.

More information at: link

To authorize access to this link: link

3- Test code sample working (Tested by me).

    import org.apache.commons.mail.DefaultAuthenticator;
    import org.apache.commons.mail.EmailException;
    import org.apache.commons.mail.SimpleEmail;
    /**
     * Created by daniela on 23/03/15.
     */

    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("[email protected]", "suasenha"));

}
/**
 * Recebe feedbacks enviados pelo usuario e encaminha por email
 * @param comments
 * @param getIluminacao
 * @param getWifi
 * @param getStars
 */
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("[email protected]");
        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");
        email.addTo("[email protected]");
        email.send();
    } catch (EmailException e) {
        System.err.println("Erro ao enviar email.");
    }
}

public static void main(String args[]) {
    Email email = new Email();
    email.sendEmail("Comments", true,
            true, (float) 9.99);
}

}

    
02.04.2015 / 15:15