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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T14:57:55+00:00 2026-05-27T14:57:55+00:00

I’ve searched over and over in this forum & google about this issue, maybe

  • 0

I’ve searched over and over in this forum & google about this issue, maybe it has to do something with my thread or the way how the game is starting.

This is the skeleton of the game application, please tell me what I’m doing wrong.

This is what I’m doing in thread;

public class GameThread extends Thread 
{    

static final long FPS = 30;
private GameView view;
private boolean running = false;
private SurfaceHolder surfaceHolder;
public boolean isSurfaceCreated = false;

private Object mPauseLock;
private boolean mPaused;

public GameThread(SurfaceHolder surfaceHolder, Handler handler, GameView view) 
{
    super();
    mPauseLock = new Object();
    mPaused = false;
    
    this.surfaceHolder = surfaceHolder;
    this.view = view;
}

public void setRunning(boolean run) 
{
      running = run;
}

/**
 * pause thread.
 */
public void onPause() {
    synchronized (mPauseLock) {
        mPaused = true;
    }
}

/**
 * resume thread.
 */
public void onResume() {
    synchronized (mPauseLock) {
        mPaused = false;
        mPauseLock.notifyAll();
    }
}

@Override
public void run() 
{

    // run our thread on high priority to keep from getting slowdown
    android.os.Process.setThreadPriority(android.os.Process.THREAD_PRIORITY_URGENT_DISPLAY);
    
    // wait for surface to become available @ start
    while (!isSurfaceCreated && running) 
    {
        try {
            sleep(50);
        } catch (InterruptedException e) {}
    }       
    
    long ticksPS = 1000 / FPS;

    long startTime;

    long sleepTime;

    while (running){

          Canvas c = null;

          startTime = System.currentTimeMillis();

          //long msSinceLastTick = System.currentTimeMillis() - view.lastTickMs;
             
             try {

                    c = surfaceHolder.lockCanvas(null);

                    synchronized (surfaceHolder) 
                    {
                    
                        /*
                         * stop updating when pause is pressed
                         */
                        if(mPaused == false)
                        {
                            view.update();
                        }

                        view.onDraw(c);

                    }

             } finally {

                    if (c != null) 
                    {

                        surfaceHolder.unlockCanvasAndPost(c);

                    }

             }

             sleepTime = ticksPS - (System.currentTimeMillis() - startTime);

             try {
         
                 
                    if (sleepTime > 0)

                           sleep(sleepTime);

                    else

                           sleep(10);

             } catch (Exception e) {}

      }

}

This is the start activity code;

import android.app.Activity;
import android.content.pm.ActivityInfo;
import android.os.Bundle;

public class test_game extends Activity {
    /** Called when the activity is first created. */

    private GameView Game;
    
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        Game = new GameView(this);
       
    setContentView(Game);
       
    }
      
    @Override
    public void onBackPressed() 
    {
        super.onBackPressed();

        //KILL ALL
        finish();
        System.runFinalizersOnExit(true);
        System.exit(0);
    }

 // ===========================================================
 // onPause
 // ===========================================================

 protected void onPause() {
     super.onPause();

 }


 // ===========================================================
 // onStop
 // ===========================================================

 protected void onStop() {
     super.onStop();
 }

 // ===========================================================
 // onDestroy
 // ===========================================================

 @Override
 protected void onDestroy() {
    super.onDestroy();
    
 }

}

Added extra lines in AndroidManifest.xml

      android:launchMode="singleTask" 
      android:alwaysRetainTaskState="true"

