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 6041507
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T06:40:17+00:00 2026-05-23T06:40:17+00:00

I think there is something that I’m just not yet getting about how these

  • 0

I think there is something that I’m just not yet getting about how these Views are to be coded. This is the third time I have had to come and post a question rearding them in the last 3 days! :S

Anyway, my problem is as follows.

edit:
This is the code inside a method that gets executed on a button press:
(Points of importance are noted)

>>  setContentView(R.layout.stop);
>>  timer = (TextView) findViewById(R.id.timer2);
    GPSMain.button = (Button) findViewById(R.id.stopbutton);

    startService(new Intent(context, Timer.class));

}

This is the Timer class that is executed in the “startService” call:
(Again points of importance are noted)

public class Timer extends Service {

static int totalSeconds = 0;
private int hour = 0;
private int min = 0;
private int sec = 0;
String mTimeFormat = "%02d:%02d:%02d";
final private Handler mHandler = new Handler();
static String timeTaken;
Context context = this;

Runnable mUpdateTime = new Runnable() {
    public void run() { updateTimeView(); }
};

@Override
public void onCreate() {

    Toast.makeText(context, "Timer Started", Toast.LENGTH_LONG).show();

    mHandler.postDelayed(mUpdateTime, 1000);
}

public void updateTimeView() {
    totalSeconds += 1;
    sec += 1;
    if(sec >= 60) {
        sec = 0;
        min += 1;
        if (min >= 60) {
            min = 0;
            hour += 1;
        }
    }
>>  timeTaken = String.format(mTimeFormat, hour, min, sec);
>>  GPSMain.timer.setText("Time Taken: "+timeTaken);
    mHandler.postDelayed(mUpdateTime, 1000);

}

@Override
public IBinder onBind(Intent intent) {
    // TODO Auto-generated method stub
    return null;
}
}

The logcat returns a NullPointerException on the following line of code:

GPSMain.timer.setText("Time Taken: "+timeTaken);

If I remove that line of code the code executes properly*, well I say properly because it executes all the way to the end of the application code, but the reason that I want to print the timer to the screen as it is counting is because I need to make sure that it is functioning correctly.

*not only does it run properly but it displays the timer text view with it’s default defined string from the xml. It’s only when I try to update it from the java that it crashes.

Here is the full xml file that the code is referencing at this point:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
<Button
    android:id="@+id/stopbutton"
    android:layout_width="100px" 
    android:layout_height="100px" 
    android:text="Stop"
    android:layout_centerInParent="true"
/>
<TextView  
    android:id="@+id/timer2"
    android:layout_width="fill_parent" 
    android:layout_height="wrap_content" 
    android:text="Time Taken:       unknown"
    android:layout_below="@+id/timer"
    />
</RelativeLayout>

UPDATE!

The MyTimer Class:

@SuppressWarnings("unchecked")
public class MyTimer extends AsyncTask { 

Timer _timerTask = new Timer();
static int totalSeconds = 0, hour = 0, min = 0, sec = 0;
static String mTimeFormat = "%02d:%02d:%02d";
static String timeTakenString;

@Override
protected Object doInBackground(Object... params) {

        TimerTask timer = new TimerTask() {

            @Override
            public void run() {

                totalSeconds += 1;
                sec += 1;
                if(sec >= 60) {
                    sec = 0;
                    min += 1;
                    if (min >= 60) {
                        min = 0;
                        hour += 1;
                    }
                }
                timeTakenString = String.format(mTimeFormat, hour, min, sec);
                GPSMain.timer.setText("Time Taken: "+GPSMain.timeTaken);

            }
        };
         (_timerTask).scheduleAtFixedRate(timer,1000,1000);

    return null;
}

}

The method to start the timer thread:

void startService(){

    setContentView(R.layout.stop);

    timer = (TextView) findViewById(R.id.timer2);

    GPSMain.button = (Button) findViewById(R.id.stopbutton);

    new MyTimer().execute();

}
  • 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-23T06:40:18+00:00Added an answer on May 23, 2026 at 6:40 am

    You can use TimerTask instead of Service . Here is the code ..

    TimerTask timer = new TimerTask() {
    
                @Override
                public void run() {
    
                            // Here you can update your UI
        }
     }
    
    Timer _timerTask = new Timer();
            _timerTask.scheduleAtFixedRate(timer,60000,60000);
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

This is something that I think would be very useful. Basically, I'd like there
I think there must be something subtle going on here that I don't know
I'm new to Entity Framework, and I'm think there is something that I misunderstand
So I'm learning jQuery and I think there is something that I am missing.
Is there such a thing? I'm talking about something like a C++ new command
I think there is a simple answer to this, but for some reason I
This is an accident and I don't think there's a solution. But I'm asking
EDIT : I just found something that works on every page load except the
The following query finds the directors of all shops. I think that there is
I recall that there was this one method that could piece together an image,

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.