android ブロック崩しを作っています。
SurfaceViewを継承したCustomViewを利用しています。
実行中に戻るボタンとホームボタンを押してみました。
戻るボタン ー正常。
ホームボタン ー エラーが発生して、画面が真っ暗なまま動かなくなる
ホームボタン押下時にエラーが出て画面が動かなくなるのですが、SurfaceViewの使い方が間違ってますよね。。。?
アクティビティの繊維(Destroy Createdなど…)についての知識がほぼないまま使用していたため、原因をご教授いただけると幸いです。
エラー
E/SurfaceHolder: Exception locking surface java.lang.IllegalStateException: Surface has already been released. at android.view.Surface.checkNotReleasedLocked(Surface.java:434) at android.view.Surface.lockCanvas(Surface.java:258) at android.view.SurfaceView$4.internalLockCanvas(SurfaceView.java:822) at android.view.SurfaceView$4.lockCanvas(SurfaceView.java:790) at com.example.blockbreak.MainActivity$CustomView.run(MainActivity.java:245) at java.lang.Thread.run(Thread.java:818)
MainActivity(中略しています)
package com.example.blockbreak; public class MainActivity extends AppCompatActivity { int width,height; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); //カスタムビュークラスのインスタンスをコンテントにセット setContentView(new CustomView(this)); } class CustomView extends SurfaceView implements SurfaceHolder.Callback, Runnable { //スレッドクラス Thread mainLoop; Canvas canvas; // 描画用 @Override public void onWindowFocusChanged(boolean hasFocus) { { super.onWindowFocusChanged(hasFocus); Point point = new Point(0, 0); point.set(getWidth(), getHeight()); width = getWidth();//720 height = getHeight();//1230 Log.d("size", "width: "+width); Log.d("size", "height: "+height); } } public Point getViewSize(View View){ Point point = new Point(0, 0); point.set(View.getWidth(), View.getHeight()); return point; } //コンストラクタ public CustomView(Context context) { super(context); // SurfaceView描画に用いるコールバックを登録する。 getHolder().addCallback(this); // 描画用の準備 paint = new Paint(); paint2 = new Paint(); //TODO:コンテンツ領域が取れてないのでとる。 WindowManager wm = (WindowManager)getSystemService(WINDOW_SERVICE); // ディスプレイのインスタンス生成 Display disp = wm.getDefaultDisplay(); Point size = new Point(); disp.getSize(size); view_w = size.x; view_h = size.y; Log.d("view", "width: "+view_w); Log.d("view", "height: "+view_h); //ボールを生成 ball = new Ball(view_w/2, view_h/3 , view_w, view_h ); // バーを生成 view_w/2画面の半分 bar = new Bar( view_w/2 , view_h - 250 ); touchX = bar.x;//タッチしてないとき0なので count = new Points(); } @Override public void surfaceChanged(SurfaceHolder holder, int format, int width, int height) { } // SurfaceView生成時に呼び出されるメソッド。 @Override public void surfaceCreated(SurfaceHolder holder) { // 背景 Canvas canvas = holder.lockCanvas(); canvas.drawColor(Color.BLACK); holder.unlockCanvasAndPost(canvas); // スレッド開始 mainLoop = new Thread(this); mainLoop.start(); } @Override public void surfaceDestroyed(SurfaceHolder holder) { } @Override public boolean onTouchEvent(MotionEvent event) { touchX =event.getRawX(); touchY=event.getRawY(); touchAction=event.getAction(); //720 1280 bar.y = view_w - 250 1080 タッチできないようにする範囲の上ライン float upperLine = bar.y-bar.height-margin;//1065 switch (touchAction) { case MotionEvent.ACTION_DOWN: flag = false; break; case MotionEvent.ACTION_MOVE: //上側だけタッチできないように if(touchX > 0 && touchX < view_w && touchY > 0 && touchY < upperLine ) flag = false; else flag = true; break; case MotionEvent.ACTION_UP: flag =false; break; } return true; } public void collisionDetection(){ if (bar.collideWidth(ball,bar)){ if ( ball.x + ball.size >= bar.x - bar.halfBar && ball.x - ball.size <= bar.x + bar.halfBar ) { ball.vy *= -1; count.count(50); } else if ( ball.y + ball.size >= bar.y - bar.height && ball.y - ball.size <= bar.y + bar.height) { ball.vx *= -1; count.count(50); } } } @Override public void run(){ while (true) { if (width >0 || height > 0) canvas = getHolder().lockCanvas(); if (canvas != null) { //背景 canvas.drawColor(Color.BLACK); count.paintPoints(canvas); canvas.drawText("残機:"+ Life.life, 630, 60, paint6); //スコア表示 // canvas.drawText("点数:"+ count, 10, 30, paint3); // draw(canvas); //Log.d("point","x"+ count.x); Log.d("point","y"+ count.y); //max720 1280 画面サイズ num =(view_w - 32 + margin)/(blockWidth *2);//横ブロックの個数. 端のmargin 16*2 //配列の初期化 //配列の初期化 block = new Block[num]; block2 = new Block[num]; block3 = new Block[num]; block4 = new Block[num]; block5 = new Block[num]; block6 = new Block[num]; block7 = new Block[num]; block8 = new Block[num]; block9 = new Block[num]; block10 = new Block[num]; // //ブロックを描画する // for(int i=0; i <24 ;i+=1) { // // canvas.drawRect(block[i].x -40,block[i].y -10, block[i].x +40, block[i].y +10, paint3); // // } //バーを描画する left top right bottom canvas.drawRect(bar.x - bar.halfBar , bar.y -bar.height , bar.x + bar.halfBar , bar.y + bar.height , paint3); //Ballクラスからボールを描画 canvas.drawCircle( ball.x, ball.y, ball.size , paint5); //バーのスライド移動 if(flag) bar.move(touchX); //Ballクラスのボールを移動 ball.move(); // ball.x += ball.vx; // ball.y += ball.vy; //TODO:ボールの中心で判定されてるので、ボールの幅で判定するようにする(Option) //壁に衝突 // if (ball.x < 0 || getWidth() < ball.x) ball.vx *= -1; // if (ball.y < 0 || getHeight() < ball.y) ball.vy *= -1; // if (ball.y < 0) ball.vy *= -1;//ゲームオーバー用 //ボールとバーの当たり判定 collisionDetection(); //TODO:玉とブロックの衝突判定 ブロック消失 玉反発 音がなる ball.gameOver(); if(!ball.isLive) { canvas.drawText("Game Over",view_w/2,view_h/2,paint7); } getHolder().unlockCanvasAndPost(canvas); } } } public void onDraw(Canvas c){ count.paintPoints(canvas); } } }
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。