我无法在游戏中显示间隙添加的自然突破点。 现在我可以加载并显示这些插页式插件,即使我正在更改活动。
我的主要问题是,当这些添加显示时,我无法自行决定。
我使用OpenGl ES并使用badlogic框架。因此,当我切换屏幕时,每次反复调用主要活动。
这就是我现在创建的,通过使用我的共享首选项和一个小助手类,我能够跟踪添加的哪个阶段。
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()上出现了nullpointer错误
这是一些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)
有人知道如何在我想要的时候显示这些加载的添加内容吗?偏好只是我正在玩的一个想法。
答案 0 :(得分:1)
首先,要解决NPE,您必须在广告关闭或无法加载时重建插页式广告。你可以在onAdClosed()和onAdFailed()回调中执行此操作:
public abstract class GLGame extends Activity implements Game, Renderer{
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
interstitial = new InterstitialAd(GLGame.this);
interstitial.setAdUnitId("*****");
reloadInterstitials();
// Create ad request
AdRequest adRequest = new AdRequest.Builder().build();
// Begin loading your interstitial
interstitial.loadAd(adRequest);
}
private void reloadInterstitials(){
interstitial.setAdListener(new AdListener() {
@Override
public void onAdLoaded() {
// TODO Auto-generated method stub
super.onAdLoaded();
}
@Override
public void onAdFailedToLoad(int errorCode) {
// TODO Auto-generated method stub
super.onAdFailedToLoad(errorCode);
interstitial = new InterstitialAd(GLGame.this);
interstitial.setAdUnitId("*****");
// Begin loading your interstitial
interstitial.loadAd(new AdRequest.Builder().build());
loadInterCallBacks();
}
@Override
public void onAdOpened() {
// TODO Auto-generated method stub
super.onAdOpened();
}
@Override
public void onAdClosed() {
// TODO Auto-generated method stub
super.onAdClosed();
interstitial = new InterstitialAd(GLGame.this);
interstitial.setAdUnitId("****");
loadInterCallBacks();
// Begin loading your interstitial
interstitial.loadAd(new AdRequest.Builder().build());
}
@Override
public void onAdLeftApplication() {
// TODO Auto-generated method stub
super.onAdLeftApplication();
}
});
}
//显示插页式广告的方法
public void displayInterstitial(){
if(interstitial.isLoaded())
interstitial.show();
}
}
要回答主要问题,您可以使用收听者在需要时显示已加载的广告。
第1步:定义界面
public interface MyInterstitialListener {
public void showInterstitial();
}
第2步:让GLGame实现界面:
public abstract class GLGame extends Activity implements Game, Renderer, MyInterstitialListener{
public void showInterstitial() {
runOnUiThread(new Runnable(){
@Override
public void run() {
// TODO Auto-generated method stub
displayInterstitial();
} });
}
}
然后最后在任何Screen类中显示广告:
public class LevelOneScreen extends GLScreen {
MyInterstitialListener mL;
public LevelOnecreen(Game game) {
super(game);
mL = (MyInterstitialListener)game;
mL.showInterstitial();
}
}
你可以在游戏中的自然突破点上调用MyInterstitialListener#showInterstitial()。如果您想在游戏循环中调用它,请使用布尔标志来调用它一次:
public void update(float deltaTime) { //game loop.
if(!adCalled){
mL.showInterstitial();
adCalled = true;
}
}
答案 1 :(得分:0)
请参阅以下代码。
private void displayAdmobInterstitialAd(InterstitialAd pAd){
if(pAd.isLoaded()){
pAd.show();
}
}
public void popAdmobInterstitialAd(){
final InterstitialAd adMobInterstitialAd = new InterstitialAd(ResourcesManager.getInstance().activity);
adMobInterstitialAd.setAdUnitId("Your AD unit ID");
final AdRequest adRequest = new AdRequest.Builder().build();
ResourcesManager.getInstance().getActivity().runOnUiThread(new Runnable() {
@Override
public void run() {
adMobInterstitialAd.loadAd(adRequest);
}
});
adMobInterstitialAd.setAdListener(new AdListener() {
public void onAdLoaded(){
displayAdmobInterstitialAd(adMobInterstitialAd);
}
});
}
我在我的andengine项目的sceneManager类中使用此方法。 只需在runOnUiThread上执行loadAd ......就是这样。
<强> my android app 强>