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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 6, 20262026-06-06T14:19:28+00:00 2026-06-06T14:19:28+00:00

I’m making an app which reads the data from a website every interval and

  • 0

I’m making an app which reads the data from a website every interval and posting a value to the screen. I’m having difficulty with making it start and stopping it.

I can make the loop start no problem and I can stop it as well. But if I try and start it again, the program crashes.

The code is here:

    final Timer myTimer = new Timer(); 


    loop.setOnClickListener(new View.OnClickListener() {

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

            MyTimerTask mytimertask = new MyTimerTask();
            myTimer.scheduleAtFixedRate(mytimertask, 0, 5000);
        }
    });

    stoploop.setOnClickListener(new View.OnClickListener() {

        public void onClick(View v) {
            // TODO Auto-generated method stub
            textView.setText("STOPPED");
            myTimer.cancel();

        }
    });


}

private class MyTimerTask extends TimerTask {
    @Override
    public void run() {
        // TODO Auto-generated method stub
        runOnUiThread(new Runnable(){

            public void run() {
                // TODO Auto-generated method stub
                //textView.setText("changed");
                readWebpage(text);
            }

        });

    }

}

I’m quite new to threads which is why I’m probably missing something fundamental here. Any help would be greatly appreciated.

Thank you

[edit] I should add, I adapted this from a solution here:
How do I repeat a method every 10 minutes after a button press and end it on another button press

   06-29 01:38:02.958: D/AndroidRuntime(1119): Shutting down VM
   06-29 01:38:02.958: W/dalvikvm(1119): threadid=1: thread exiting with uncaught exception    (group=0x40a13300)
   06-29 01:38:02.968: E/AndroidRuntime(1119): FATAL EXCEPTION: main
   06-29 01:38:02.968: E/AndroidRuntime(1119): java.lang.IllegalStateException: Timer was canceled
   06-29 01:38:02.968: E/AndroidRuntime(1119):  at    java.util.Timer.scheduleImpl(Timer.java:561)
   06-29 01:38:02.968: E/AndroidRuntime(1119):  at    java.util.Timer.scheduleAtFixedRate(Timer.java:528)
   06-29 01:38:02.968: E/AndroidRuntime(1119):  at com.example.beam.MainActivity$1.onClick(MainActivity.java:67)
   06-29 01:38:02.968: E/AndroidRuntime(1119):  at android.view.View.performClick(View.java:4084)
   06-29 01:38:02.968: E/AndroidRuntime(1119):  at android.view.View$PerformClick.run(View.java:16966)
   06-29 01:38:02.968: E/AndroidRuntime(1119):  at android.os.Handler.handleCallback(Handler.java:615)
   06-29 01:38:02.968: E/AndroidRuntime(1119):  at android.os.Handler.dispatchMessage(Handler.java:92)
   06-29 01:38:02.968: E/AndroidRuntime(1119):  at android.os.Looper.loop(Looper.java:137)
   06-29 01:38:02.968: E/AndroidRuntime(1119):  at android.app.ActivityThread.main(ActivityThread.java:4745)
   06-29 01:38:02.968: E/AndroidRuntime(1119):  at java.lang.reflect.Method.invokeNative(Native Method)
   06-29 01:38:02.968: E/AndroidRuntime(1119):  at java.lang.reflect.Method.invoke(Method.java:511)
   06-29 01:38:02.968: E/AndroidRuntime(1119):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:786)
   06-29 01:38:02.968: E/AndroidRuntime(1119):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
   06-29 01:38:02.968: E/AndroidRuntime(1119):  at dalvik.system.NativeStart.main(Native Method)
   06-29 01:38:03.028: W/ActivityManager(156):   Force finishing activity com.example.beam/.MainActivity
   06-29 01:38:03.039: W/WindowManager(156): Failure taking screenshot for (246x410) to layer 21010
   06-29 01:38:03.552: W/ActivityManager(156): Activity pause timeout for ActivityRecord{41366318 com.example.beam/.MainActivity}
  • 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-06T14:19:30+00:00Added an answer on June 6, 2026 at 2:19 pm
    private class MyTimerTask extends TimerTask {
        @Override
        public void run() {
    
                    mDialogHandler.sendEmptyMessage(DIALOG_OK);
    
        }
    
    }
    
     private Handler mDialogHandler = new Handler(){
                public void handleMessage(android.os.Message msg) {
                        switch (msg.what) {
                        case DIALOG_OK:
                                // We are now back in the UI thread
                                //textView.setText("changed");
                                readWebpage(text);
                        }
    
                };
        };
    

    You’re trying to start a Thread from inside a thread which is illegal. I also added some code for your UI Thread woes.

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

Sidebar

Related Questions

link Im having trouble converting the html entites into html characters, (&# 8217;) i
I used javascript for loading a picture on my website depending on which small
We're building an app, our first using Rails 3, and we're having to build
I have a text area in my form which accepts all possible characters from
For some reason, after submitting a string like this Jack’s Spindle from a text
I am trying to understand how to use SyndicationItem to display feed which is
I have a string like this: La Torre Eiffel paragonata all’Everest What PHP function
I would like to run a str_replace or preg_replace which looks for certain words
I'm parsing an RSS feed that has an ’ in it. SimpleXML turns this
Does anyone know how can I replace this 2 symbol below from the string

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.