2014-12-05 68 views
0

我嘗試在MainActivity之外顯示廣告。如果我把showAd在這個類它的工作好,並顯示我的廣告,但是當我通過實現界面我的遊戲類調用這個函數我有一個錯誤: 「要求主線程」在課堂之外撥打MainActivity功能

MMRequest request = new MMRequest(); ; 
    MMInterstitial interstitial; 
    @Override 
    public void onCreate(Bundle savedInstanceState) 
    { 
     super.onCreate(savedInstanceState); 
     MMSDK.initialize(this); 
     interstitial = new MMInterstitial(this);    
     interstitial.setMMRequest(request); 
     interstitial.setApid("xxxxxx"); 

     AndroidApplicationConfiguration cfg = new AndroidApplicationConfiguration();  

     RelativeLayout layout = new RelativeLayout(this); 

     View gameView = initializeForView(new JumpJackieJump(new RequestHandler(), this), cfg); 
     layout.addView(gameView);   

     setContentView(layout); } 

@Override 
public void showAd() 
{ 
    interstitial.fetch(); 
    interstitial.setListener(new RequestListenerImpl() 
    { 
     @Override 
     public void requestCompleted(MMAd mmAd) 
     { 
      interstitial.display();    
     }   
}); 
+0

請發表較完整的代碼。上面的代碼片段並不清楚。 – 2014-12-05 13:38:07

回答

0

這是因爲主線程的UI線程。 (請記住,這僅適用於Android - 更多參考請參閱this)。

根據Android文檔,您應該使用avoid updating the UI outside the UI thread

Additionally, the Andoid UI toolkit is not thread-safe. So, you must not manipulate your UI from a worker thread—you must do all manipulation to your user interface from the UI thread.

這是說,你可以做你在找什麼東西像this

+0

謝謝。有用! ;) – metalcracker 2014-12-05 13:58:22