2014-09-02 59 views
1

我一直在试图弄清楚如何将广告添加到我的项目几天。我希望你们能告诉我该怎么做。我试图在第一轮后广告广告(第一次玩家死亡)在他们关闭广告后;它将允许他们再次玩。我做错了什么,如何添加插页式广告

没有广告出现,我收到错误,如:

09-02 17:14:53.825: W/dalvikvm(635): threadid=11: thread exiting with uncaught exception  (group=0x40a13300) 
09-02 17:14:53.835: E/AndroidRuntime(635): FATAL EXCEPTION: GLThread 75 
09-02 17:14:53.835: E/AndroidRuntime(635): java.lang.NullPointerException 
09-02 17:14:53.835: E/AndroidRuntime(635): at  com.JrodManU.LaserJumper.android.Ad.loadAd(Ad.java:45) 
09-02 17:14:53.835: E/AndroidRuntime(635): at  com.JrodManU.LaserJumper.android.AndroidLauncher.loadAd(AndroidLauncher.java:26) 
09-02 17:14:53.835: E/AndroidRuntime(635): at com.JrodManU.LaserJumper.LaserJumper.loadAd(LaserJumper.java:36) 
09-02 17:14:53.835: E/AndroidRuntime(635): at com.JrodManU.LaserJumper.screens.InGameScreen. <init>(InGameScreen.java:51) 
09-02 17:14:53.835: E/AndroidRuntime(635): at  com.JrodManU.LaserJumper.LaserJumper.create(LaserJumper.java:15) 
09-02 17:14:53.835: E/AndroidRuntime(635): at  com.badlogic.gdx.backends.android.AndroidGraphics.onSurfaceChanged(AndroidGraphics.java:236) 
09-02 17:14:53.835: E/AndroidRuntime(635): at  android.opengl.GLSurfaceView$GLThread.guardedRun(GLSurfaceView.java:1505) 
09-02 17:14:53.835: E/AndroidRuntime(635): at  android.opengl.GLSurfaceView$GLThread.run(GLSurfaceView.java:1240) 

我的课:

AndroidLauncher

package com.JrodManU.LaserJumper.android; 

import android.os.Bundle; 

import com.badlogic.gdx.backends.android.AndroidApplication; 
import com.badlogic.gdx.backends.android.AndroidApplicationConfiguration; 
import com.JrodManU.LaserJumper.GameEventListener; 
import com.JrodManU.LaserJumper.LaserJumper; 

public class AndroidLauncher extends AndroidApplication implements GameEventListener { 
    Ad ad = new Ad(); 
    @Override 
    protected void onCreate (Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     AndroidApplicationConfiguration config = new AndroidApplicationConfiguration(); 
     initialize(new LaserJumper(this), config); 
    } 

    @Override 
    public void showAd() { 
     ad.showAd(); 
    } 

    @Override 
    public void loadAd() { 
     ad.loadAd(); 
    } 

    @Override 
    public boolean isShowing() { 
     return ad.isShowing(); 
    } 

    @Override 
    public boolean isLoaded() { 
     return ad.isLoaded(); 
    } 
} 

InGameScreen(Screen类)

package com.JrodManU.LaserJumper.screens; 

import com.JrodManU.LaserJumper.LaserJumper; 

public class InGameScreen implements Screen { 
    LaserJumper game; 
    boolean firstTime; 

    public InGameScreen(LaserJumper game) { 
     this.game = game; 
     game.loadAd(); 
     firstTime = true; 
    } 

    @Override 
    public void render(float delta) { 
     generalUpdate(); 
    } 

