2013-03-23 56 views
2

我正在開發一個簡單的Android遊戲應用程序。在我的主要活動(用於播放的活動)期間,當條件得到驗證時,1/2秒後,我想停止當前活動並重新啓動它。遊戲有時候效果很好,但是經常隨機地,它會「沒有錯誤」地崩潰[它不會重新開始活動,但會關閉返回到遊戲菜單活動的視圖(用於菜單,選項等的活動。 。)]。RemoteException發送setActive(false)通知 - 隨機活動關閉

以下是一段代碼時發生的問題:

if(scoreManager.isEndGame()){ 
        final Handler mHandler = new Handler(); 
        Runnable mUpdateTimeTask = new Runnable() { 
         public void run() { 
          long remainingTime = scoreManager.getSecondsUntilFinished(); 
          scoreManager.setRemainingTime(remainingTime*1000); 
          finish(); 
          startActivity(getIntent()); 
         } 

        }; 
        mHandler.postDelayed(mUpdateTimeTask, 500); 
       } 

scoreManager包含在每個istantiated與REMAININGTIME重啓CountDownTimer(它被實例化上的onCreate())。

在logcat中我讀到:

InputMethodManagerService:GOT的RemoteException發送 SETACTIVE(假)通知爲PID 18494 UID 10062

I/ActivityManager:顯示com.myproject.activities/.MenuActivity: + 774ms(總計+3s697ms)

基本上經常活動被破壞,它不會重新啓動。 不會引發異常。我在「HTC Desire」上測試了這個問題。

請幫忙。

回答

1

已解決!!!

最後我發現了這個問題,它是關於主要活動的「onCreate」方法。 試圖隨機設置背景圖像,有時選定的圖像的分辨率過大(1024x768或更大)(R.drawable.background_3和R.drawable.background_5),因此主要活動被中斷並返回到MenuActivity。我解決了將所有背景圖像的分辨率設置爲640x480。

public void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 

    try{ 
     //stuff... 

     //set background 
     LinearLayout linLay = (LinearLayout) findViewById(R.id.mainLayout); 
     linLay.setBackgroundResource(getBackgroundCode(new Random().nextInt(12))); 

     //other stuff... 
    }catch(Exception e){ 
     Log.e("MainPlayActivity", "onCreate() - Error during cretion. Exception: "+e.getMessage(), e); 
    } 
} 



private int getBackgroundCode(int n){ 

    int result=0; 

    switch (n){ 
    case 0: 
     result = R.drawable.background_0; 
     break; 
    case 1: 
     result = R.drawable.background_1; 
     break; 
    case 2: 
     result = R.drawable.background_2; 
     break; 
    case 3: 
     result = R.drawable.background_3; 
     break; 
    case 4: 
     result = R.drawable.background_4; 
     break; 
    case 5: 
     result = R.drawable.background_5; 
     break; 
    case 6: 
     result = R.drawable.background_6; 
     break; 
    case 7: 
     result = R.drawable.background_7; 
     break; 
    case 8: 
     result = R.drawable.background_8; 
     break; 
    case 9: 
     result = R.drawable.background_9; 
     break; 
    case 10: 
     result = R.drawable.background_10; 
     break; 
    case 11: 
     result = R.drawable.background_11; 
     break; 
    default : 
     result = R.drawable.background_0; 
    } 
    return result; 
}