Android System Login Error (Maketoast)

Asked

Viewed 30 times

1

I am creating a system Login with json on android but when I will present a message by maketoast it gives an error:

Process: Imm.pt.videoclub, PID: 11853 java.lang.Runtimeexception: Can’t 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.videoclub.VideoClube.showMessage(Videoclub.java:66) at Imm.pt.videoclub.VideoClube.Verifyanswer(Videoclub.java:49) At Imm.pt.videoclub.VideoClube.access$000(Videoclub.java:17) At Imm.pt.videoclub.Videoclub$1.run(Videoclub.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);

    }
}

1 answer

1


When you’re inside a Thread other than UI Thread, you need to invoke the elements that will work with the UI in UI Thread.

In these cases we use the method runOnUiThread.

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

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

A complete example using your method callServer, 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();
}
  • 1

    Thanks for the help!

Browser other questions tagged

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