GameView looks like this:

   public class GameView extends SurfaceView {

   private SurfaceHolder holder;
   private GameThread Thread = null;
   private int testVar = 0;
   private boolean isPaused = false;


   public GameView(Context context) 
   {

         super(context);

     loadGameFiles();
        
         holder = getHolder();
         
         Thread = new GameThread(holder, new Handler(), this);
         
         holder.addCallback(new SurfaceHolder.Callback() 
         {

                @Override
                public void surfaceDestroyed(SurfaceHolder holder) 
                {

                       boolean retry = true;

                       Thread.setRunning(false);

                       while (retry) 
                       {

                              try {

                                    Thread.join();
                                    retry = false;

                              } catch (InterruptedException e) 
                              {

                              }

                       }

                }

                @Override
                public void surfaceCreated(SurfaceHolder holder) 
                {
                    
                    Thread.isSurfaceCreated = true; 
                    
                    if(isPaused == true)
                    {
                        onResume();
                        
                    }

                    Thread.setRunning(true);
                    Thread.start();
                    GameMenu();


                }



                @Override
                public void surfaceChanged(SurfaceHolder holder, int format, int width, int height) 
                {
                }

         });

         setFocusable(true); 

   }


   
   public boolean isPaused()
   {
       return this.isPaused;
   }
   
   /**
    * Pauses the physics update & animation.
    */
   public void onPause() {
       synchronized (holder) 
       {
           if(isPaused == false) 
           {   
               isPaused = true;
           Thread.onPause();
           }
       }
   }
  
   public void onResume()
   {
       synchronized (holder) 
       {
           if(isPaused == true) 
           {   
               isPaused = false;
               Thread.onResume();
           }
       }
   }

   etc..

Thanks for the replies, the forum commentbox doesn’t let me post this; anyway, here it is:

Hi Frankenstein, I’ve tried what you suggested here, still doesn’t work. My resume code in the GameView looks like this;

public void onResume(){ synchronized (holder) 
           { if(isPaused == true){   
                   isPaused = false; //resume notification to the gameview
                   Thread.onResume(); //resume the thread
               }
           }
       }

This is on the Activity class;
 @Override
 protected void onPause() {
     super.onPause();
     Game.onPause();
 }
 @Override
 protected void onResume() {
     super.onResume();
     Game.onResume();
 }

I have not used logcat, not sure how to use it, I can sure call it in each steps in java but where does it displays the error/logs? thanks in advance guys!

When I resume it shows following error in LogCat;

Added this line to avoid the previous problem;

                    Thread.isSurfaceCreated = true; 
                    Thread.setRunning(true);
                    Thread.start();
                    loadGame();

UPDATE:
The application returns to black screen, but it’s not frozen.

Errors (updated) in logcat;
12-14 16:54:52.176: ERROR/MediaPlayerService(3937):  create PVPlayer
12-14 16:55:03.172: ERROR/dalvikvm(4619): Failed to write stack traces to /data/anr/traces.txt (1093 of 3526): Unknown error: 0
12-14 16:55:03.910: ERROR/dalvikvm(25464): Failed to write stack traces to /data/anr/traces.txt (2401 of 3332): Math result not representable
12-14 16:55:03.918: ERROR/dalvikvm(25279): Failed to write stack traces to /data/anr/traces.txt (-1 of 2354): Math result not representable
12-14 16:55:32.394: ERROR/imdg81(19379): IsShutDownStarted()
12-14 16:55:32.590: ERROR/imdg81(19379): IsShutDownStarted()
12-14 16:55:37.902: ERROR/MediaPlayer(19379): error (100, 0)
12-14 16:55:37.930: ERROR/SoundPool(5264): Unable to load sample: (null)
12-14 16:55:39.281: ERROR/SoundBooster(5328): readSBTable: file open error!
12-14 16:55:39.281: ERROR/AcousticEQ(5328): [AEQ] aeqcoe.txt: file open error!
12-14 16:55:39.500: ERROR/AudioService(19379): Media server died.
12-14 16:55:39.500: ERROR/AudioService(19379): Media server started.
12-14 16:55:39.996: ERROR/MediaPlayerService(5328):  create PVPlayer
  • 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-27T14:57:55+00:00Added an answer on May 27, 2026 at 2:57 pm

    In your onSurfaceCreated method you have this block of code:

      if(isPaused == true){
          onResume();
    
      }
    
      Thread.setRunning(true);
      Thread.start();
    

    your call to Thread.start() is called no matter what (even if isPaused is true). Since your thread is already started in this case, you’re trying to start it twice and thus getting an illegal state exception.

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

Sidebar

Related Questions

I'm parsing an RSS feed that has an ’ in it. SimpleXML turns this
I have a string like this: La Torre Eiffel paragonata all’Everest What PHP function
link Im having trouble converting the html entites into html characters, (&# 8217;) i
That's pretty much it. I'm using Nokogiri to scrape a web page what has
I want to count how many characters a certain string has in PHP, but
For some reason, after submitting a string like this Jack’s Spindle from a text
Basically, what I'm trying to create is a page of div tags, each has
this is what i have right now Drawing an RSS feed into the php,
I've got a string that has curly quotes in it. I'd like to replace
I am reading a book about Javascript and jQuery and using one of the

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.