0

The goal of my program is pretty simple. It is to display an integer that adds two to itself every second.
Since, some of the answers get updated by latest methods, I have tried many, alas, none of them work. I have tried Timer, Handler the while loop, executor etc. With timer I get Timer-0 error, Executor runs only once, and Handler wants me to declare the auto-incremented variable to be Final. I do want the number to be incremented for an hour or endlessly if possible. I am showing one of the options I have tried here.

public class stats extends AppCompatActivity {


    TextView textView2;
    TextView textView1;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_stats);
        textView2 = findViewById(R.id.textView2);

        final int total = 30000; // the total number
//...
//when you want to start the counting start the thread bellow.
        new Thread(new Runnable() {
            int counter = 0;
            public void run() {
                while (counter < total) {
                    try {
                        Thread.sleep(1000);
                    } catch (InterruptedException e) {
                        // TODO Auto-generated catch block
                        e.printStackTrace();
                    }
                    textView1.post(new Runnable() {

                        public void run() {
                            textView1 = findViewById(R.id.textView1);
                            textView1.setText("" + counter);

                        }

                    });
                    counter += 2;
                }

            }

        }).start();
    }

and here is the error

java.lang.NullPointerException: Attempt to invoke virtual method 'boolean android.widget.TextView.post(java.lang.Runnable)' on a null object reference. Now, this error is for the line textView1.post(New runnable)

Could there be a more simpler approach to the problem?

2 Answers2

3

Your texview is null because you have not intialised it in onCreate method.

add:

textView1 = findViewById(R.id.textView1);
Hilary Mwape
  • 425
  • 1
  • 4
  • 15
0

You forget to initialize textView1.

You must initialize that first like you do in textView2.

It should look like this.

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_stats);
    textView1 = findViewById(R.id.textView1);
    textView2 = findViewById(R.id.textView2);

    final int total = 30000; // the total number
    //...
    //when you want to start the counting start the thread bellow.
    new Thread(new Runnable() {
        int counter = 0;
        public void run() {
            while (counter < total) {
                try {
                    Thread.sleep(1000);
                } catch (InterruptedException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
                textView1.post(new Runnable() {

                    public void run() {

                        textView1.setText("" + counter);

                    }

                });
                counter += 2;
            }

        }

    }).start();
}