Call Activity after thread is executed

Asked

Viewed 185 times

1

I have an app that logs a teacher into a webservice and right after logging in it inserts the user into the android database. If the result is different from null, I want you to call the Activity menu. However 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();


}
  • usually closes so because it gave some error, via rule some exception that was not picked up, checks the logcat, will probably have a stacktrace there

  • My question is whether you can call the other Activity inside runOnUiThread(new Runnable() { @Override public void run() {

  • But have you tried it? Did you make a mistake? As far as I know run() already wheel within its "main" thead, then it should work.

  • Yes, you made the following mistake

  • ActivityThread.handleLaunchActivit

  • Does Progressdialog.show work? I always use the dialog variable show method. Check if it works. You may also need to place the show inside a separate thread. Then you use runOnUiThread.

Show 1 more comment

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 don’t forget to register the Activity there in your manifest.

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

Browser other questions tagged

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