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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 29, 20262026-05-29T10:50:04+00:00 2026-05-29T10:50:04+00:00

Here’s my code for a stopwatch application on Android: package com.google.labyrinth; public class Tabalicious

  • 0

Here’s my code for a stopwatch application on Android:

package com.google.labyrinth;

public class Tabalicious extends Activity implements OnClickListener, Runnable
{
    TextView    stopwatchCounter;

    long        startTime, stopTime;

    Thread      stopwatch;
    Button      buttonStart;
    Button      buttonStop;

    protected void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        setContentView( R.layout.tabalicious );


        stopwatchCounter    = (TextView) findViewById ( R.id.txtStopwatchCounter );
        buttonStart         = ( Button ) findViewById ( R.id.buttonStart );
        buttonStop          = ( Button ) findViewById ( R.id.buttonStop );

        buttonStart.setOnClickListener( this );

        buttonStop.setOnClickListener( this );
        buttonStop.setEnabled(false);

    }

    public void onClick(View viewClicked)
    {

        switch( viewClicked.getId() )
        {
        case R.id.buttonStart:

            this.buttonStart.setEnabled(false);
            this.buttonStop.setEnabled(true);

            this.startTime = System.currentTimeMillis();

            stopwatch = new Thread(this);
            stopwatch.start();

            break;

        case R.id.buttonStop:

                this.buttonStop.setEnabled(false);
                this.buttonStart.setEnabled(true);

            try {
                stopwatch.join();
            } catch (InterruptedException e) {
                e.printStackTrace();
            }

            break;


        }

    }

    public void run()
    {
                    //Update after 5 seconds.
            try {
                Thread.sleep(5000);
            } catch (InterruptedException e) {

                e.printStackTrace();
            }

            this.stopTime = System.currentTimeMillis();
            this.stopwatchCounter.setText( String.valueOf( (this.stopTime - this.startTime) ) ); //<- Debugger Info: This line is at the top of the call stack.
    }

}

The problem is that when I run the application I get a CalledFromWrongThread exception:

02-10 19:53:32.835: E/AndroidRuntime(18482): FATAL EXCEPTION:
Thread-10 02-10 19:53:32.835: E/AndroidRuntime(18482):

android.view.ViewRoot$CalledFromWrongThreadException: Only the
original thread that created a view hierarchy can touch its views.
02-10 19:53:32.835: E/AndroidRuntime(18482):

at android.view.ViewRoot.checkThread(ViewRoot.java:3092) 02-10
19:53:32.835: E/AndroidRuntime(18482):

at android.view.ViewRoot.invalidateChild(ViewRoot.java:677) 02-10
19:53:32.835: E/AndroidRuntime(18482):

at android.view.ViewRoot.invalidateChildInParent(ViewRoot.java:703)
02-10 19:53:32.835: E/AndroidRuntime(18482):

at android.view.ViewGroup.invalidateChild(ViewGroup.java:2597) 02-10
19:53:32.835: E/AndroidRuntime(18482):

at android.view.View.invalidate(View.java:5326) 02-10 19:53:32.835:
E/AndroidRuntime(18482):

at android.widget.TextView.checkForRelayout(TextView.java:5761) 02-10
19:53:32.835: E/AndroidRuntime(18482):

at android.widget.TextView.setText(TextView.java:2814) 02-10
19:53:32.835: E/AndroidRuntime(18482):

at android.widget.TextView.setText(TextView.java:2682) 02-10
19:53:32.835: E/AndroidRuntime(18482):

at android.widget.TextView.setText(TextView.java:2657) 02-10
19:53:32.835: E/AndroidRuntime(18482): at
com.google.labyrinth.Tabalicious.run(Tabalicious.java:144) 02-10
19:53:32.835: E/AndroidRuntime(18482): at
java.lang.Thread.run(Thread.java:1027)

I’m not quite clear on what’s going on. I would appreciate your help. thanks!

  • 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-29T10:50:05+00:00Added an answer on May 29, 2026 at 10:50 am

    You are trying to edit UI elements but you are not on the UI thread. In Android, UI elements can only be edited by the UI thread. There are many ways to update UI elements after doing computation on another thread, including calling the runOnUiThread() method, using a Handler, or overriding the PostExecute() method of an AsyncTask.

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

Sidebar

Related Questions

Here's my Manifest: <?xml version=1.0 encoding=utf-8?> <manifest xmlns:android=http://schemas.android.com/apk/res/android package=com.mappp.mobile android:versionCode=1 android:versionName=1.0 > <supports-screens android:largeScreens=true
Here is my code, which takes two version identifiers in the form 1, 5,
Here is my persistence.xml : <?xml version=1.0 encoding=UTF-8?> <persistence xmlns=http://java.sun.com/xml/ns/persistence xmlns:xsi=http://www.w3.org/2001/XMLSchema-instance xsi:schemaLocation=http://java.sun.com/xml/ns/persistence http://java.sun.com/xml/ns/persistence/persistence_1_0.xsd version=1.0>
Here is an example: I write html code inside of textarea, then I swap
here is my configuration: http://domain.com (obviously fictitious name...) hosted on a server running Apache
Here is my code...I have two dimensional matrices A,B. I want to develop the
Here is my code sample, let me know if it can be further improved?
Here is my code (Say we have a single button on the page that
here are 2 screen shots when i try to debug my code in visual
Here is my simplified data structure: Object1.h template <class T> class Object1 { private:

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.