Question

I have two activities, one is main and I have another activity called Test as well

in the main activity i have a button when I click on this button I go to the Test activity

in the Test activity I want the application to automatically append some text to the textview that it has every 10 seconds

this text can be for example "test" so every 10 seconds "test" will be added but, when I leave the activity and go to the main activity, this process will continue happening so if for example I spend 20 seconds on the main activity and then return to the Test activity, I will see two more "test" strings in the textview..

I tried doing this with timertask and had no success at all

this is the Test activity's code, in the main activity I just have the button that shows me the other activity

public class test2 extends Activity {
    /** Called when the activity is first created. */

    private TimerTask task;
    private Handler handler;
    private TextView tv;
    private Timer t;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.text1);

        t = new Timer();
        task = new TimerTask() {
            public void run() {
                handler.post(new Runnable() {
                    public void run() {
                        TextView tv = (TextView) findViewById(R.id.tv1);
                        tv.append("test");

                    }
                });

            }
        };
        t.schedule(task, 2000);
    }
}

when I click in the button of the main activity in order to go to the Test activity I get the following error:

                FATAL EXCEPTION: Timer-0
                java.lang.NullPointerException

thanks in advance

Was it helpful?

Solution

If you want to implement a way of updating the view from the background, I would recommend switching to use a Service instead, with a timer possibly periodically sending Broadcasts, which the main activity can access with a BroadcastReceiver. The TimerTask could be implemented on the start of the background service.

If you want to immediately update the test class when it loads, you could implement an "update" BroadcastReceiver on the service as well, whose function would be to send another broadcast with the updated value.

This way, you wouldn't need to worry about your Test activity getting paused while you switch between activities (if that is what you're going for).

EDIT :

Here is a pretty good tutorial that should have everything you need:

http://www.vogella.de/articles/AndroidServices/article.html

OTHER TIPS

without the knowledge where the application crashes (i.e. the line of code where the nullPointerException is thrown) all answers will be not very precise.

Here's what I think about your problem:

you call findViewById(R.id.tv1). this returns the correct view if and only if your activity is active AND your layout does provide an textview with id tv1. I assume that your xml layout file is correct. I think it may have to do something that you call this method out of a Timer which does not have to do anything with your application. try it with getApplication().findViewById(R.id.tv1). This will ensure that findViewById searches in the right activity where you set your xml layout.

An activity has its own lifecycle, see here. This will later cause problems since your activity might get killed and the update process will fail. As John pointed out, Service or AlarmManager is the thing you should use. Always make sure that you do not wake up the cpu too often since this will cause battery drains.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top