Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7043673
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T02:19:07+00:00 2026-05-28T02:19:07+00:00

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

  • 0

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

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-28T02:19:07+00:00Added an answer on May 28, 2026 at 2:19 am

    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

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have two activities one displaying map view and another listview, the main objective
I have two activities. One activity has the main game and the other activity
Ive got two activities in the same application. the first one is with gui(main
I have two activities A and B, and from activity A, I click on
I have an application that contains two Activities with <intent-filter> <action android:name=android.intent.action.MAIN/> <category android:name=android.intent.category.LAUNCHER/>
I have two activities: one displays an image and a button, the other displays
I have two activities in my Android 2.1 application. The first is the Main
In my application I have two activities. In the first activity I have a
i have two activities within an ActivityGroup, one ProvinceActivity and another CityActivity. so code:
I have an Android Honeycomb application with two activities; the first one has a

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.