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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 18, 20262026-05-18T04:34:35+00:00 2026-05-18T04:34:35+00:00

I seem to be having trouble with updating a TextView from a thread. I

  • 0

I seem to be having trouble with updating a TextView from a thread. I have a GameConnection class (which manages a socket connection) which I want to use across activities. It calls a local “onMessage”, which then uses the target handler to call dispatch Message. The “Handler” in this case, is in my GameBrowser activity.

Here’s code from the GameConnection class.

in = new BufferedReader(new InputStreamReader(socket.getInputStream()));
out = new PrintWriter(socket.getOutputStream(),true);

String message = "".intern();

// as a newline character is read, we interpret it as a message
while ((message = in.readLine()) != null && isConnected){
    onMessage(message);
}

As said above, a local method “onMessage” method handles dispatching of the message.

  private void onMessage(String message){
       ... // create message from String
      handler.dispatchMessage( msg );
  }

However, when I get the response in the GameBrowser class, I get a CalledFromWrongThreadException . Initially, I was using a callback method, which of course wasn’t working. So, after some research, I’ve found that I have to use a Handler, but I can’t seem to get it right.

public class GameBrowser extends Activity {

@Override
protected void onCreate(Bundle savedInstanceState) {

    super.onCreate(savedInstanceState);
    Log.d(C.tag, "GameBrowser.onCreate addr:" + this);

    handler = new Handler(new HandlerCallback());

    connection.addMessageListener(handler);
    connection.connect();
    txtGameLabel = (TextView)findViewById( R.id.txtGamesLabel);
    setContentView(R.layout.game_browser);


}

private class HandlerCallback implements Callback{

    @Override
    public boolean handleMessage(Message msg) {

        if (txtGameLabel == null){
            txtGameLabel =  (TextView)findViewById( R.id.txtGamesLabel);
        }

        String message = msg.getData().getString("message");

        Log.d(C.tag, "GameBrowser recieved message " + message);

        txtGameLabel.setText("Data: " + message);

        return true;
    }
}

}
  • 1 1 Answer
  • 1 View
  • 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-18T04:34:36+00:00Added an answer on May 18, 2026 at 4:34 am

    I figured out what I was doing wrong. Instead of calling the handler from the socket thread, I used a callback, then used Runnable to post to the handler in the GameConnection class. When onMessage executes “run”, which executes “updateTextField”, we’re back in the main thread.

    @Override
    public void onMessage(final String message) {
    
        handler.post(new Runnable(){
            @Override
            public void run() {
                updateTextField(message);
    
            }
        });
    
    }
    
    private void updateTextField(String message){
        if (txtGameLabel == null)
            txtGameLabel = (TextView)findViewById( R.id.txtGamesLabel);
    
        txtGameLabel.setText(message);
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I seem to be having trouble reading preferences from my AppWidgetProvider class. My code
I'm having trouble with my DatabaseConnection class. I cannot seem to get $dbUser or
I'm having trouble with updating an ASP:UpdatePanel using javascript (jQuery). Here're what I have.
I seem to be having trouble querying my 'events' table which contains foreign keys.
I seem to be having trouble with this. I have a Task table with
I seem to be having trouble splitting a namespace across projects. I have a
I seem to be having trouble getting a 'proper' connection between my Java server
I'm having some trouble updating Access tables from a DataGridView. What's confusing me is
I have started looking into Backbone.js and I seem to be having trouble figuring
I seem to be having trouble with my code. I need to say: if

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.