Error while displaying message

Asked

Viewed 31 times

0

 public void iniciar() {

    Thread thread = new Thread(new Runnable() {

        @Override
        public void run() {

            Socket socket = null;
            DataOutputStream dataOutputStream = null;
            DataInputStream dataInputStream = null;

            try {
                socket = new Socket(ip, 10000);
                dataOutputStream = new DataOutputStream(socket.getOutputStream());
                dataInputStream = new DataInputStream(socket.getInputStream());
                dataOutputStream.writeUTF(valor);
                valorLido = dataInputStream.readLine();
                if (valorLido.equals("0")){
                    Toast.makeText(MainActivity.this, "Ambiente escuro! Lâmpada deve ser acesa!", Toast.LENGTH_LONG).show();
                    imageView = (ImageView) findViewById(R.id.imageView_Lampada);
                    imageView.setImageResource(R.drawable.lampada_acesa);
                }else{
                    Toast.makeText(MainActivity.this, "Ambiente claro! Lâmpada deve ser apagada!", Toast.LENGTH_LONG).show();
                    imageView = (ImageView) findViewById(R.id.imageView_Lampada);
                    imageView.setImageResource(R.drawable.lampada_apagada);
                }
            } catch (Exception e) {
                e.printStackTrace();
            } finally {
                try {
                    if (socket != null) {
                        socket.close();
                    }
                    if (dataOutputStream != null) {
                        dataOutputStream.close();
                    }
                    if (dataInputStream != null) {
                        dataInputStream.close();
                    }
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        }
    });

    thread.start();

}

Does anyone know why they are making a mistake when running Toast.makeText?

  • 1

    Could post error log?

  • 1

    Add: It looks like you’re trying to post a Toast still inside the Worker Thread. Android doesn’t let you do anything that affects the user interface from within any thread other than the main thread (UI Thread). If you want to post a Toast, return to Ui thread using runOnUiThread(runnable)

1 answer

1

Try to use:

        runOnUiThread(new Runnable() {
        @Override
        public void run() {
            if (valorLido.equals("0")){
                Toast.makeText(MainActivity.this, "Ambiente escuro! Lâmpada deve ser acesa!", Toast.LENGTH_LONG).show();
                imageView = (ImageView) findViewById(R.id.imageView_Lampada);
                imageView.setImageResource(R.drawable.lampada_acesa);
            }else{
                Toast.makeText(MainActivity.this, "Ambiente claro! Lâmpada deve ser apagada!", Toast.LENGTH_LONG).show();
                imageView = (ImageView) findViewById(R.id.imageView_Lampada);
                imageView.setImageResource(R.drawable.lampada_apagada);
            }
        }
    });

Android won’t let you make changes to the view if you’re not on Main Thread.

Browser other questions tagged

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