I'm creating an application, which starts on a screen, and automatically (within "n" seconds), opens another activity.
I then decided to perform load-loading tests, but I came across the following situation: It included two TextViews, and one was to display changes of a value that decreases over n seconds. I can make the normal print in Log.e, but TextView does not change.
Can anyone tell me the reason?
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>