2014-12-04 100 views
1

我是新手到Android,它的我AndroidLauncherjava文件intersial代碼上ADMOB-java.lang.IllegalStateException:isLoaded必須調用主UI線程

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


     RelativeLayout layout = new RelativeLayout(this); 
     AndroidApplicationConfiguration config = new AndroidApplicationConfiguration(); 


     requestWindowFeature(Window.FEATURE_NO_TITLE); 
     getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, 
       WindowManager.LayoutParams.FLAG_FULLSCREEN); 
     getWindow().clearFlags(WindowManager.LayoutParams.FLAG_FORCE_NOT_FULLSCREEN); 

     // Create the libgdx View 
     View gameView = initializeForView(new MyGdxGame(this),config); 


     AdView adView = new AdView(this); 
     adView.setAdUnitId("ca-app-pub-6916351754834612/9855033027"); 
     adView.setAdSize(AdSize.BANNER); 
     adView.loadAd(new AdRequest.Builder() 
     .build()); 

     layout.addView(gameView); 

     // Add the AdMob view 
     RelativeLayout.LayoutParams adParams = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT, RelativeLayout.LayoutParams.WRAP_CONTENT); 
     adParams.addRule(RelativeLayout.ALIGN_PARENT_BOTTOM, RelativeLayout.TRUE); 
     adParams.addRule(RelativeLayout.CENTER_HORIZONTAL, RelativeLayout.TRUE); 
     adView.setLayoutParams(adParams); 
     adView.setBackgroundColor(Color.BLACK); 

     layout.addView(adView, adParams); 


     iAd = new InterstitialAd(this); 
     iAd.setAdUnitId(AD_UNIT_ID); 
     loadInterstitial(); 
     iAd.setAdListener(new AdListener() { 
      @Override 
      public void onAdLoaded() { 
       } 

      @Override 
      public void onAdFailedToLoad(int errorCode) { 

      } 
     }); 
     setContentView(layout); 
     } 

    public void loadInterstitial() { 
     /*AdRequest adRequest = new AdRequest.Builder() 
     .addTestDevice(AdRequest.DEVICE_ID_EMULATOR) 
     .addTestDevice("0FD328B10106BD9B2BE832163D43D085") 
     .build();*/ 
     AdRequest adRequest = new AdRequest.Builder().build(); 
     iAd.loadAd(adRequest); 

     //iAd.loadAd(adRequest); 
    } 

    public void showInterstitial() { 
     if (iAd.isLoaded()) { 
      iAd.show(); 
     } else { 
      //Log.d(TAG, "Interstitial ad is not loaded yet"); 
     } 
    } 

它我的插頁式廣告代碼;我得到java.lang.IllegalStateException: isLoaded must be called on the main UI thread這裏.Checked幾個解決方案,可以不固定。我才知道的iAd是不是loading.Can任何人幫助我,我哪裏錯了。謝謝你在前進

+0

我張貼我的解決方案在http://stackoverflow.com/questions/28501787/admob-interstitial-and-error-isloaded-must-be-called-on-the-main-ui-thread/34845549#34845549 – 2016-01-18 00:47:43

回答

4

您沒有顯示showInterstitial()被調用的地方,其中包含對isLoaded的調用!
請檢查堆棧跟蹤以獲取更多線索。

盲目我要說的是,你從一個事件偵聽器或其他(GDX?)後臺線程調用showInterstitial。如果是這樣的話,你有兩個選擇:

showInterstitial()防呆

public void showInterstitial() { 
    if(Looper.myLooper() != Looper.getMainLooper()) { 
     runOnUiThread(new Runnable() { 
      @Override public void run() { 
       doShowInterstitial(); 
      } 
     }); 
    } else { 
     doShowInterstitial(); 
    } 
} 
private void doShowInterstitial() { 
    if (iAd.isLoaded()) { 
     iAd.show(); 
    } else { 
     //Log.d(TAG, "Interstitial ad is not loaded yet"); 
    } 
} 

製作調用代碼聰明

void myMethodCallingShowInterstitial() { 
    ... doing my other background stuff ... 
    // replace showInterstitial(); with below: 
    activityReference.runOnUiThread(new Runnable() { 
     @Override public void run() { 
      activityReference.showInterstitial(); 
     } 
    }); 
    ... doing my other background stuff ... 
} 

在上述兩種情況下,任何你runOnUiThread後做不能依靠的間質性正在顯示!如果你不小心同時實現,因爲萬無一失的方法,不會再張貼到UI這不是一個問題。

作爲runOnUiThread的替代方案,您可以使用Handler,更多信息請參閱the official documentation on this

+0

謝謝:)優秀解釋 – 2014-12-05 15:53:57

3

你好,你可以用runOnUiThread

public void showInterstitial() { 
runOnUiThread(new Runnable() { 
    public void run() { 
     if (iAd.isLoaded()) { 
      iAd.show(); 
     } else { 
      //Log.d(TAG, "Interstitial ad is not loaded yet"); 
     } 
    } 
}); 

}

希望這有助於嘗試。

相關問題