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

  • Home
  • SEARCH
  • 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 8204403
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 7, 20262026-06-07T07:53:29+00:00 2026-06-07T07:53:29+00:00

I have a problem with my GameLoop in my Android projekt (my first Android

  • 0

I have a problem with my GameLoop in my Android projekt (my first Android projekt):

I have an Activity that the GameView (SurfaceView) starts.

setContentView(new GameView(this));

The GameView (SurfaceView) starts the GameThread.

    public void surfaceCreated(SurfaceHolder holder) {
    Log.d("GameView", "surfaceCreate");
    surfaceHolder = holder;
    synchronized (this) {               //Must be executed exclusively
        if (gameLoop == null) {
            gameLoop = new GameLoop();  //Start animation here
            gameLoop.start();
        }
    }
}

    private class GameLoop extends Thread {

    public boolean running = true;

    public void run() {
        Canvas canvas = null;
        final SurfaceHolder surfaceHolder = GameView.this.surfaceHolder;

        while (running) {
            try {
                canvas = surfaceHolder.lockCanvas();
                synchronized (surfaceHolder) {
                    update();

                    checkCollision();

                    render(canvas);
                }
            } finally {
                if (canvas != null)
                    surfaceHolder.unlockCanvasAndPost(canvas);
            }
        }
    }

}

And now I can play. Up to this point and everything is OK.

If the player loses a new Activity (GameOver) started and the GameThread stoped.
Here he crashes!

    public void endgame() {

    Log.d("GameView", "ENDGAME");

    this.score.setStopscoring(true);
    this.box.stop();

    synchronized (surfaceHolder) {
        boolean retry = true;
        if (gameLoop != null) { 
            gameLoop.running = false;
            while (retry) {
                try {
                    gameLoop.join();
                    retry = false;
                } catch (Exception e) {
                }
            }
        }

    }

    Context context = getContext();
    context.startActivity(new Intent(context, _1GameOver.class));   

}

Afte the gameLoop.join(); it freezes.

I’ve tried a lot but nothing worked.
Thank you before for your help

  • 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-07T07:53:32+00:00Added an answer on June 7, 2026 at 7:53 am

    I have not found the exact error.

    Instead, I converted the code a bit.

        public void surfaceDestroyed(SurfaceHolder holder) {
    
            boolean retry = true;
    
            if (gameLoop != null) {
    
                gameLoop.running = false;
    
                while (retry) {
                    try {
                        gameLoop.join();
                        retry = false;  
                    } catch (InterruptedException e) {
                    }
                }
            }
            gameLoop = null;
    }
    

    And everything I conclude where I close my Activity.

    ((_1GameActivity)getContext()).finish();
    

    Not pretty but it works!

    Pease out

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

Sidebar

Related Questions

I have this big problem that i dont know how to fix. I have
I have problem with my query on C, I’m using the oci8 driver. This
Have problem while getting data from Memcached on .NET MVC solution. I have this
I have problem with List<Image> List<Image> _Images = new List<Image>(); int currIndex = 0;
I have searched and searched for a solution to this problem, but it seems
I have problem in some JavaScript that I am writing where the Switch statement
I have problem compilin this code..can anyone tell whats wrong with the syntax CREATE
I'm developing a 2d Game using Canvas/Surfaceview and have a problem with thread. So
I have a puzzle game for android. When the puzzle starts I take the
I have the start of an android game. It has a GameLoop (thread) class

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.