线程不会中断,应用程序在恢复时不断崩溃

时间:2013-02-13 22:50:52

标签: java android

背景信息:当我第一次启动我的应用程序时,它会显示一个启动画面大约3秒钟,然后再启动一个男人跳舞的主要活动。

问题:当我点击主页按钮,然后再次单击该应用程序时,它会显示黑屏约6或7秒,然后显示错误消息“您的应用程序已停止响应”。

MainActivity:

public class MainActivity extends Activity {

MediaPlayer ourSong;

/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
            WindowManager.LayoutParams.FLAG_FULLSCREEN);
    requestWindowFeature(Window.FEATURE_NO_TITLE);
    setContentView(new GameView(this));
    ourSong = MediaPlayer.create(MainActivity.this, R.raw.song);
    ourSong.start();
}

@Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
    if (keyCode == KeyEvent.KEYCODE_BACK) {
        // preventing default implementation previous to
        // android.os.Build.VERSION_CODES.ECLAIR
        return true;
    }
    return super.onKeyDown(keyCode, event);
}

@Override
protected void onPause() {
    // TODO Auto-generated method stub
    super.onPause();
    ourSong.release();
}

@Override
protected void onResume() {
    // TODO Auto-generated method stub
    super.onResume();
}

}

Gameview类:

public class GameView extends SurfaceView {
private Bitmap bmp;
private SurfaceHolder holder;
private GameLoopThread gameLoopThread;
private Sprite sprite;

public GameView(Context context) {
    super(context);
    gameLoopThread = new GameLoopThread(this);
    holder = getHolder();
    holder.addCallback(new SurfaceHolder.Callback() {

        @Override
        public void surfaceDestroyed(SurfaceHolder holder) {
            boolean retry = true;
            gameLoopThread.setRunning(false);
            while (retry) {
                try {
                    gameLoopThread.join();
                    retry = false;
                } catch (InterruptedException e) {
                }
            }
        }

        @Override
        public void surfaceCreated(SurfaceHolder holder) {
            gameLoopThread.setRunning(true);

gameLoopThread.start();             }

        @Override
        public void surfaceChanged(SurfaceHolder holder, int format,
                int width, int height) {
        }
    });

    bmp = BitmapFactory.decodeResource(getResources(),
            R.drawable.gangnamiconbothlarge2x2);
    sprite = new Sprite(this, bmp);
}

@Override
protected void onDraw(Canvas canvas) {
    canvas.drawColor(Color.BLACK);
    sprite.onDraw(canvas);
}
}

GameLoopThread:

public class GameLoopThread extends Thread {
   static final long FPS = 10;
   private GameView view;
   private boolean running = false;

   public GameLoopThread(GameView view) {
         this.view = view;
   }

   public void setRunning(boolean run) {
         running = run;
   }

   @Override
   public void run() {
         long ticksPS = 1000 / FPS;
         long startTime;
         long sleepTime;
         while (running) {
                Canvas c = null;
                startTime = System.currentTimeMillis();
                try {
                       c = view.getHolder().lockCanvas();
                       synchronized (view.getHolder()) {
                              view.onDraw(c);
                       }
                } finally {
                       if (c != null) {
                              view.getHolder().unlockCanvasAndPost(c);
                       }
                }
                sleepTime = ticksPS-(System.currentTimeMillis() - startTime);
                try {
                       if (sleepTime > 0)
                              sleep(sleepTime);
                       else
                              sleep(10);
                } catch (Exception e) {}
         }
   }
}

logcat的:

02-13 19:26:39.061: W/dalvikvm(30571): threadid=1: thread exiting with uncaught   exception (group=0x40be61f8)
02-13 19:26:39.061: E/AndroidRuntime(30571): FATAL EXCEPTION: main
02-13 19:26:39.061: E/AndroidRuntime(30571): java.lang.IllegalThreadStateException: Thread already started.
02-13 19:26:39.061: E/AndroidRuntime(30571):     at java.lang.Thread.start(Thread.java:1045)
02-13 19:26:39.061: E/AndroidRuntime(30571):     at com.meteorfiber.gangnamstyle.GameView$1.surfaceCreated(GameView.java:39)
02-13 19:26:39.061: E/AndroidRuntime(30571):     at android.view.SurfaceView.updateWindow(SurfaceView.java:576)
02-13 19:26:39.061: E/AndroidRuntime(30571):     at android.view.SurfaceView.onWindowVisibilityChanged(SurfaceView.java:226)
02-13 19:26:39.061: E/AndroidRuntime(30571):     at android.view.View.dispatchWindowVisibilityChanged(View.java:6008)
02-13 19:26:39.061: E/AndroidRuntime(30571):     at android.view.ViewGroup.dispatchWindowVisibilityChanged(ViewGroup.java:945)
02-13 19:26:39.061: E/AndroidRuntime(30571):     at android.view.ViewGroup.dispatchWindowVisibilityChanged(ViewGroup.java:945)
02-13 19:26:39.061: E/AndroidRuntime(30571):     at android.view.ViewGroup.dispatchWindowVisibilityChanged(ViewGroup.java:945)
02-13 19:26:39.061: E/AndroidRuntime(30571):     at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:1141)
02-13 19:26:39.061: E/AndroidRuntime(30571):     at android.view.ViewRootImpl.handleMessage(ViewRootImpl.java:2628)
02-13 19:26:39.061: E/AndroidRuntime(30571):     at android.os.Handler.dispatchMessage(Handler.java:99)
02-13 19:26:39.061: E/AndroidRuntime(30571):     at android.os.Looper.loop(Looper.java:137)
02-13 19:26:39.061: E/AndroidRuntime(30571):     at android.app.ActivityThread.main(ActivityThread.java:4512)
02-13 19:26:39.061: E/AndroidRuntime(30571):     at java.lang.reflect.Method.invokeNative(Native Method)
02-13 19:26:39.061: E/AndroidRuntime(30571):     at java.lang.reflect.Method.invoke(Method.java:511)
02-13 19:26:39.061: E/AndroidRuntime(30571):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:794)
02-13 19:26:39.061: E/AndroidRuntime(30571):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:561)
02-13 19:26:39.061: E/AndroidRuntime(30571):     at dalvik.system.NativeStart.main(Native Method)

0 个答案:

没有答案