2013-05-05 99 views
0

我想從具有線程的活動(實際上它在活動的SurfaceView中)移動到其他活動。但是當我運行它時,它有時會起作用,有時它沒有響應(它會轉移到其他活動,但活動會凍結)。我現在很沮喪。我到處搜索,但我無法找到我的問題的答案。有人請幫助我。 :(從具有線程的活動開始其他活動

我會告訴你的代碼,如果你需要其他的代碼片段,我會展示給你

下面是活動代碼:。

public class MapActivity extends Activity { 

    private static final String TAG = MapActivity.class.getSimpleName(); 
    private DisplayMetrics metrics; 
    private MapView mapView; 

    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     requestWindowFeature(Window.FEATURE_NO_TITLE); 
     DrawableManager.initInstance(getApplicationContext()); 
     getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, 
       WindowManager.LayoutParams.FLAG_FULLSCREEN); 
     Log.d(TAG, "start game activity"); 
     metrics = new DisplayMetrics(); 
     getWindowManager().getDefaultDisplay().getMetrics(metrics); // dapetin 
                    // ukuran 
                    // layar 
                    // mapView = 
                    // new 
                    // MapView(this, 
                    // metrics.widthPixels, 
                    // metrics.heightPixels); 
     MapView.initInstance(getApplicationContext(), metrics.widthPixels, 
       metrics.heightPixels); 
     setContentView(MapView.getInstance()); 
    } 

    @Override 
    public void onPause() { 
     super.onPause(); 
     MapView.getInstance().thread.setRunning(false); 
    } 
} 

這裏是SurfaceView代碼(我不給你的代碼的一部分,因爲它會太長):

public class MapView extends SurfaceView implements SurfaceHolder.Callback { 
    public final Random rand; 
    private Resources res; 
    private static MapView instance; 
    private static int screenWidth; 
    private static int screenHeight; 
    private static final String TAG = MapView.class.getSimpleName(); 
    public mapThread thread; 
    private Matrix matrix = new Matrix(); 
    public static Block blockTile[][]; 
    private Player player; 
    public static int tileSize; 
    public static boolean isMalam = false; 
    public static boolean isAdaMonster = false; 
    private ArrayList<Monster> monsters; 
    public static int leftMargin; 
    public static final int nTileH = 10; 
    public static final int nTileW = 17; 
    private int ctrMonster = 0; 
    private int ctrDetik = 0; 
    private int ctrMenit = 0; 
    private int ctrJam = 6; 
    private int ctrHari = 0; 
    private static boolean isOutdoor; 

    private Pair<Float, Float> tapPos; 
    private Pair<Float, Float> tapPos2; 
    private Context parentActivity; 
    private boolean isTouch = false; 
    private boolean Found = false; 
    private Street street; 
    private Store store; 
    private Combinatorium combinatorium; 
    private Stadium stadium; 
    private Home home; 
    private AreaLuar3 arealuar3; 
    private AreaLuar2 arealuar2; 
    private AreaLuar1 arealuar1; 
    private String dataMap[]; 
    public static String currentMap; 

    public MapView(Context context, int scrWidth, int scrHeight) { 
     super(context); 
     parentActivity = context; 
     blockTile = new Block[nTileW][nTileH]; 
     screenWidth = scrWidth; 
     screenHeight = scrHeight; 
     int sTile = scrHeight/nTileH; 
     tileSize = sTile; 
     getHolder().addCallback(this); 
     res = context.getResources(); 
     rand = new Random(); 
     dataMap = new String[15]; 
     currentMap = "street"; 
     player = new Player(7, 4); 
     build(); 
     setFocusable(true); 
    } 

    public void surfaceCreated(SurfaceHolder arg0) { 
     // inisialisasi thread 
     initThread(); 
     Log.d(TAG, "surface created"); 
    } 

    public void surfaceDestroyed(SurfaceHolder arg0) { 
     releaseThread(); 
     Log.d(TAG, "surface destroyed"); 
    } 

    public static void initInstance(Context context, int W, int H) { 
     assert instance == null; 
     instance = new MapView(context, W, H); 
    } 

    public static MapView getInstance() { 
     assert instance != null; 
     return instance; 
    } 

    // inisialisasi thread 
    public void initThread() { 
     if (thread == null || !thread.isAlive()) { 
      thread = new mapThread(getHolder(), this); 
      thread.start(); 
     } 
     thread.setRunning(true); 
    } 

    private void releaseThread() { 
     boolean retry = true; 
     while (retry) 
      try { 
       thread.join(); 
       retry = false; 
       thread = null; 
      } catch (InterruptedException e) { 
      } 
    } 

