Why can’t I instantiate an object by clicking my button?

Asked

Viewed 211 times

0

The operation of the button is correct, it executes all previous lines. However, when 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)
  • 5

    You can also report the error occurring on Logcat?

  • Okay, it’s been edited!

  • Line 53: Email email = new Email();

  • This class of Apache for sending email you are using, it needs both the activation.jar and mail.jar package Javax. You included them in your project?

  • I received a help that is needed Asynctask

1 answer

1


Daniela,

I ran your code to test and realized that you have problems when authenticating.

1 - Check if the email actually has is "[email protected]" because I believe it should be "[email protected]", without the ". br" at the end.

2 - Sign in to your Google account from connectionfor free 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 authorize less secure apps to access your account, log in to your account and access the link: https://www.google.com/settings/security/lesssecureapps then click on activate.

More information on: https://support.google.com/accounts/answer/6010255

To authorize access this link: https://support.google.com/accounts/answer/6010255

3- Example of test code 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);
}

}

Browser other questions tagged

You are not signed in. Login or sign up in order to post.