用户William建议我更改我的代码以显示插页式广告
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
this.setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
//setDebugMode(true);
initialiseAccelerometer();
interstitial = new InterstitialAd(this);
interstitial.setAdUnitId(getResources().getString(R.string.InterstitialAd_unit_id));
interstitial.setAdListener(new AdListener() {
public void onAdClosed() {
// Create another ad request.
final AdRequest adRequest = new AdRequest.Builder()
.addTestDevice(AdRequest.DEVICE_ID_EMULATOR)
.build();
interstitial.loadAd(adRequest);
}
});
// Create ad request.
final AdRequest adRequest = new AdRequest.Builder()
.addTestDevice(AdRequest.DEVICE_ID_EMULATOR)
.build();
interstitial.loadAd(adRequest);
}
并展示广告:
public synchronized void GameOver() {
if (lives_left > 0) {
//success! - game passed - save score
ScoreManager.save_localscore_simple(score_times[currentLevel], "" + currentLevel, false);
if (Level_Buttons.length >= currentLevel + 1)
ScoreManager.save_localscore_simple(unlocked, "unlock" + (currentLevel + 1));
if (sound_success != 0 && !sound_muted)
sp.play(sound_success, 1, 1, 0, 0, 1);
//open interstitial ad
if (interstitial.isLoaded()) {
interstitial.show();
}
} else {
//game not passed
if (sound_gameover != 0 && !sound_muted)
sp.play(sound_gameover, 1, 1, 0, 0, 1);
}
//open interstitial ad
if (interstitial.isLoaded()) {
interstitial.show();
}
StopMusic();
state = GAMEOVER;
}
在游戏的每个游戏中,我都会收到此错误:
W/dalvikvm(20469): threadid=11: thread exiting with uncaught exception (group=0x4180bda0)
E/AndroidRuntime(20469): FATAL EXCEPTION: Thread-67543
E/AndroidRuntime(20469): Process: com.test.mygame, PID: 20469
E/AndroidRuntime(20469): java.lang.IllegalStateException: isLoaded must be called on the main UI thread.
E/AndroidRuntime(20469): at com.google.android.gms.common.internal.bh.b(SourceFile:251)
E/AndroidRuntime(20469): at com.google.android.gms.ads.internal.b.e(SourceFile:345)
E/AndroidRuntime(20469): at com.google.android.gms.ads.internal.client.m.onTransact(SourceFile:66)
E/AndroidRuntime(20469): at android.os.Binder.transact(Binder.java:361)
E/AndroidRuntime(20469): at com.google.android.gms.internal.ap$a$a.isReady(Unknown Source)
E/AndroidRuntime(20469): at com.google.android.gms.internal.au.isLoaded(Unknown Source)
E/AndroidRuntime(20469): at com.google.android.gms.ads.InterstitialAd.isLoaded(Unknown Source)
E/AndroidRuntime(20469): at com.test.mygame.MainGame.GameOver(MainGame.java:1128)
E/AndroidRuntime(20469): at com.test.mygame.MainGame.Step(MainGame.java:773)
E/AndroidRuntime(20469): at com.test.nudge.Screen.run(Screen.java:207)
E/AndroidRuntime(20469): at java.lang.Thread.run(Thread.java:841)
这里有什么问题?请尽可能简单地解释我,因为我之前没有任何编程知识的新手:)谢谢!
答案 0 :(得分:16)
这可能不是完整的答案,但很难说出正确的答案,因为上面的代码并不清楚。你没有显示你在哪里使用GameOver(),但我认为你在错误的地方调用它,我认为你在任何后台线程中调用它,因为在GameOver中你调用interstitial.isLoaded()
导致你的问题。就像stacktrace所说的那样,在主ui线程中调用它。例如,在您的GameOver()中调用它:
runOnUiThread(new Runnable() {
@Override public void run() {
if (interstitial.isLoaded()) {
interstitial.show();
}
}
});
可能你必须用活动参考来调用它,这取决于你在哪里调用GameOver():
mYourActivity.runOnUiThread(new Runnable() {
@Override public void run() {
if (interstitial.isLoaded()) {
interstitial.show();
}
}
});
答案 1 :(得分:0)
分享我的案例和解决方案。
我做了新线程并调用了isLoaded()。它得到了同样的错误。 所以我用下面的代码使用Messager来解决它。
希望得到这个帮助。
代码如下。
主线程:
public void onCreate() {
mHandler = new Handler(Looper.getMainLooper()) {
@Override
public void handleMessage(Message message) {
switch (message.what) {
case ACTION_ADMOB_IS_LOADED:
MainActivity.logForDebug(TAG,"AdMob : Check for loaded");
if(mIAds.isLoaded()) {
MainActivity.logForDebug(TAG,"AdMob : Loaded ...... OK");
mIAds.show();
}
break;
}
}
};
}
其他线程
public void run(){
while(true){
Message message;
// ------ For AdMob ------
Message message = mHandler.obtainMessage(ACTION_ADMOB_IS_LOADED,null);
message.sendToTarget();
}
}