Login Error on an Android System (MakeToast) [duplicate]

1

I'm creating a login system with json on android but when I'm going to submit a message through maketoast it gives an error:

  

Process: imm.pt.videoclube, PID: 11853                                                                      java.lang.RuntimeException: Can not create handler inside thread that   has not called Looper.prepare ()                                                                          at android.os.Handler (Handler.java:200)                                                                          at android.os.handler. (Handler.java:114)                                                                          at android.app.Dialog. (Dialog.java:152)                                                                          at android.app.Dialog. (Dialog.java:202)                                                                          at   android.support.v7.app.AppCompatDialog. (AppCompatDialog.java:46)                                                                          at android.support.v7.app.AlertDialog (AlertDialog.java:97)                                                                          at   android.support.v7.app.AlertDialog $ Builder.create (AlertDialog.java:932)                                                                          at imm.pt.videoclube.VideoClube.showMessage (VideoClube.java:66)                                                                          at imm.pt.videoclube.VideoClube.VerifyAnswer (VideoClube.java:49)                                                                          at imm.pt.videoclube.VideoClube.access $ 000 (VideoClube.java:17)                                                                          at imm.pt.videoclube.VideoClube $ 1.run (VideoClube.java:43)

Code:

package imm.pt.videoclube;

import android.app.Application;
import android.content.DialogInterface;
import android.content.Intent;
import android.support.v7.app.AlertDialog;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

import org.json.JSONException;
import org.json.JSONObject;

public class VideoClube extends AppCompatActivity {
    Button btn;
    EditText usr,psw;


    public void entrar(View view){
        usr = (EditText) findViewById(R.id.usr);
        psw = (EditText) findViewById(R.id.psw);
        String json = generateJSON();
        callServer("veruser", json);

    }

    private String generateJSON(){
        JSONObject jo = new JSONObject();
        try{
            jo.put("user", usr.getText().toString());
            jo.put("pass", psw.getText().toString());
        }
        catch (JSONException e){e.printStackTrace();}
        return(jo.toString());
    }
    private void callServer(final String method, final String data){
        new Thread(){
            public void run(){
                String answer = HttpConnection.getSetDataWeb("http://192.168.1.71/video/app/process.php", method, data);
                VerifyAnswer(answer);
            }
        }.start();
    }
    private void VerifyAnswer(String answer){
        if(answer.equals("N")){
            showMessage("Dados Invalidos!");
        }else{
            showMessage("Bem-vindo!");
            Intent inicial = new Intent(VideoClube.this, VideoClubeStart.class);
            startActivity(inicial);
        }
    }
    private void showMessage(String msg) {
        Toast.makeText(getApplicationContext(), msg, Toast.LENGTH_SHORT).show();
    }
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_video_clube);

    }
}
    
asked by anonymous 06.03.2018 / 23:01

1 answer

1

When you are inside a Thread other than UI Thread , you need to invoke the elements that will work with UI on UI Thread .

In these cases we use the runOnUiThread method.

In your case, just call the VerifyAnswer method inside runOnUiThread :

runOnUiThread(new Runnable() {
   @Override
   public void run() {
       VerifyAnswer(answer);
   }
});

A complete example using your callServer method would look like this:

private void callServer(final String method, final String data){
    new Thread(){
        public void run(){
            String answer = HttpConnection.getSetDataWeb("http://192.168.1.71/video/app/process.php", method, data);    

            runOnUiThread(new Runnable() {
              @Override
              public void run() {
                 VerifyAnswer(answer);
              }
            });
        }
    }.start();
}
    
06.03.2018 / 23:06