0

I want to display a timer from 1 to 100 in my TextView, but this throws me an Exception. My application throws a RuntimeException like shown below:

Thanks in advance for your help.

java.lang.RuntimeException: An error occurred while executing doInBackground()

My MainActivity class where the RuntimeException is thrown.

public class MainActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        TextView txt = findViewById(R.id.txt);
        Button button1 = findViewById(R.id.bt1);
        Button button2 = findViewById(R.id.bt2);
        button1.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                new QQ().execute();
            }
        });
    }


    public void onProgressUpdate(int i) {
        TextView txt = findViewById(R.id.txt);
        Button button1 = findViewById(R.id.bt1);
        Button button2 = findViewById(R.id.bt2);
        txt.setText(i);
    }

    private class QQ extends AsyncTask<Integer, Integer, Integer> {

        @Override
        protected void onPreExecute() {
            super.onPreExecute();
        }

        protected Integer doInBackground(Integer... args) {
            for (int i = 0; i < 1000000; i++) {

                try {
                    Thread.sleep(1000); ///задержка
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
                publishProgress(i);
                return null;
            }
            return null;
        }

        private void publishProgress(int i) {
            publishProgressq(i);
        }

        protected void onPostExecute(int i) {

        }
    }

    private void publishProgressq(int i) {
        onProgressUpdate(i);
    }
}

My Layout class in xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="#bbbbbb"
    android:orientation="vertical">

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:background="#bbbbbb"
        android:gravity="top"
        android:orientation="horizontal">

        <TextView
            android:id="@+id/txt"
            android:layout_width="151dp"
            android:layout_height="110dp"
            android:textSize="45dp"
            android:text="0">
        </TextView>
        <Button

            android:id="@+id/bt1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:onClick="onClick"
            android:text="START" />

        <Button
            android:id="@+id/bt2"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:onClick="onClick"
            android:text="STOP" />
    </LinearLayout>
</LinearLayout>
Marcin Orlowski
  • 72,056
  • 11
  • 123
  • 141
вава
  • 9
  • 1

2 Answers2

1

You can change your code as below:

public void onProgressUpdate(int i) {
    runOnUiThread(new Runnable() {
        @Override
        public void run() {
            TextView txt = findViewById(R.id.txt);
            Button button1 = findViewById(R.id.bt1);
            Button button2 = findViewById(R.id.bt2);
            txt.setText(i);
        }
    });
}

It will run on main thread.

Pratik Butani
  • 60,504
  • 58
  • 273
  • 437
  • 1
    [`onProgressUpdate()`](https://developer.android.com/reference/android/os/AsyncTask#onProgressUpdate(Progress...)) already runs on the UI thread. – Mike M. Dec 21 '19 at 18:22
1

Inside your public void onProgressUpdate , set text like this :

txt.setText(String.valueOf(i));

If you set a TextView to an int, it will be interpreted as an Android resource id. If you want the value of the int as your text (and not the resource it points to), make it a String first.

intellignt_idiot
  • 1,962
  • 2
  • 16
  • 23