Call activity after the thread is executed

1

I have an app that logs in to a teacher in a webservice and after logging in, it inserts the user into the android database. If the result is other than null, I want it to call the activity menu. But my application is closing. Follow the code.

public class Login extends Activity {

ProfessorWS professorWS = new ProfessorWS();
Professor professor = new Professor();
ProfessorDAO professorDAO = new ProfessorDAO(this);
private EditText edUsuario, edSenha;
private Handler handler = new Handler();
Intent intent = new Intent(this,Menu.class);


@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_login);
    edUsuario = (EditText) findViewById(R.id.edUsuario);
    edSenha = (EditText) findViewById(R.id.edSenha);

}

public void btEntrarOnClick (View view){


    String msg = getString(R.string.dlg_msg);
    String titulo = getString(R.string.dlg_titulo);

    final ProgressDialog dialog = ProgressDialog.show(this, titulo, msg);

    new Thread(new Runnable() {
        @Override
        public void run() {
            try {
                LinkedHashMap<String, Object> params = new LinkedHashMap<String, Object>();
                params.put("user", edUsuario.getText().toString());
                params.put("senha", edSenha.getText().toString());

                professor = professorWS.buscarProfessor(params);
                professorDAO.insereProfessor(professor);

               handler.post(new Runnable() {
                    @Override
                    public void run() {

                        if (professor.getCodProfessor() != null){ //
                            Toast t = Toast.makeText(getBaseContext(), "Código pro: " + professor.getCodProfessor(), Toast.LENGTH_SHORT);
                            t.show();


                        }else{
                            Toast t = Toast.makeText(getBaseContext(), "Código professor nao encotrado " , Toast.LENGTH_SHORT);
                            t.show();
                        }
                    }
                });
            } catch (Exception e) {
            } finally {
                dialog.dismiss();
            }
        }
    }).start();


}
    
asked by anonymous 08.09.2016 / 18:36

1 answer

1

Just do it this way below:

if (professor.getCodProfessor() != null){ 
     Intent intent = new Intent(Login.this,Menu.class);
     startActivity(intent);
}else{
     Toast.makeText(Login.this, "Código professor nao encotrado ", Toast.LENGTH_SHORT).show();
}

And do not forget to register Activity la in your manifest.

<activity
     android:name=".Menu"
     android:label="@string/menu_title">
</activity>
    
08.09.2016 / 19:41