从其他活动

时间:2015-05-14 20:00:42

标签: android android-activity surfaceview back

我正在制作游戏,目的是杀死僵尸,并升级你的武器。但我有问题,当我试图暂停我的游戏,去购物,我没有问题,但当我试图回到游戏 - 应用程序崩溃。这是代码:

   public GameView(Context context) {
         super(context);
         gameLoopThread = new GameLoopThread(this);

         getHolder().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) {
                       //createSprites();

                       gameLoopThread.setRunning(true);
                       gameLoopThread.start();
                       thread1.start();
                       thread_level.start();
                       wielkosc_czcionki=getWidth()/25;

                }

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


        if (!is_game) // is not running
        {
                    paused=!paused;
                    is_game=!is_game;
            }
            else{ // is running
                is_game=!is_game;
                paused=!paused;
                startuj_sklep();
                //startuj_sklep();
            }

        }
       Thread thread_level = new Thread(){
        public void run(){
            while(true){
            if(akt_zabit%(int)level_zabit==0){
            level++;    
            level_zabit=level_zabit*1.5;
            resp_time=resp_time-20;
            pause();


            }

            }
        }

      };    


 void startuj_sklep(){

     Context context = getContext(); // from MySurfaceView/Activity
     Intent intent = new Intent(context, Sklep.class);
     int tab[]={dolary,zycie_gracza_akt,zycie_gracza_def,poj_mag,szybkosc_strzalu, reload_time_def};
     intent.putExtra("dane", tab);
     context.startActivity(intent);

 }

所以"暂停"方法是让整个游戏停止,然后使用" startuj_sklep"(engish:start_shop)。 " startuj_sklep"方法开始新的活动。 它正在转变新的活动,但当它崩溃时。

           back.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();

            }
        });
  

05-14 15:48:52.031:E / AndroidRuntime(1272):致命异常:主要   05-14 15:48:52.031:E / AndroidRuntime(1272):java.lang.IllegalThreadStateException:线程已经启动   05-14 15:48:52.031:E / AndroidRuntime(1272):at java.lang.Thread.checkNotStarted(Thread.java:871)   05-14 15:48:52.031:E / AndroidRuntime(1272):at java.lang.Thread.start(Thread.java:1025)   05-14 15:48:52.031:E / AndroidRuntime(1272):at Morisson.zombieapocalypse.GameView $ 3.surfaceCreated(GameView.java:149)   05-14 15:48:52.031:E / AndroidRuntime(1272):在android.view.SurfaceView.updateWindow(SurfaceView.java:571)   05-14 15:48:52.031:E / AndroidRuntime(1272):在android.view.SurfaceView.onWindowVisibilityChanged(SurfaceView.java:232)   05-14 15:48:52.031:E / AndroidRuntime(1272):在android.view.View.dispatchWindowVisibilityChanged(View.java:7682)   05-14 15:48:52.031:E / AndroidRuntime(1272):在android.view.ViewGroup.dispatchWindowVisibilityChanged(ViewGroup.java:1071)   05-14 15:48:52.031:E / AndroidRuntime(1272):在android.view.ViewGroup.dispatchWindowVisibilityChanged(ViewGroup.java:1071)   05-14 15:48:52.031:E / AndroidRuntime(1272):在android.view.ViewGroup.dispatchWindowVisibilityChanged(ViewGroup.java:1071)   05-14 15:48:52.031:E / AndroidRuntime(1272):在android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:1227)   05-14 15:48:52.031:E / AndroidRuntime(1272):在android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:1004)   05-14 15:48:52.031:E / AndroidRuntime(1272):在android.view.ViewRootImpl $ TraversalRunnable.run(ViewRootImpl.java:5481)   05-14 15:48:52.031:E / AndroidRuntime(1272):在android.view.Choreographer $ CallbackRecord.run(Choreographer.java:749)   05-14 15:48:52.031:E / AndroidRuntime(1272):在android.view.Choreographer.doCallbacks(Choreographer.java:562)   05-14 15:48:52.031:E / AndroidRuntime(1272):在android.view.Choreographer.doFrame(Choreographer.java:532)   05-14 15:48:52.031:E / AndroidRuntime(1272):在android.view.Choreographer $ FrameDisplayEventReceiver.run(Choreographer.java:735)   05-14 15:48:52.031:E / AndroidRuntime(1272):在android.os.Handler.handleCallback(Handler.java:730)   05-14 15:48:52.031:E / AndroidRuntime(1272):在android.os.Handler.dispatchMessage(Handler.java:92)   05-14 15:48:52.031:E / AndroidRuntime(1272):在android.os.Looper.loop(Looper.java:137)   05-14 15:48:52.031:E / AndroidRuntime(1272):在android.app.ActivityThread.main(ActivityThread.java:5103)   05-14 15:48:52.031:E / AndroidRuntime(1272):at java.lang.reflect.Method.invokeNative(Native Method)   05-14 15:48:52.031:E / AndroidRuntime(1272):at java.lang.reflect.Method.invoke(Method.java:525)   05-14 15:48:52.031:E / AndroidRuntime(1272):at com.android.internal.os.ZygoteInit $ MethodAndArgsCaller.run(ZygoteInit.java:737)   05-14 15:48:52.031:E / AndroidRuntime(1272):at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)   05-14 15:48:52.031:E / AndroidRuntime(1272):at dalvik.system.NativeStart.main(Native Method)

1 个答案:

答案 0 :(得分:0)

surfaceCreated()中匿名类中的GameView回调试图启动已经运行的线程,所以你得到了

java.lang.IllegalThreadStateException: Thread already started

添加一些日志记录以查看回调何时触发以及线程何时启动和停止。这样可以让您更好地了解应用与系统的互动方式。

您可能还会感兴趣this article

(我不知道这是否是问题的一部分,但我强烈建议您不要让pause()方法充当切换。您应该始终知道对pause()的调用是否预期暂停或恢复,并将其写为切换会产生不必要的歧义。如果游戏暂停,并且您尝试暂停第二次,则应忽略第二个请求或报告错误,而不是尝试恢复它。)