Note that there are some explanatory texts on larger screens.

plurals
  1. POsetContentView() inside of a thread
    text
    copied!<p>I'm making a simple Android game written in Java.</p> <p>I have my activity...</p> <pre><code>public class GameName extends Activity{ ... public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); } </code></pre> <p>Inside the "main" layout I have a button that calls a method called "startTheGame":</p> <pre><code>public void startTheGame(View v) { theGame = new Panel(this); setContentView(theGame); } </code></pre> <p>Here is the panel code (simplified)</p> <pre><code>class Panel extends SurfaceView implements SurfaceHolder.Callback { public GameThread _thread; public Panel(Context context) { super(context); getHolder().addCallback(this); setFocusable(true); } ... @Override public void surfaceCreated(SurfaceHolder holder) { _thread = new GameThread(getHolder(), this); _thread.setRunning(true); _thread.start(); } ... } </code></pre> <p>So as you can see I have a "GameThread" class that is started... here it is below:</p> <pre><code>class GameThread extends Thread { private SurfaceHolder _surfaceHolder; private Panel _panel; private boolean _run = false; public GameThread(SurfaceHolder surfaceHolder, Panel panel) { _surfaceHolder = surfaceHolder; _panel = panel; } public void setRunning(boolean run) { _run = run; } @Override public void run() { Canvas c; while (_run) { c = null; try { ... if (health &lt;= 0) { _run = false; //change views? setContentView(R.layout.over); } c = _surfaceHolder.lockCanvas(null); synchronized (_surfaceHolder) { _panel.onDraw(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) { _surfaceHolder.unlockCanvasAndPost(c); } try { Thread.sleep(60); } catch (Exception e) { } } } } </code></pre> <p>Hopefully you can see that if the health is &lt;= 0 it stops the thread and I am trying to change to another layout I created called "over".</p> <p>It just stops the screen from updating (drawing) but I never see the new panel.</p> <p>I've tried:</p> <pre><code>GameName cs = ((GameName)getApplicationContext()) cs.setContentView(R.layout.over); </code></pre> <p>But I'm getting a ClassCastException...</p> <p>Please Help!</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload