In the code, I deleted all the declared variables and objects so as not to clutter up. When you press the power button off, super.Pause () is called and surfaceDestroy is not called. I decided to try to override the onPause () method and call destroy () through it. To do this, I in the class Menu must initialize an instance of the object of the class MainGamePanel. and call mMainGamePanel.gestroy (). If I do not initialize, the error Attempt to invoke virtual method 'void example.igeniy.MainGamePanel.destroy ()' on a null object reference will appear
Help to initialize and explain why this is so. Thank you. I will also be happy if you tell me why the power off button does not call the surfaceDestroyed method.
public class MainGamePanel extends SurfaceView implements SurfaceHolder.Callback { public MainGamePanel(Context context) { super(context); getHolder().addCallback(this); setFocusable(true); } @Override public void surfaceChanged(SurfaceHolder holder, int format, int width, int height) { Log.d(TAG,"surfaceChanged"); } @Override public void surfaceCreated(SurfaceHolder holder) { Log.d(TAG,"surfaceCreated"); thread = new MainThread(getHolder(), this); thread.setRunning(true); thread.start(); } @Override public void surfaceDestroyed(SurfaceHolder holder) { Log.d(TAG,"surfaceDestroyed"); destroy(); } public synchronized void destroy(){ if(thread == null){ return; } boolean retry = true; thread.setRunning(false); while (retry){ try{ thread.join(); retry = false; } catch (InterruptedException ignored){ } } thread = null; } And another class:
public class Menu extends AppCompatActivity { MainGamePanel mMainGamePanel = new MainGamePanel(просит ввести сюда Contex contect); //если дословно ввести Contex contex , то context горит красным (пишу это во избежание глупых советов) @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN); setContentView(new MainGamePanel(this)); } @Override protected void onPause() { super.onPause(); mMainGamePanel.destroy(); // вот что надо вызвать pause = false; Log.d(TAG, "onPause"); } }
surfaceDestroyedmethodsurfaceDestroyednot automatically called? - georgehardcore