Java – the application does not respond after recovery
After recovery, my "game" kept crashing Basically, I start the application and everything is normal Then I press the home button to return to the home screen It's still all normal However, when I open the application backup, it just freezes. About a minute later, I receive an unresponsive message
This 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){ } } } }
Any help would be appreciated
This is my logcat output: http://pastebin.com/3b09YAkP I have a major game thread because I read that if the rendering thread gets stuck for 2 seconds, it will crash
Solution
It seems that it may be one of two things
You should probably explicitly pause your thread in onpause() If it tries to resume without pausing, it may cause application deadlock
You should also view the Google I / O presentation on replica island About an hour Write down his part about garbage collection This may be happening and cause your application to crash due to an anr error
I hope it helps!