我一直试图弄清楚如何将广告添加到我的项目中好几天。我希望你们能告诉我该怎么做。我试图在第一轮后广告(玩家第一次去世)后关闭广告;它会让他们再次玩。
没有广告出现,我收到的错误如下:
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(屏幕类)
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..
}
}
}
广告
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 :(得分:0)
您的插页式广告位于单独的活动Ad
中,然后您直接将其实例化为Ad ad = new Ad();
你永远不应该像那样实例化Android活动(他们使用Intents启动活动的方式),因为活动必须服从Activity lifecycle,Android将通过活动生命周期方法调用。
结果是Ad Activity的onCreate()方法没有被调用,你的InterstitialAd从未被实例化,后来导致了NPE。
解决方案提案: