Application does not respond after resuming

My "game" continues to crash after I resumed it. Basically, I run the application and everything works. Then I press the home button and return to the main screen. Still, everything is normal. But THEN, when I open the application, it just freezes, and after 1 minute or so I get a message that does not respond.

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 stream because I read that if the rendering stream got stuck for 2 seconds, it will work.

+4
source share
2 answers

It looks like it could be one of two things.

You should probably explicitly pause your thread in onPause() . This can cause an application deadlock if it tries to resume operation without suspension.

You should also read the Google I / O presentation on Replica Island. This is about an hour. Pay attention to the part where he talks about garbage collection. Perhaps this could happen and cause your application to crash due to an ANR error.

Hope this helps!

+1
source

Source: https://habr.com/ru/post/1380257/


All Articles