Hallo,
ich habe folgendes Problem und zwar, wenn ich meine App über Home-Button kurz verlasse und die App anschließend wieder öffne, dann stürzt meine App ab.
Hier ist mal der Quellcode:
GameLoopThread:
GameActivity:
GameView:
Die App stürzt dann immer ab (Anwendung TestApp reagiert nicht. Schließen?)
Kann mir da jemand weiterhelfen, wie ich das Problem ambesten lösen kann?
Danke schonmal für die Hilfe.
MfG
ich habe folgendes Problem und zwar, wenn ich meine App über Home-Button kurz verlasse und die App anschließend wieder öffne, dann stürzt meine App ab.
Hier ist mal der Quellcode:
GameLoopThread:
Java:
public GameLoopThread(GameView theView) {
this.theView = theView;
}
public GameLoopThread(Callback callback) {
// TODO Auto-generated constructor stub
}
public void setRunning(boolean run) {
isRunning = run;
}
protected void setPause(){
synchronized (this) {
isPaused = true;
}
}
protected void setResume(){
synchronized (this) {
isPaused = false;
this.notifyAll();
}
}
@SuppressLint("WrongCall")
@Override
public void run() {
synchronized (this) {
while (isRunning) {
if (isPaused) {
try {
this.wait();
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
} else {
Canvas theCanvas = null;
try {
theCanvas = theView.getHolder().lockCanvas();
synchronized (theView.getHolder()) {
theView.onDraw(theCanvas);
}
} finally {
if (theCanvas != null) {
theView.getHolder().unlockCanvasAndPost(theCanvas);
}
}
}
}
}
}
GameActivity:
Java:
@Override
protected void onPause(){
super.onPause();
gameView.setPause();
}
@Override
protected void onStop(){
super.onStop();
gameView.setPause();
}
@Override
protected void onResume(){
super.onResume();
gameView.setResume();
}
GameView:
Java:
public GameView(Context context) {
super(context);
// TODO Auto-generated constructor stub
theGameLoopThread = new GameLoopThread(this);
surfaceHolder = getHolder();
surfaceHolder.addCallback(new SurfaceHolder.Callback() {
public void surfaceDestroyed(SurfaceHolder holder) {
boolean retry = true;
theGameLoopThread.setRunning(false);
while(retry){
try {
theGameLoopThread.join();
retry=false;
}catch(InterruptedException e){
}
}
}
public void surfaceCreated(SurfaceHolder holder) {
// start the game loop
System.out.println("Starting Game Loop @@@@@@@@@");
if(isPaused){
System.out.println("GamePaused");
theGameLoopThread.setResume();
}
else{
System.out.println("Game Starting");
theGameLoopThread.setRunning(true);
}
theGameLoopThread.start();
}
public void surfaceChanged(SurfaceHolder holder, int format, int width,
int height) {
// TODO Auto-generated method stub
}
});
init(context);
}
public void setPause() {
theGameLoopThread.setPause();
isPaused = true;
}
public void setResume() {
theGameLoopThread.setResume();
theGameLoopThread.setRunning(true);
//theGameLoopThread.start();
isPaused = false;
}
Die App stürzt dann immer ab (Anwendung TestApp reagiert nicht. Schließen?)
Kann mir da jemand weiterhelfen, wie ich das Problem ambesten lösen kann?
Danke schonmal für die Hilfe.
MfG