0

i am trying to find a way that the thread can be shut down and restarted without the program crashing. It is called from a menu and an activity sets the panel as its content view and i wish that when the return arrow is pressed on the android that it returns to the activity and then the thread can be restarted however currently any variation i try causes it to crash at one point or another :(

package SortItOut.sortitout;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.view.MotionEvent;
import android.view.SurfaceHolder;
import android.view.SurfaceView;

public class Level1Panel extends SurfaceView implements
SurfaceHolder.Callback {

private Level1Thread thread;
static Bitmap background;
static int x = 0;

 public Level1Panel(Context context) {
super(context);
getHolder().addCallback(this);
background = BitmapFactory.decodeResource(getResources(), R.drawable.gamebackground);
thread = new Level1Thread(getHolder(), this);
setFocusable(true);
}


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

 public void surfaceCreated(SurfaceHolder holder) {
 thread.setRunning(true);
 thread.start();
 }


 public void surfaceDestroyed(SurfaceHolder holder) {
 thread.stop();
}

public void render(Canvas canvas)
{
canvas.drawBitmap(background, x, 0, null);
x = x + 20;
}

}

======Thread=======

package SortItOut.sortitout;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.view.SurfaceHolder;

public class Level1Thread extends Thread {


 private boolean running;
 private SurfaceHolder surfaceHolder;
 private Level1Panel gamePanel;


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

 public Level1Thread(SurfaceHolder surfaceHolder, Level1Panel gamePanel) {
     super();
     this.surfaceHolder = surfaceHolder;
     this.gamePanel = gamePanel;
    }



 public void run() {
     Canvas canvas;

     while (running) {
     canvas = null;

     try {
         canvas = this.surfaceHolder.lockCanvas();
        synchronized (surfaceHolder) {
        this.gamePanel.render(canvas);

        }
       } finally {

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



}
Ben Kneebone
  • 77
  • 2
  • 11

2 Answers2

0

You should not use thred.stop(); You must allow the thred to stop on its own by stopping the loop within it.

Check the second example of SurfaceView aniamtion: How can I use the animation framework inside the canvas?

Community
  • 1
  • 1
Lumis
  • 21,517
  • 8
  • 63
  • 67
  • I implemented the code in this link under the surface destroyed method. The previous activity no longer crashes however re-starting the thread has no effect – Ben Kneebone Aug 01 '11 at 17:34
  • e.g if i restart the thread, a blackscreen displays instead of the moving image – Ben Kneebone Aug 01 '11 at 17:46
  • I think that could because you are using static X which accumulates value and survives the activitie's destruction. Try placing x = 0; in the constructor "public Level1Thread(SurfaceHolder...)" – Lumis Aug 01 '11 at 20:18
0

Here are some suggestions (without getting any details on the exception of the crash):

  • You should Implement Runnable instead of Extend Thread.
  • You should let the thread know that it should exit the loop.
  • You should interrupt() the thread instead of calling stop() (interrupt also takes the thread out of a blocking state).
  • You should handle the InterruptedException inside the run method.
  • You should exit gracefully when you're interrupted (i.e. finish up whatever you're doing and clean up).

I'm sure I'm missing some things, but generally this should keep you out of trouble. Again, without knowing the specific exception you're getting, I would assume that you're calling stop and the thread is not cleaning up properly at whatever state it was when running, thus you get the crashes at one point or another (depending on what's corrupted with the state).

Community
  • 1
  • 1
Kiril
  • 39,672
  • 31
  • 167
  • 226