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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T21:21:15+00:00 2026-06-14T21:21:15+00:00

I recently refactored an old project and found that a particular portion no longer

  • 0

I recently refactored an old project and found that a particular portion no longer wants to run properly no matter what I do. Essentially, I have an Activity with a TextView. This view is updated at timed intervals from a thread invoked within the same class. The pause is accomplished with a Thread.sleep and a Handler is used to trigger the update of the UI. The thing is, now I either get a CalledFromWrongThreadException saying that I cannot manipulate the View from another thread, a long pause followed by all the text hitting the View at once, or it seemingly fires off immediately and doesn’t sleep at all. I had previously done it like so (relevant parts included):

public class Updated extends Activity implements Runnable {

private TextView textView;
private String text;
private Handler handle = new Handler(){
    @Override
    public void handleMessage(Message msg){
        textView.append(text);
    }
};
//onCreate and other such things...{
    new Thread(this).start();
}

public void run(){
     //A healthy chunk of logic/conditionals
     text = "Blah blah";
     handle.sendEmtpyMessage(0);
     try {
    Thread.sleep(1500l);
 } catch (InterruptedException e) {
    e.printStackTrace();
 }
}

That’s the gist of what I was using previously, which worked fine. However, I really didn’t like the practice of setting a class variable and then having the Handler grab it later. I modified it to pass a Message with the .obj field set to a String, which would then be peeled out and appended to the TextView. This would blow up with the aforementioned exception. I tried leaving the Handler declaration in place, but moving the logic within to another method where the Handler would .post(Runnable(){}); with the same guts, but that would just stall and append it after a significant wait. I’m not terribly savvy with the inner workings of Handlers and Threads within Java/Android, so any pointers would be quite welcome.

  • 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-14T21:21:16+00:00Added an answer on June 14, 2026 at 9:21 pm

    You are creating the handler statically, which creates it in the loader thread. Create the handler in onCreate() and it will be bound to the UI thread, and resolve your issue. Also, instead of Thread.sleep, you can do handler.sendEmptyMessageDelayed whenever you receive a message, and just send an initial message to kick things off. I think Thread.sleep in handler can jam a thread.

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

Sidebar

Related Questions

I have a DataTrigger that I recently refactored. It used to have the DataContext
I recently refactored a WPF app so that it no longer wraps each use
I've recently refactored my src directory in my IntelliJ Android project now, when I
I recently refactored old code files from ABCFile.cpp/.h to AbcFile.cpp/.h to match my company's
On one project we have been working on recently, we have in essence totally
I recently refactored some code and now have a static utility class with a
I have recently started using generics in java, and in that attempt tried to
I have twice recently refactored code in order to change the order of parameters
Background I have an app that I recently updated to Rails 3.2.1 (from Rails
I'm writing an android app that does a lot of stuff. I recently refactored

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.