4

My 'game' keeps crashing after I resume it. Basically, I launch the app and everything works. Then I press the home button and go back to my home screen. Still all the normal stuff. But THEN when I open the app back up, it just freezes and after 1 minute or so, I get a not responding message.

Here is my main activity:

package com.amzoft.android.starraider;

import android.app.Activity;
import android.content.Context;
import android.graphics.Canvas;
import android.hardware.Sensor;
import android.hardware.SensorEvent;
import android.hardware.SensorEventListener;
import android.hardware.SensorManager;
import android.os.Bundle;
import android.util.Log;
import android.view.KeyEvent;
import android.view.MotionEvent;
import android.view.SurfaceHolder;
import android.view.SurfaceView;
import android.view.View;
import android.view.View.OnKeyListener;
import android.view.View.OnTouchListener;
import android.view.Window;
import android.view.WindowManager;

public class StarRaiderMain extends Activity implements OnTouchListener, OnKeyListener, SensorEventListener{

FastRenderView renderView;
SensorManager sensorManager;
Sensor accelerometer;
Thread mainGameThread;

Player player;

public void onCreate(Bundle savedInstanceState) 
{
    super.onCreate(savedInstanceState);
    requestWindowFeature(Window.FEATURE_NO_TITLE);
    getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);
    renderView = new FastRenderView(this);
    renderView.setOnKeyListener(this);
    renderView.setFocusableInTouchMode(true);
    renderView.requestFocus();
    setContentView(renderView);

    sensorManager = (SensorManager)getSystemService(Context.SENSOR_SERVICE);
    accelerometer = sensorManager.getSensorList(Sensor.TYPE_ACCELEROMETER).get(0);

    mainGameThread = new Thread(new mainGameThread());

    player = new Player(this);
}

@Override
protected void onResume()
{
    super.onResume();
    renderView.resume();
    renderView.requestFocus();
    sensorManager.registerListener(this, accelerometer, SensorManager.SENSOR_DELAY_GAME);
}

@Override
protected void onPause()
{
    super.onPause();
    renderView.pause();
    sensorManager.unregisterListener(this);
}

@Override
public void onAccuracyChanged(Sensor sensor, int accuracy) 
{

}

@Override
public void onSensorChanged(SensorEvent event) 
{
    player.onSensorChanged(event);
}

@Override
public boolean onKey(View v, int keyCode, KeyEvent event) 
{
    player.onKey(v, keyCode, event);
    return true;
}

@Override
public boolean onTouch(View v, MotionEvent event) 
{

    return true;
}

class FastRenderView extends SurfaceView implements Runnable
{
    Thread renderThread = null;
    SurfaceHolder holder;
    volatile boolean running = false;

    public FastRenderView(Context context)
    {
        super(context);
        holder = getHolder();
    }

    public void resume()
    {
        running = true;
        renderThread = new Thread(this);
        renderThread.start();
    }

    public void pause()
    {
        running = false;
        while(true)
        {
            try{
                renderThread.join();
            }catch(InterruptedException e){
                Log.d("StarRaider", e.getMessage());
            }
        }
    }

    @Override
    public void run()
    {
        while(running)
        {
            if(!holder.getSurface().isValid())
                continue;
            Canvas canvas = holder.lockCanvas();
            onDraw(canvas);
            holder.unlockCanvasAndPost(canvas);
        }
    }

    public void onDraw(Canvas canvas)
    {
        canvas.drawRGB(255, 255, 255);
        player.onDraw(canvas);
    }
}
class mainGameThread extends Thread
{
    mainGameThread()
    {
        super("mainGameThread");
        start();
    }
    public void run()
    {
        try{
            player.onUpdate();
        }catch(Exception e){

        }
        }
    }
}

screenie

Any help would be greatly appreciated.

Here is my logcat output: http://pastebin.com/3b09YAkP I have a main game thread, because I read that if the render thread gets stuck for 2 seconds, it crashes.

Kara
  • 6,115
  • 16
  • 50
  • 57
chrypthic
  • 579
  • 1
  • 6
  • 15

2 Answers2

1

It looks like it might be one of two things.

You should probably be explicitly pausing your thread in onPause(). That might cause application deadlock if it tries to resume without having been paused.

You should also take a look at the Google I/O presentation on Replica Island. It's about an hour long. Take note of the part where he talks about garbage collection. This might be what's happening and causing your app to crash because of an ANR error.

I hope this helps!

Codeman
  • 12,157
  • 10
  • 53
  • 91
  • how do i explicitly pause it? – chrypthic Nov 08 '11 at 19:07
  • Please read this article: http://developer.android.com/resources/articles/painless-threading.html – Codeman Nov 08 '11 at 19:10
  • k, I got rid of the entire while loop in the pause method in my FastRenderView inner class. It seems to work now, but is there anything else i could do? I have a book on android game development, and I have learnt the basics. But now the book wants to write a whole framework and i really cant be asked to, because it suddenly assumes that i know what all the stuff it is talking about. I want to make this game, and then read the section on OpenGL ES. – chrypthic Nov 08 '11 at 19:13
  • Honestly, I'm not sure. I am just learning OpenGL ES myself, so I can't speak to that. I'm glad you fixed your problem though! – Codeman Nov 08 '11 at 19:16
  • Yes, thanks. Can you provide me with a link to the I/O video? – chrypthic Nov 08 '11 at 19:17
  • http://www.youtube.com/watch?v=7-62tRHLcHk I learned a lot in just an hour watching this :) – Codeman Nov 08 '11 at 19:18
0

May be related to this bug http://code.google.com/p/android/issues/detail?id=2566

user1139880
  • 1,828
  • 3
  • 18
  • 27