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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T12:00:36+00:00 2026-06-13T12:00:36+00:00

I am trying to develop a custom stop watch application in android but it

  • 0

I am trying to develop a custom stop watch application in android but it throw following exception:

android.view.ViewRoot$CalledFromWrongThreadException: Only the original thread that created a view hierarchy can touch its views.

I have also tried to use chronometer but when we use it’s stop method it starts from its previous value.

following is my activity file

public class MainActivity extends Activity implements OnClickListener
{

TextView sec,milisec,min;
Button start,stop,pause,resume;
int count=0;
Chronometer chrono;
Timer timer;

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

    chrono=(Chronometer)findViewById(R.id.chronometer);
    milisec=(TextView)findViewById(R.id.milisec);
    sec=(TextView)findViewById(R.id.sec);
    min=(TextView)findViewById(R.id.min);
    start=(Button)findViewById(R.id.start);
    stop=(Button)findViewById(R.id.stop);
    pause=(Button)findViewById(R.id.pause);
    resume=(Button)findViewById(R.id.resume);


    System.out.println("-------1-------");

    start.setOnClickListener(new OnClickListener() 
    {
        @Override
        public void onClick(View v) 
        {
            //chrono.start();

        //count=Integer.parseInt(count_view.getText().toString());*/
        timer = new Timer();
        timer.schedule(new TimerTask()
        {
           public void run()
           {
               System.out.println("-------2-------");
               View v = new View(getApplicationContext());
               count_increment(v);
            }
          },1000,10000);

    }   

    });

    stop.setOnClickListener(new OnClickListener() 
    {
        @Override
        public void onClick(View v) 
        {
            //chrono.clearComposingText();
            //chrono.setText("00");
            timer.cancel();

        }
    });
}

public void count_increment(View v)
{
    count++;
    System.out.println("count="+count);
    //count_str=String.valueOf(count);
    milisec.setText(""+count);

}

@Override
public void onClick(View v) {
    // TODO Auto-generated method stub

}



}

and following is xml file

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent" >

<LinearLayout
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:layout_alignParentLeft="true"
    android:layout_alignParentTop="true"
    android:layout_marginTop="30dp"
    android:gravity="center_horizontal" >

    <TextView
        android:id="@+id/min"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="00" />

    <TextView
        android:id="@+id/textView2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text=":" />

    <TextView
        android:id="@+id/sec"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="00" />

    <TextView
        android:id="@+id/textView4"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text=":" />

    <TextView
        android:id="@+id/milisec"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="00" />
</LinearLayout>

<LinearLayout
    android:id="@+id/linearLayout1"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:layout_alignParentLeft="true"
    android:layout_alignParentTop="true"
    android:layout_marginTop="60dp"
    android:gravity="center_horizontal" >

    <TableLayout
        android:layout_width="wrap_content"
        android:layout_height="match_parent" >

        <TableRow
            android:id="@+id/tableRow1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content" >

            <Button
                android:id="@+id/start"
                android:layout_width="80dp"
                android:layout_height="wrap_content"
                android:text="Start" />

            <Button
                android:id="@+id/stop"
                android:layout_width="80dp"
                android:layout_height="wrap_content"
                android:text="Stop" />

            <Button
                android:id="@+id/pause"
                android:layout_width="80dp"
                android:layout_height="wrap_content"
                android:text="Pause" />

            <Button
                android:id="@+id/resume"
                android:layout_width="80dp"
                android:layout_height="wrap_content"
                android:text="Resume" />
        </TableRow>

        <TableRow
            android:id="@+id/tableRow1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content" >

            <Chronometer
                android:id="@+id/chronometer"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="Chronometer" />

        </TableRow>
    </TableLayout>
</LinearLayout>

</RelativeLayout>

What is going wrong?
Is there any other way than this to develop stopwatch?

  • 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-06-13T12:00:37+00:00Added an answer on June 13, 2026 at 12:00 pm

    The error message is quite self-explained: you shall not touch the UI element directly in a new thread. A possible workaround is that you post a runnable to the UI thread to update the UI by Activity.runOnUiThread()

    I mean something like this (pseudo code not tested):

    public void count_increment(View v)
    {
        count++;
        System.out.println("count="+count);
        //count_str=String.valueOf(count);
        // milisec.setText(""+count);
        final String str = ""+count;
        runOnUiThread(new Runnable() {
            public void run() {
                milisec.setText(str);
            }
        });
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am trying to develop a custom movie player in iPhone using AVPlayer. But
I'm trying to develop a custom generator for Yeoman using CoffeeScript but I'm facing
I am trying to develop a custom table through coding in Android. I am
I'm trying to develop a custom launcher but based on the google's Launcher2, I
I'm trying to develop a custom control for asp.net that will have the following
I am trying to develop a custom gallery like application in which, i would
I'm trying to develop a custom spring namespace that works like the built-in 'p'
I'm playing with maven plugins, specifically i'm trying to develop a custom maven plugin
I am trying to develop an application for my localhost on which I can
I am trying to develop an application to forward received SMS to a web

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.