    private void generalUpdate() { 
     if(playerDied) { 
      if(firstTime && game.isLoaded()) { 
       System.out.println("hi"); 
       game.showAd(); 
       firstTime = false; 
       while(game.isShowing()) { 
        //Waiting for the ad to go away.. 
       } 
     } 
    } 

一个d

package com.JrodManU.LaserJumper.android; 

import com.JrodManU.LaserJumper.GameEventListener; 
import com.google.android.gms.ads.AdRequest; 
import com.google.android.gms.ads.InterstitialAd; 
import com.google.android.gms.ads.AdListener; 

import android.app.Activity; 
import android.os.Bundle; 

public class Ad extends Activity implements GameEventListener{ 
    private InterstitialAd mInterstitialAd; 
    public static boolean showing; 
    public static boolean loaded;  

    @Override 
    public void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     mInterstitialAd = new InterstitialAd(this); 
     mInterstitialAd.setAdUnitId("*****"); 
     AdRequest.Builder adRequestBuilder = new AdRequest.Builder(); 
     adRequestBuilder.addTestDevice(AdRequest.DEVICE_ID_EMULATOR); 
     mInterstitialAd.setAdListener(new AdListener() { 
      @Override 
      public void onAdClosed() { 
       showing = false; 
      } 

      @Override 
      public void onAdLoaded() { 
       loaded = true; 
      } 
     }); 
    } 

    public void showAd() { 
     if (mInterstitialAd.isLoaded()) { 
      mInterstitialAd.show(); 
      showing = true; 
     } 
    } 

    public void loadAd() { 
     AdRequest.Builder adRequestBuilder = new AdRequest.Builder(); 
     mInterstitialAd.loadAd(adRequestBuilder.build()); 
    } 

    public boolean isShowing() { 
     if(showing) { 
      return true; 
     } else { 
      return false; 
     } 
    } 

    public boolean isLoaded() { 
     if(loaded) { 
      return true; 
     } else { 
      return false; 
     } 
    } 
} 

LaserJumper(主类)

package com.JrodManU.LaserJumper; 

import com.JrodManU.LaserJumper.screens.*; 
import com.badlogic.gdx.Game; 

public class LaserJumper extends Game{ 
    InGameScreen inGameScreen; 
    public Preferences preferences; 
    public GameEventListener gameEventListener; 
    @Override 
    public void create() { 
     inGameScreen = new InGameScreen(this); 
     setScreen(inGameScreen); 
    } 

    public void changeToInGame() { 
     inGameScreen = new InGameScreen(this); 
     setScreen(inGameScreen); 
    } 

    public LaserJumper(GameEventListener listener) { 
     gameEventListener = listener; 
    } 

    public void showAd() { 
     gameEventListener.showAd(); 
    } 

    public void loadAd() { 
     gameEventListener.loadAd(); 
    } 

    public boolean isShowing() { 
     return gameEventListener.isShowing(); 
    } 

    public boolean isLoaded() { 
     return gameEventListener.isLoaded(); 
    } 
} 

最后,GameEventListener

package com.JrodManU.LaserJumper; 

public interface GameEventListener { 
    public void showAd(); 
    public void loadAd(); 
    public boolean isShowing(); 
    public boolean isLoaded(); 
} 
+0

哪一行是'com.JrodManU.LaserJumper.android.Ad.loadAd(Ad.java:45)'? – 2014-09-02 21:52:03

+0

在Ad Class中,它看起来像'loadAd()'方法中使用的'mInterstitialAd'对象没有被构造,请检查是否执行了onCreate方法。 – Chedy2149 2014-09-02 21:52:08

回答

0

插页式广告是一种独立的活动Ad内,然后你直接实例像这样的接口Ad ad = new Ad();

你应该永远不会因为活动必须服从Activity lifecycle,并且Android将通过活动生命周期方法进行调用,所以Android活动就像那样(他们启动Activities的方式是使用Intents)。

结果是广告活动的onCreate()方法未被调用,并且您的InterstitialAd从未被实例化过,之后导致NPE。


解决方案

  • 不要创建插页式广告的额外活动。根本没有必要。而是在AndroidLauncher中移动插页式广告初始化。
  • 使用处理程序来管理广告的显示/隐藏。这里描述:libgdx admob wiki。 (本教程指的是传统的admob,但与新的admob的差异很小,我仍然认为它是最好的教程)。它和正常横幅的工作方式基本相同。
+0

这将适用于横幅广告。然而,我在使用插页式广告,这表明我朝着正确的方向前进。如果有人访问需要帮助,请查看此链接:https://developers.google.com/mobile-ads-sdk/docs/dfp/android/interstitial – JrodManU 2014-09-03 00:59:42

+0

也适用于插页式广告,显然,您只需添加插页式广告与广告的布局;-) – donfuxx 2014-09-03 07:41:22