java.lang.Runtimeexception: Can’t create Handler Inside thread that has not called Looper.prepare();

Asked

Viewed 2,228 times

0

What I must do to solve this problem ?

  private void callServer(final String method, final String data){

        new Thread(){

            public void run(){
               asw = Connection.getSetDataWeb("http://192.168.1.20/renan/process.php", method, data);

                if (data.isEmpty()){
                    degenerateJson(asw);
               }

                if(asw.equals("3")){
                    makeText(MainActivity.this, "Código da igreja inesistente, por favor escreva um código válido.", LENGTH_SHORT).show();
                }else if (asw.equals("2")){
                    makeText(MainActivity.this, "Senha incorreto, por favor escreva uma senha válida.", LENGTH_SHORT).show();
                }else if (asw.equals("1"))
                {
                    makeText(MainActivity.this, "E-mail incorreto, por favor escreva um email válido.", LENGTH_SHORT).show();
                }else{
                    Intent it = new Intent(MainActivity.this, MC_Home.class);
                    it.putExtra("email", email.getText().toString());
                    it.putExtra("cod_igrej", cod_igrej.getText().toString());
                    startActivity(it);
                }
            }
        }.start();
       }

O Logcat:

05-22 14:33:48.578  28186-28305/com.example.hotsystems.hs_celulas E/AndroidRuntime﹕ FATAL EXCEPTION: Thread-121
    Process: com.example.hotsystems.hs_celulas, PID: 28186
    android.view.ViewRootImpl$CalledFromWrongThreadException: Only the original thread that created a view hierarchy can touch its views.
            at android.view.ViewRootImpl.checkThread(ViewRootImpl.java:6094)
            at android.view.ViewRootImpl.requestLayout(ViewRootImpl.java:824)
            at android.view.View.requestLayout(View.java:16431)
            at android.view.View.requestLayout(View.java:16431)
            at android.view.View.requestLayout(View.java:16431)
            at android.view.View.requestLayout(View.java:16431)
            at android.view.View.requestLayout(View.java:16431)
            at android.view.View.requestLayout(View.java:16431)
            at android.view.View.requestLayout(View.java:16431)
            at android.view.View.requestLayout(View.java:16431)
            at android.view.View.requestLayout(View.java:16431)
            at android.widget.TextView.checkForRelayout(TextView.java:6594)
            at android.widget.TextView.setText(TextView.java:3813)
            at android.widget.TextView.setText(TextView.java:3671)
            at android.widget.TextView.setText(TextView.java:3646)
            at android.app.ProgressDialog.setMessage(ProgressDialog.java:316)
            at com.example.hotsystems.hs_celulas.MainActivity$3.run(MainActivity.java:140)

Using Asynctaks, however I have not yet tested, this giving error when calling another Activity, if you have everything correct.:

package com.example.hotsystems.hs_celulas;

import android.app.Progressdialog; import android.content.Context; import android.content.Intent; import android.os.Asynctask;

public class Activity_login extends Asynctask { private context; private progressidialog progress;

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

protected void onPreExecute(){
    ProgressDialog progresso = new ProgressDialog(context);
    progresso.setMessage("Aguarde...");
    progresso.show();
}
protected String doInBackground(String... asw){
    if(asw.equals("3"))
        progresso.setMessage("Código da igreja inesistente, por favor escreva um código válido.");
    else if (asw.equals("2")){
        progresso.setMessage("Senha incorreto, por favor escreva uma senha válida.");
        // makeText(MainActivity.this, "Senha incorreto, por favor escreva uma senha válida.", LENGTH_SHORT).show();
    }else if (asw.equals("1"))
    {
        progresso.setMessage("E-mail incorreto, por favor escreva um email válido.");
        // makeText(MainActivity.this, "E-mail incorreto, por favor escreva um email válido.", LENGTH_SHORT).show();
    }

    return null;
}
protected void onProgressUpdate(){

}
protected void onPostExecute(){
    progresso.setMessage("Seja Bem vindo.");
    progresso.show();

    Intent it = new Intent(context, MC_Home.class);
    startActivity(it);
    progresso.dismiss();

}

}

  • Renan, you can’t change the View's out of MainThread. In that case you should wear one AsyncTask.

  • how the implementation would look ?

  • I recommend taking a look at this question: http://answall.com/questions/10416/como-utilizar-asynctask-no-android

  • I saw more I’m not so, not working out the way I need to request from the server the information, for this reason Asynctaks this making me difficult, for example I need to check what was typed wrong, whether it was the email, the password or even the company code.

No answers

Browser other questions tagged

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