Nullpointerexception on login screen

Asked

Viewed 140 times

1

I have this Nullpointerexception error and I don’t know pq. I have only one login screen, one dao that takes two parameters, email and password:

java.lang.NullPointerException
at android.content.ContextWrapper.getPackageName(ContextWrapper.java:135)
at android.content.ComponentName.<init>(ComponentName.java:75)
at android.content.Intent.<init>(Intent.java:3662)
at br.com.caixadesujestoes.dao.Dao.Logar(Dao.java:47)
at br.com.caixadesujestoes.visao.Activity_Login$1.onClick(Activity_Login.java:94)
at android.view.View.performClick(View.java:4240)
at android.view.View$PerformClick.run(View.java:17721)
at android.os.Handler.handleCallback(Handler.java:730)
at android.os.Handler.dispatchMessage(Handler.java:92)
at android.os.Looper.loop(Looper.java:137)
at android.app.ActivityThread.main(ActivityThread.java:5103)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:525)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:737)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
at dalvik.system.NativeStart.main(Native Method)
eglSurfaceAttrib not implemented

Activity:

public void onCreate(Bundle bundle){
    super.onCreate(bundle);
    setContentView(R.layout.layout_login);

    final Dao dao = new Dao(this);

    // Resolvendo o erro NetworkOnMainThreadException utilizando ActionBar na versao min do SDK 11
    StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder().permitAll().build();
    StrictMode.setThreadPolicy(policy);

    ActionBar ab = getActionBar();
    ab.setTitle("Login");

    email       = (EditText)findViewById(R.id.editEmail);
    senha       = (EditText)findViewById(R.id.editSenhaUsuario);

    entrar = (Button)   findViewById(R.id.btnEntrar);
    entrar.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View arg0) {

            if(email.getText().toString().length() <= 0){
                email.setError("Campo Email não pode ser vazio");
                email.requestFocus();
            }else if (senha.getText().toString().length() <= 0){
                    senha.setError("Campo Senha não pode ser vazio");
                    senha.requestFocus();
                }else{

                    Email = email.getText().toString();
                    Senha = senha.getText().toString();
                    dao.Logar(Email, Senha);

                }
        }
    });

Dao:

public class Dao extends Activity {

Context context;
Funcoes funcoes = new Funcoes();

public Dao(Context context){
    this.context = context;
}

public void Logar(String email, String senha){
    String urlPost = "http://192.168.1.105/tccbimbati/android/login.php";
    ArrayList<NameValuePair> pPost = new ArrayList<NameValuePair>();
    pPost.add(new BasicNameValuePair("email", email));
    pPost.add(new BasicNameValuePair("senha", senha));

    String respostaRetornada = null;
    Log.i("Entrou", "Vai entrar no try");

    try{
        respostaRetornada = ConexaoHttpClient.executaHttpPost(urlPost, pPost);
        Log.i("Entrou", "Entrou");
        String resposta = respostaRetornada.toString();
        resposta = resposta.replaceAll("\\s+", "");

        Log.i("Logar", "Resposta: "+resposta);

        if (resposta.equals("1")){
            Log.i("Email", "Email: "+email);
            Log.i("Senha", "Senha: "+senha);

            Intent intent = new Intent(this, PrincipalActivity.class);
            intent.putExtra("email", email);
            intent.putExtra("senha", senha);
            startActivity(intent);
        }else{
            Toast.makeText(context, "Usuário e ou senha inválida", Toast.LENGTH_LONG).show();
        }

    }catch(Exception erro){
        erro.printStackTrace();
        funcoes.mensagemSimples(context, "Erro", "Erro: "+erro);
    }
}

}

1 answer

3


The problem is on this line:

Intent intent = new Intent(this, PrincipalActivity.class)

Like you’re not inside a Activity, then the this ends up being your object Dao and not the context of Activity passed by the builder.

To work you have to use context:

Intent intent = new Intent(this.context, PrincipalActivity.class)
  • 1

    Good, it worked... Thank you

Browser other questions tagged

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