    public void update() { 
     ctrDetik++; 
     if (ctrDetik > 59) { 
      ctrDetik = 0; 
      ctrMenit++; 
      if (ctrMenit > 59) { 
       ctrMenit = 0; 
       ctrJam++; 
       cekMalam(); 
       if (ctrJam > 23) { 
        ctrJam = 0; 
        ctrHari++; 
       } 
      } 
     } 
     if (!Found && isAdaMonster) { 
      ctrMonster++; 
      for (Monster mons : monsters) { 
       if (mons.cekEqualBlock(player)) { 
        Found = true; 
        Intent test = new Intent(parentActivity, MainMenu.class); 
        test.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK); 
        parentActivity.startActivity(test); 
       } 
       if (Found) 
        break; 
       if (ctrMonster == 30) { 
        ctrMonster = 0; 
        monsters.get(0).moveRandom(); 
        monsters.get(1).moveCloser(player.getXBlock(), 
          player.getYBlock()); 
       } 
      } 
     } 
     if (isTouch) 
      if (Math.abs(tapPos2.first - tapPos.first) > 100 
        || Math.abs(tapPos2.second - tapPos.second) > 100) 
       if (Math.abs(tapPos2.first - tapPos.first) > Math 
         .abs(tapPos2.second - tapPos.second)) { 
        if (tapPos2.first - tapPos.first > 0) 
         player.move(2); 
        else 
         player.move(4); 
       } else if (Math.abs(tapPos2.first - tapPos.first) < Math 
         .abs(tapPos2.second - tapPos.second)) 
        if (tapPos2.second - tapPos.second > 0) 
         player.move(3); 
        else 
         player.move(1); 
    } 
} 

這裏是線程代碼:

package com.map; 

import android.graphics.Canvas; 
import android.view.SurfaceHolder; 

public class mapThread extends Thread { 
    private boolean running; 
    private SurfaceHolder surfaceHolder; 
    private MapView mapView; 
    private final static int MAX_FPS = 60; // fps yang 
              // diinginkan 
    private final static int MAX_FRAME_SKIPS = 5; // maksimum 
                // jumlah 
                // frame 
                // yang bisa 
                // diskip 
    private final static int FRAME_PERIOD = 1000/MAX_FPS; 

    public mapThread(SurfaceHolder surfaceHolder, MapView gameMapView) { 
     super(); 
     this.surfaceHolder = surfaceHolder; 
     mapView = gameMapView; 
    } 

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

    @Override 
    public void run() { 
     Canvas canvas; 
     long beginTime; // waktu mulai siklus 
     long timeDiff; // waktu yang diperlukan satu siklus untuk selesai 
     int sleepTime; // ms untuk tidur(<0 jika ketinggalan) 
     int framesSkipped; // jumlah frame yang akan diskip 

     sleepTime = 0; 

     while (running) { 
      canvas = null; 
      // ngunci canvas untuk digambar 
      try { 
       canvas = surfaceHolder.lockCanvas(); 
       synchronized (surfaceHolder) { 
        beginTime = System.currentTimeMillis(); 
        framesSkipped = 0; // reset jumlah frame yang pengen diskip 
        // update game state 
        // draw canvas di panel 
        mapView.update(); 
        mapView.render(canvas); 
        // hitung berapa lama satu siklus 
        timeDiff = System.currentTimeMillis() - beginTime; 
        // hitung waktu tidur 
        sleepTime = (int) (FRAME_PERIOD - timeDiff); 

        if (sleepTime > 0) 
         // tidurin thread selama waktu tidur tsb 
         // cycle lebih cepat dari fps 
         try { 
          Thread.sleep(sleepTime); 
         } catch (InterruptedException e) { 
         } 

        while (sleepTime < 0 && framesSkipped < MAX_FRAME_SKIPS) { 
         // ketinggalan fps, update tanpa manggil render 
         mapView.update(); 
         sleepTime += FRAME_PERIOD; 
         framesSkipped++; 
        } 
       } 
      } finally { 
       // in case of an exception the surface is not left in 
       // an inconsistent state 
       if (canvas != null) 
        surfaceHolder.unlockCanvasAndPost(canvas); 
      } 
     } 
    } 
} 

startActivity在SurfaceView代碼中的更新方法中執行。我該怎麼辦?真的需要幫助。

+0

在這個意義上所發生的事情不迴應... – Pragnani 2013-05-05 13:21:17

+0

對不起,我不明白你的意思:( – darkstallion 2013-05-05 13:26:51

+0

'有時它的工作原理,有時不是responding'我的意思是, – Pragnani 2013-05-05 13:28:35

回答

0

SurfaceView擴展視圖,因此您可以調用View.post()方法。 在您當前啓動您想要執行此操作的代碼中。 如果你想讓它更清潔,你可以調用另一個函數來做到這一點。

post(new Runnable() { 
    @Override 
    public void run() { 
     Intent test = new Intent(parentActivity, MainMenu.class); 
     test.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK); 
     parentActivity.startActivity(test);   
}); 

這將調用mainThread上的startActivity。

+0

我試過你建議的方法,但結果是一樣的。 – darkstallion 2013-05-05 17:22:56