2011-12-06 38 views
1

在我的應用程序中,我正在使用一個畫布和一些對象。這個實現是在一個獨立的線程中完成的,這個線程叫做mythread.in特定的場景,我想在我的screen.first上顯示一個警告對話框,通過使用下面給出的功能。如何在我的程序中使用處理程序?

public void StopChecking() 
{ 
    if(stopgameflag==true) 
    { 
     context=MyActivity.this; 
     AlertDialog.Builder builder = new AlertDialog.Builder(context); 
     builder.setMessage("Game Over !!!") 
       .setCancelable(false) 
       .setPositiveButton("Play Again", 
         new DialogInterface.OnClickListener() { 

          @Override 
          public void onClick(DialogInterface dialog,int which) { 
           setContentView(R.layout.main); 
           dialog.cancel(); 
          } 
         }) 
       .setNegativeButton("Exit", 
         new DialogInterface.OnClickListener() { 

          @Override 
          public void onClick(DialogInterface dialog,int which) { 
           dialog.cancel(); 

          } 
         }); 


     Log.d(TAG, "Stopping...oooooooooooooooooooooooooooooo"); 

    } 
} 

這是一個寫在MyActivity類(主要活動)中的函數,但它崩潰了。 有人說,處理程序是做這件事的最佳方法。我搜索了相同的,但我什麼都不明白..
任何人都可以告訴我,我如何實現處理程序來顯示警告對話框...

+0

如果你想撞車幫,然後張貼在logcat的堆棧跟蹤 – Craigy

+0

確保Craigy .... 11月12日至6日:22:21.263:ERROR/AndroidRuntime( 366):java.lang.IllegalStateException:系統服務不可用於onCreate之前的活動() ..這是logcat中的錯誤 –

+0

Re將你在onCreate()上面放的代碼移動到 – xDragonZ

回答

1

呼叫遊戲結束條件檢查後,此功能

if(gameover) { 
    StopChecking(); 
} 


public void StopChecking() { 

    final AlertDialog alertDialog = new AlertDialog.Builder(this).create(); 
    alertDialog.setTitle("*GameOver*");   
    alertDialog.setButton("Exit", new DialogInterface.OnClickListener() { 
     public void onClick(DialogInterface dialog, int which) {          

      return; 
      } 
     }); 
     alertDialog.setButton2("Play Again", new DialogInterface.OnClickListener() { 
      public void onClick(DialogInterface dialog, int which) { 

       // start game again      
       return; 
      } 
     }); 
     alertDialog.show();  
} 
+0

我試過這個..但是我的應用程序崩潰了,並且給出錯誤.... 12-06 11:33:29.384:ERROR/AndroidRuntime(404):java.lang.RuntimeException:無法在線程內創建處理程序,但尚未調用Looper.prepare() –

+0

您需要什麼.. –

+0

我只是想在特定場景中顯示警告對話框...實際上這是一個遊戲..當遊戲結束時,我必須顯示一個警告對話框,其中有兩個選項「再次玩」和「退出」。我的遊戲寫在名爲mythread的線程中。 –

相關問題