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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T11:13:19+00:00 2026-06-12T11:13:19+00:00

So I have an activity with a TextView to hold the score like so:

  • 0

So I have an activity with a TextView to hold the score like so:

public class PlayGameActivity extends Activity {
    private GameThread mGameThread;
    private GameView mGameView;

    private Handler mHandler = new Handler();

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

        final TextView scoreView = (TextView) findViewById(R.id.textView2);

        mGameView = (GameView) findViewById(R.id.gameView1);
        mGameThread = mGameView.getThread();

        mHandler.post(new Runnable() {
            @Override
            public void run() {
                if (mGameView != null) {
                    scoreView.setText(mGameThread.getScore());
                }
            }
        });
    }
}

Then in my GameView I have the function declared like so:

class GameView extends SurfaceView implements SurfaceHolder.Callback {
    class GameThread extends Thread {
        int score = 0;

        public String getScore(){
            return Integer.toString(score);
        }

        @Override
        public void run() {
            synchronized (mSurfaceHolder) {
                if (something){
                    score++
                }
            }
        }
    }
}

The problem is that mGameThread.getScore() always returns 0 as if score is not updated when it certainly is.

If anyone has any incite it is greatly appreciated. Also I have striped lots of unnecessary code out of the snippets above. If you want to see the full text you may do so here:

  • PlayGameActivity.java
  • GameView.java
  • 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-12T11:13:21+00:00Added an answer on June 12, 2026 at 11:13 am

    There’s a better way to update UI data, use event / listener mechanism.

    Sitting in a looped thread is not an OOP style programming. If the change of score happens in your app than you definitely have a handle to detect the change. Once detected, update the UI with one simple setText call to your textview.

    Do not overcrowd your UI event queue stack as you (maybe unknowingly) do by calling post(….), this will make your UI less responsive to actual user interactions especially if you’re doing it in a timed loop. Remember that the UI event queue stack is ONE for the entire system, it is meant to handle user’s interactions with the device.

    If you need to change UI component from non-UI thread use

         activity.runOnUiThread(Runnable); 
    

    method instead of post(Runnable). This way you may have a bit longer executing code (although not recommended) defined in Runnable. Otherwise make sure that the code in post(Runnable) is very quick to execute and finish.

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

Sidebar

Related Questions

I have created one class as public class FormActivity extends Activity { TextView tv
I have this activity, package org.dewsworld.ui; import android.app.Activity; import android.os.Bundle; import android.widget.TextView; public class
I have this code: import android.app.Activity; import android.os.Bundle; import android.os.CountDownTimer; import android.widget.TextView; public class
this is my Manual.java file: public class Manual extends Activity implements OnClickListener{ Button create;
I have an activity that shows some TextView s and EditText s, as well
Alright, so i have an Activity. In the activity there is a textview and
I have a Button and a TextView . When I hold this Button for
I have an activity where I grab a textview normallythen I have a separate
I have an activity with a layout contains an ImageView and TextView . ImageView
Let's say I have an Activity which displays a View containing a TextView .

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.