所以在我的LIBGDX游戏中,我想在点击按钮时显示Interstital广告,例如,重启游戏的按钮。
在我有这个按钮的课程中,我已经全局设置了这个:
AdControlller adController;
public MainMenu(AdControlller adController) {
this.adController = adController;
}
然后在按钮的touchUp方法中我设置了这个:
public void touchUp (InputEvent event, float x, float y, int pointer, int button){
howToPlayButton.addAction(Actions.scaleTo(1, 1,.1f));
mainMenu.addAction(Actions.sequence(Actions.fadeOut(0.24f),Actions.run(new Runnable() {
@Override
public void run() {
if (adController.isNetworkConnected()) {
adController.showInterstitalAd(new Runnable() {
@Override
public void run() {
game.setScreen(new HowToPlay(game));
loopMusic.dispose();
}
});
}
}
})));
}
});
问题是,当我点击按钮时,它会给我带来这个logcat错误:
FATAL EXCEPTION: GLThread 148306
Process: com.mygdx.game, PID: 30808
java.lang.NullPointerException: Attempt to invoke interface method 'boolean com.mygdx.game.AdControlller.isNetworkConnected()' on a null object reference
at com.mygdx.game.screens.MainMenu$5$1.run(MainMenu.java:167)
at com.badlogic.gdx.scenes.scene2d.actions.RunnableAction.run(RunnableAction.java:42)
at com.badlogic.gdx.scenes.scene2d.actions.RunnableAction.act(RunnableAction.java:32)
at com.badlogic.gdx.scenes.scene2d.actions.SequenceAction.act(SequenceAction.java:65)
at com.badlogic.gdx.scenes.scene2d.Actor.act(Actor.java:95)
at com.badlogic.gdx.scenes.scene2d.Group.act(Group.java:46)
at com.badlogic.gdx.scenes.scene2d.Stage.act(Stage.java:222)
at com.badlogic.gdx.scenes.scene2d.Stage.act(Stage.java:186)
at com.mygdx.game.screens.MainMenu.render(MainMenu.java:267)
at com.badlogic.gdx.Game.render(Game.java:46)
at com.mygdx.game.Main.render(Main.java:37)
at com.badlogic.gdx.backends.android.AndroidGraphics.onDrawFrame(AndroidGraphics.java:474)
at android.opengl.GLSurfaceView$GLThread.guardedRun(GLSurfaceView.java:1522)
at android.opengl.GLSurfaceView$GLThread.run(GLSurfaceView.java:1239)
我做错了什么?
IS网络连接方法
@Override
public boolean isNetworkConnected() {
ConnectivityManager connMgr = (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE);
NetworkInfo networkInfo = connMgr.getActiveNetworkInfo();
return (networkInfo != null && networkInfo.isConnected());
}