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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 24, 20262026-05-24T19:03:43+00:00 2026-05-24T19:03:43+00:00

First, note that exactly the same thing happens with the LunarLander example. Anyways, this

  • 0

First, note that exactly the same thing happens with the LunarLander example.
Anyways, this works great until I leave the app and try to return to it, whereupon I get a force close (IllegalThreadStateException).

MainGameActivity:

package com.tests.testgame1;

import android.app.Activity;
import android.os.Bundle;
import android.util.Log;

public class MainGameActivity extends Activity {

    private static MainGameView mainGameView;
    //private static Thread mainGameThread;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        mainGameView = new MainGameView(getApplicationContext());
        //mainGameThread = mainGameView.getThread();


//        if (savedInstanceState == null) {
//            // we were just launched: set up a new game
//          mainGameThread.setState(mainGameThread.STATE_READY);
//            Log.w(this.getClass().getName(), "SIS is null");
//        } else {
//            // we are being restored: resume a previous game
//          mainGameThread.restoreState(savedInstanceState);
//            Log.w(this.getClass().getName(), "SIS is nonnull");
//        }

        setContentView(mainGameView);
    }

    @Override
    protected void onStart() {
        super.onStart();
        // The activity is about to become visible.
    }

    @Override
    protected void onResume() {
        super.onResume();
        mainGameView.unpauseGame();
        // The activity has become visible (it is now "resumed").
    }

    @Override
    protected void onPause() {
        super.onPause();
        mainGameView.pauseGame();
        // Another activity is taking focus (this activity is about to be "paused").
    }

    @Override
    protected void onStop() {
        super.onStop();
        // The activity is no longer visible (it is now "stopped")
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        // The activity is about to be destroyed.
    }





    @Override
    protected void onSaveInstanceState(Bundle outState) {
        // just have the View's thread save its state into our Bundle
        super.onSaveInstanceState(outState);
//        mainGameThread.saveState(outState);
        Log.w(this.getClass().getName(), "SIS called");
    }

}

MainGameView:

package com.tests.testgame1;



import android.content.Context;
import android.util.Log;
import android.view.SurfaceHolder;
import android.view.SurfaceHolder.Callback;
import android.view.SurfaceView;


public class MainGameView extends SurfaceView implements SurfaceHolder.Callback {

    private static final String TAG = "MainGameView"; //
    MainGameThread mainGameThread; //
    SurfaceHolder mainSurfaceHolder;
    Context mainContext;

    public MainGameView(Context context) {
        super(context);
        mainSurfaceHolder = getHolder();
        mainSurfaceHolder.addCallback(this);
        mainContext = context;
        mainGameThread = new MainGameThread(this, mainSurfaceHolder, mainContext);



        Log.d(TAG, "View created");



    }

    public Thread getThread() {
        return mainGameThread;
    }

    public void pauseGame() {
        mainGameThread.pauseGame();
    }


    public void unpauseGame() {
        //mainGameThread.start();
        mainGameThread.unpauseGame();
    }
    // 



    @Override
    public void onWindowFocusChanged(boolean hasWindowFocus) {
        if (!hasWindowFocus) mainGameThread.pauseGame();
    }


    public void onDraw()
    {

    }

    @Override
    public void surfaceChanged(SurfaceHolder arg0, int arg1, int arg2, int arg3) {
        // TODO Auto-generated method stub

    }

    @Override
    public void surfaceCreated(SurfaceHolder arg0) {

        mainGameThread.setRunning(true);
        mainGameThread.start();

    }

    @Override
    public void surfaceDestroyed(SurfaceHolder arg0) {

        // we have to tell thread to shut down & wait for it to finish, or else
        // it might touch the Surface after we return and explode
        boolean retry = true;
        mainGameThread.setRunning(false);
        while (retry) {
            try {
                mainGameThread.join();
                retry = false;
            } catch (InterruptedException e) {
            }
        }

    }

}

MainGameThread:

package com.tests.testgame1;

import android.content.Context;
import android.content.res.Resources;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Color;
import android.util.Log;
import android.view.SurfaceHolder;
import android.view.View;

class MainGameThread extends Thread {

    enum GameStates
    {
        GameStateRunning,
        GameStatePaused
    }

    private boolean running;
    private GameStates gameMode;
    private View view;
    private Context context;
    private SurfaceHolder surfaceHolder;

    Bitmap backgroundImage;

