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?
Could post error log?
– Lennoard Silva
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)
– Lennoard Silva