【发布时间】:2014-11-19 17:22:23
【问题描述】:
我无法在游戏的自然断点上显示插页式添加。 现在我可以加载和显示这些插页式添加,即使我正在更改活动。
我的主要问题是我无法决定何时显示这些添加。
我使用 OpenGl ES 并使用 badlogic 框架。因此,每次切换屏幕时都会一遍又一遍地调用 mainactivity。
这是我现在创建的,通过使用我的共享首选项和一个小助手类,我可以追踪添加的阶段。
public abstract class GLGame extends Activity implements Game, Renderer {
enum GLGameState {
....
}
GLSurfaceView glView;
GLGraphics glGraphics;
....
public InterstitialAd interstitial;
private static final String AD_UNIT_ID = "****";
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
...
glView = new GLSurfaceView(this);
glView.setRenderer(this);
setContentView(glView);
glGraphics = new GLGraphics(glView);
....
// my shared pref is set to 0 when add is not loaded
if (addcheck.showadd(getApplicationContext())==0){
interstitial = new InterstitialAd(getApplicationContext());
interstitial.setAdUnitId(AD_UNIT_ID);
// Create ad request.
AdRequest adRequest = new AdRequest.Builder()
.addTestDevice(AdRequest.DEVICE_ID_EMULATOR)
.addTestDevice("****")
.build();
// Begin loading your interstitial.
// I set my shared pref to 1 to tell the add is loaded, for later use in the game
interstitial.loadAd(adRequest);
interstitial.setAdListener(new AdListener(){
public void onAdLoaded(){
addcheck.setadd(1,getApplicationContext());
}});
}
// Somewhere in my game I set the pref to 2, where I want to show the add.
if (addcheck.showadd(getApplicationContext())==2&interstitial.isLoaded()){
displayInterstitial();
// after showing the add, I put my pref back to 1, so I it wont show later
addcheck.setadd(1,getApplicationContext());
}
}
public void displayInterstitial() {
if (interstitial.isLoaded()) {
interstitial.show();
}
}
在我的例子中,我将从同一个 mainactivity 中调用 displayInterstitial,但是这个 mainactivity 会重新加载几次。我认为插页式广告不再有效,因为我在 interstitial.isLoaded() 上收到空指针错误
这是一些 logcat 输出,这不是我的主要问题。
07-13 21:49:53.009: E/AndroidRuntime(29230): FATAL EXCEPTION: main
07-13 21:49:53.009: E/AndroidRuntime(29230): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.badlogic.androidgames.glbasics/com.glbasics.MainScreen}: java.lang.NullPointerException
07-13 21:49:53.009: E/AndroidRuntime(29230): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1956)
07-13 21:49:53.009: E/AndroidRuntime(29230): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1981)
有人知道如何在我想要的时候显示这些加载的添加吗?偏好只是我在玩的一个想法。
【问题讨论】:
-
我的解决方案有效吗?
-
我还在努力。我会及时通知你。
-
好的。如果您不想使用界面,请调用 GLGame.showInterstitialAd()。只需确保静态定义 interstitial 和 showInterstital()
-
这实际上是我一段时间的工作,但弹出相同的消息。今晚再试一次。
-
最后一个选项不起作用,因为切换屏幕时会重新加载主屏幕。这个原因: java.lang.IllegalStateException: isLoaded 必须在主 UI 线程上调用。我去试试你说的界面
标签: android admob interstitial