    public MainGameThread(View _view, SurfaceHolder _surfaceHolder, Context _context)
    {
        this.view = _view;
        this.surfaceHolder = _surfaceHolder;
        this.context = _context;
        this.running = true;
        this.gameMode = GameStates.GameStateRunning;

        Resources res = context.getResources();
        backgroundImage = BitmapFactory.decodeResource(res, R.drawable.background);
    }
    public void setRunning(boolean b) {
        running = b;
    }

    @Override
    public void run() {


        while (running) {
            if (gameMode == GameStates.GameStateRunning)
                    doStep();

            Canvas c = null;
            try {
                c = surfaceHolder.lockCanvas(null);
                synchronized (surfaceHolder) {

                    doDraw(c);
                }
            } finally {
                // do this in a finally so that if an exception is thrown
                // during the above, we don't leave the Surface in an
                // inconsistent state
                if (c != null) {
                    synchronized (surfaceHolder) {
                    surfaceHolder.unlockCanvasAndPost(c);
                    }  
                }
            }

        }


    }

    private void doDraw(Canvas canvas) {
        canvas.drawBitmap(backgroundImage, 0, 0, null);
        //canvas.drawColor(Color.BLUE);

    }

    private void doStep() {

    }

    public void pauseGame()
    { //
        synchronized (surfaceHolder) {
            if (gameMode == GameStates.GameStateRunning){
                gameMode = GameStates.GameStatePaused;
            }

        }
    }

    public void unpauseGame() {
        synchronized (surfaceHolder) {
            if (gameMode == GameStates.GameStatePaused){
                gameMode = GameStates.GameStateRunning;
            }

        }
    }


}

Stack trace:

Test Game [Android Application] 
    DalvikVM[localhost:8625]    
        Thread [<1> main] (Suspended (exception IllegalThreadStateException))   
            MainGameView(SurfaceView).updateWindow(boolean) line: 545   
            MainGameView(SurfaceView).onWindowVisibilityChanged(int) line: 206  
            MainGameView(View).dispatchWindowVisibilityChanged(int) line: 3891  
            FrameLayout(ViewGroup).dispatchWindowVisibilityChanged(int) line: 719   
            LinearLayout(ViewGroup).dispatchWindowVisibilityChanged(int) line: 719  
            PhoneWindow$DecorView(ViewGroup).dispatchWindowVisibilityChanged(int) line: 719 
            ViewRoot.performTraversals() line: 744  
            ViewRoot.handleMessage(Message) line: 1727  
            ViewRoot(Handler).dispatchMessage(Message) line: 99 
            Looper.loop() line: 123 
            ActivityThread.main(String[]) line: 4627    
            Method.invokeNative(Object, Object[], Class, Class[], Class, int, boolean) line: not available [native method]  
            Method.invoke(Object, Object...) line: 521  
            ZygoteInit$MethodAndArgsCaller.run() line: 868  
            ZygoteInit.main(String[]) line: 626 
            NativeStart.main(String[]) line: not available [native method]  
        Thread [<6> Binder Thread #2] (Running) 
        Thread [<5> Binder Thread #1] (Running) 
        Thread [<7> Binder Thread #3] (Running) 
  • 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-24T19:03:43+00:00Added an answer on May 24, 2026 at 7:03 pm

    Think you are having an issue in your surfaceCreated Method. When you are exiting the thread it is being terminated. Once this has occurred you can not call

    mainGameThread.start();

    Instead, inside surfaceCreated, first query the state of the thread first, then if it has been terminated, start a new one as you did in MainGameView,

    if (mainGameThread .getState() == Thread.State.TERMINATED)
    {
    
        mainGameThread = new MainGameThread(this, mainSurfaceHolder, mainContext);  
        mainGameThread.setRunning(true);
        mainGameThread.start();
    }
    else
    {
        mainGameThread.setRunning(true);
        mainGameThread.start();
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

The first thing i would like to say is that this is not exactly
First as a note I am using this plugin in a Rails app. Ok
NOTE: I am not set on using VI, it is just the first thing
NOTE: I am not exactly sure how to title or tag this question, so
I have an app that uses EF POCO for accessing data. Everything works great,
How can I retrieve the first and last record from a table. Note :
First off, there's a bit of background to this issue available on my blog:
I noticed the WinForms RichTextBox has a ZoomFactor property that I assume is exactly
EDIT : see bottom First off I searched for an answer before asking this
First off, I'm using Access 2000 and DAO. I have code that executes a

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.