Display problem of a setText

Asked

Viewed 242 times

0

I am creating an application, which starts on one screen, and automatically (within "n" seconds), opens another Activity.

I decided to perform some tests with load display (loading), but I came across the following situation: Includes two Textview, and one of them was to display modifications of a value that decreases over n seconds. I can do the normal print in Log. e, but Textview does not change.

Can anyone tell me why?

Java code:

package luiscarlos.tccluis;

import android.annotation.SuppressLint;
import android.app.ProgressDialog;
import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

public class TelaInicial extends AppCompatActivity {

    int n=6; // tempo maximo de permanencia da tela
    private TextView cont;
    //private ProgressDialog progresso;
    int contador = 0; //contador de tempo
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_tela_inicial);
        //progresso = new ProgressDialog(this);
        cont = (TextView) findViewById(R.id.contador);
        ContadorDeTempo();
    }

    public void ContadorDeTempo()
    {
        //progresso.setMessage(a);
        //progresso.show();
        setContentView(R.layout.activity_tela_inicial); //recortado do metodo acima. Ajuste de conteudo para a tela inicial
        new Thread(new Runnable() {
            @Override
            public void run() {
                contador++;
                try
                {
                    while(contador == 1 || contador <= n)
                    {
                        Thread.sleep(1000);
                        contador++;
                        String a = Integer.toString(n-contador);
                        cont.setText(a);  // aonde deveria setar o valor de "a"

                        Log.e("Contador = ", Integer.toString(contador));
                        Log.e("contadora = ", a);

                    }
                }
                catch (InterruptedException e)
                {
                    e.printStackTrace();
                }
                if (contador == n+1)
                {
                    Intent intent = new Intent(TelaInicial.this, TelaLogin.class); // chamada da tela seguinte
                    startActivity(intent);
                    contador++;
                }
            }
        }).start(); // inserido para iniciar a troca de activities
    }


    @Override
    protected void onPause() {
        // TODO Auto-generated method stub
        super.onPause();
        finish(); // evita que essa activity seja chamada novamente
    }
}

Xml code.

<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context="luiscarlos.tccluis.TelaInicial">


    <TextView
        android:id="@+id/textView22"
        android:layout_width="181dp"
        android:layout_height="50dp"
        android:layout_marginBottom="8dp"
        android:layout_marginLeft="8dp"
        android:layout_marginRight="8dp"
        android:layout_marginTop="8dp"
        android:gravity="center_vertical|center_horizontal"
        android:text="Logotipo"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toRightOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        tools:layout_editor_absoluteY="215dp" />

    <TextView
        android:id="@+id/contador"
        android:layout_width="98dp"
        android:layout_height="41dp"
        android:layout_marginBottom="8dp"
        android:layout_marginLeft="8dp"
        android:layout_marginRight="8dp"
        android:layout_marginTop="16dp"
        android:gravity="center"
        android:text="mudar"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toRightOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/textView22"
        app:layout_constraintVertical_bias="0.763" />

</android.support.constraint.ConstraintLayout>
  • I get it. I’ll try. Thank you :)

1 answer

1

The reason is that it is not allowed to use objects that use the UI, as is the case Textview, in a Thread other than the Uithread(Mainthread).

In the method run() of Thread use the method runOnUiThread() to place a Runnable in Uithread.

new Thread() {
    public void run() {

        ....
        ....
        runOnUiThread(new Runnable() {

            @Override
            public void run() {

                //Coloque aqui o código que necessita de correr
                //na UI thread
                String a = Integer.toString(n-contador);
                cont.setText(a);  // aonde deveria setar o valor de "a"
            }
        });

    }
}.start();

Another problem that the code has is to "set" again the layout of Activity in the method ContadorDeTempo().
In doing so, the attribute cont, initialized in onCreate(), does not reference the existing Textview of now layout of Activity.

Eliminate the line setContentView(R.layout.activity_tela_inicial); that is in the method ContadorDeTempo().

Browser other questions tagged

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