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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 29, 20262026-05-29T09:40:26+00:00 2026-05-29T09:40:26+00:00

I would like to insert a time counter inside a game. If the time

  • 0

I would like to insert a time counter inside a game. If the time is 0, there would be an AlertDialog which tells the user the time is out, and goes back to the previous Activity. Here is the method (it is inside a class which extends a SurfaceView):

public void showTime(){
    time--;
    Log.i("GameView time", "" + time);
    if (time <= 0){
        Log.i("gameview time","time out");
        gameTimer.setRunning(false);
        AlertDialog.Builder alt_bld = new AlertDialog.Builder(this.getContext());
        AlertDialog alert = alt_bld.create();
        alert.setTitle("Time is out. You lose.");
        alert.setButton("OK", new DialogInterface.OnClickListener() {
            public void onClick(DialogInterface dialog, int which) {
                main.onBackPressed();
            }});
        alert.show();
    }
}

The GameTimer class is a Thread:

public class GameTimer extends Thread{

private GameView gameView;
private boolean run;

public GameTimer(GameView gameView){
    this.gameView = gameView;
}

public void setRunning(boolean value){
    this.run = value;
} 

public void run(){
             Looper.prepare();
    while (run){
        try {
            gameView.showTime();
            sleep(1000);
        } catch (Exception e){
            e.printStackTrace();
        }
    }
            Looper.loop();
}

}

The AlertDialog appears, but the app crashes, with the message: Only the original thread that created a view hierarchy can touch views. But this is the thread which created… Where’s the problem?

  • 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-05-29T09:40:27+00:00Added an answer on May 29, 2026 at 9:40 am

    Do this using Handler or Use

    this.runOnUiThread(new Runnable() {
    
            public void run() {
                // TODO Auto-generated method stub
    
            }
        });
    

    The error itself tells the whole story.
    And if you are not in Activity/ View’s Parent class then use some callback Mechnasim.
    This will help you to resolve your issue.
    Cheers.

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

Sidebar

Related Questions

I would like to know how can I manually insert some php code inside
I would like to know how to insert TIME into SQL Server using C#
I would like to insert more rows in one time into my SQL table.
Site hosted via IIS 7.0 I would like to set my session time-out to
I would like to insert the current Subversion revision number (as reported by svnversion
I would like to insert dates in an emacs buffer like I do in
I would like to insert notes on the fixes to be done in specific
I have a little problem where I would like to insert a svn diff
I have created a test table in MySQL and would like to insert 10
I would like to programmatically insert additional rows into a DataGrid (to act as

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.