2014-09-02 5 views
1

일 동안 내 프로젝트에 광고를 추가하는 방법을 알아 내려고 노력했습니다. 너희들이 나에게 무엇을해야하는지 보여 줄 수 있기를 바란다. 첫 번째 라운드가 끝나면 광고를 게재하려고합니다 (처음 플레이어가 사망 함). 그것은 그들이 다시 게임을하도록 허락 할 것입니다.무엇이 잘못 되었나요? 삽입 광고를 추가하는 방법

어떤 광고가 표시되지 않고 I는 다음과 같은 오류를 얻고있다 :

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(); 
    } 
} 

그리고 마지막으로,

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)'입니까? –

+0

광고 클래스에서'loadAd()'메소드에 사용 된'mInterstitialAd' 객체가 생성되지 않은 것처럼 보입니다. onCreate 메소드가 실행되는지 확인하십시오. – Chedy2149

답변

0

귀하의 삽입 광고는 별도의 활동 Ad 안에 한 다음과 같이 직접 인스턴스화 GameEventListener 인터페이스 Ad ad = new Ad();

활동은 Activity lifecycle을 준수해야하고 Android는 활동 라이프 사이클 메소드를 통해 호출하기 때문에 안드로이드 활동 (예 : 활동을 시작하는 방법은 의도를 사용하는 것)과 비슷합니다.

결과적으로 Ad Activity의 onCreate() 메소드가 호출되지 않고 나중에 NPE로 이어지는 InterstitialAd가 인스턴스화 된 적이 없습니다.


솔루션 제안 :

  • 가 삽입 광고에 대한 추가 작업을 만들지 마십시오. 그것은 전혀 필요하지 않습니다. 대신 AndroidLauncher 내부에서 삽입 광고 초기화를 이동하십시오.
  • 처리기를 사용하여 광고 표시/숨기기를 관리합니다. 여기에 설명되어 있습니다 : libgdx admob wiki. (이 튜토리얼은 레거시 admob을 말하지만 새 admob과의 차이점은 거의 없으며 여전히 최고의 튜토리얼이라고 생각합니다.) 정상적인 배너와 동일한 방식으로 작동합니다.
+0

이것은 배너 광고에 적용됩니다. 그러나 Interstitial 광고를 사용하고 있었는데 이것이 올바른 방향으로 나를 가리켰습니다. https://developers.google.com/mobile-ads-sdk/docs/dfp/android/interstitial – JrodManU

+0

이 삽입 광고에도 사용되는 경우이 링크를 확인하세요. 삽입 광고를 추가해야합니다. adview가있는 레이아웃 ;-) – donfuxx

관련 문제