2

andengineゲームにAdMobを実装しようとしています。ビューの設定には以下のコードを使用しました。他のonSetContentView()ゲームではエラーは発生しませんが、他のゲームでは次のエラーが発生します。

@Override
protected void onSetContentView() {
    //Creating the parent frame layout:
    final FrameLayout frameLayout = new FrameLayout(this);
    //frameLayout.
    //Creating its layout params, making it fill the screen.
    final FrameLayout.LayoutParams frameLayoutLayoutParams = new FrameLayout.LayoutParams(FrameLayout.LayoutParams.FILL_PARENT,
                    FrameLayout.LayoutParams.FILL_PARENT);

   System.out.println("enter on setContentview");

   final AdView adView = new AdView(this, AdSize.BANNER, "a14fb36149e903c");
        adView.refreshDrawableState();
            adView.setVisibility(AdView.VISIBLE);

            try{

                  // Create an ad request.
                   AdRequest adRequest = new AdRequest();
                  // Set<String> s = "AdRequest.TEST_EMULATOR ";


                   adRequest.addTestDevice("E20D4AA1FE0CA3F46CD22145AD392B87");
                   // Fill out ad request.

                   // Add the AdView to the view hierarchy. The view will have no size
                   // until the ad is loaded.

                  // Start loading the ad in the background.
               adView.loadAd(adRequest);
            }catch(Exception e){
                System.out.println("this si the error"+e.toString());
            }
    //Creating the banner layout params. With this params, the ad will be placed in the bottom of the screen, middle horizontally.
    final FrameLayout.LayoutParams bannerViewLayoutParams =
            new FrameLayout.LayoutParams(FrameLayout.LayoutParams.WRAP_CONTENT,
                    FrameLayout.LayoutParams.WRAP_CONTENT,
                    Gravity.BOTTOM | Gravity.CENTER_HORIZONTAL);





    //Creating AndEngine's view.
    this.mRenderSurfaceView = new RenderSurfaceView(this);
    mRenderSurfaceView.setRenderer(this.mEngine);

    System.out.println("enter on setContentview 2222");
    //createSurfaceViewLayoutParams is an AndEngine method for creating the params for its view.
    final android.widget.FrameLayout.LayoutParams surfaceViewLayoutParams =
            new FrameLayout.LayoutParams(super.createSurfaceViewLayoutParams());

    //Adding the views to the frame layout.
    frameLayout.addView(this.mRenderSurfaceView, surfaceViewLayoutParams);
   // frameLayout.addView(adView, bannerViewLayoutParams);

    System.out.println("enter on setContentview 2198613948632");
    //Setting content view
    this.setContentView(frameLayout, frameLayoutLayoutParams);

    System.out.println("enter on setContentview 33344");
}

onSetContentViewを使用したコードでは、次のような致命的なエラーが発生します。

05-16 11:56:42.421: E/AndroidRuntime(18552): java.lang.NullPointerException
05-16 11:56:42.421: E/AndroidRuntime(18552):    at org.anddev.andengine.ui.activity.BaseGameActivity.doResume(BaseGameActivity.java:179)
05-16 11:56:42.421: E/AndroidRuntime(18552):    at org.anddev.andengine.ui.activity.BaseGameActivity.onWindowFocusChanged(BaseGameActivity.java:85)
05-16 11:56:42.421: E/AndroidRuntime(18552):    at com.android.internal.policy.impl.PhoneWindow$DecorView.onWindowFocusChanged(PhoneWindow.java:2056
05-16 11:56:42.421: E/AndroidRuntime(18552):    at android.view.View.dispatchWindowFocusChanged(View.java:3788)
05-16 11:56:42.421: E/AndroidRuntime(18552):    at android.view.ViewGroup.dispatchWindowFocusChanged(ViewGroup.java:658)
05-16 11:56:42.421: E/AndroidRuntime(18552):    at android.view.ViewRoot.handleMessage(ViewRoot.java:1930)
05-16 11:56:42.421: E/AndroidRuntime(18552):    at android.os.Handler.dispatchMessage(Handler.java:99)
05-16 11:56:42.421: E/AndroidRuntime(18552):    at android.os.Looper.loop(Looper.java:143)
05-16 11:56:42.421: E/AndroidRuntime(18552):    at android.app.ActivityThread.main(ActivityThread.java:4701)
05-16 11:56:42.421: E/AndroidRuntime(18552):    at java.lang.reflect.Method.invokeNative(Native Method)
05-16 11:56:42.421: E/AndroidRuntime(18552):    at java.lang.reflect.Method.invoke(Method.java:521)05-16 11:56:42.421: E/AndroidRuntime(18552):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:859)
05-16 11:56:42.421: E/AndroidRuntime(18552):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:617)
05-16 11:56:42.421: E/AndroidRuntime(18552):    at dalvik.system.NativeStart.main(Native Method)

Jongが提案したのとまったく同じ方法でonSetContentViewを使用しています。さらに、デバッグを試みたところ、ゲームの最初のシーンが実行されていることに気付きましたが、2番目のシーンに進む前にクラッシュしました。最初のシーンの私のコードは次のとおりです

@Override
public Scene onLoadScene() {

     sceneGameSplash = new Scene();

    System.out.println("size of height and width is : "+this.getHeight + "   "+this.getWidth);



    final Sprite spriteGameSplash = new Sprite(0, 0,
            gameSplashScreenRegion);

    spriteGameSplash.setWidth(display.getWidth());

    spriteGameSplash.setHeight(display.getHeight());

    sceneGameSplash.attachChild(spriteGameSplash);


    sceneGameSplash.registerUpdateHandler(new TimerHandler(5f, true,
            new ITimerCallback() {

                @Override
                public void onTimePassed(final TimerHandler pTimerHandler) {
                    System.out.println("5");
                    sceneGameSplash.unregisterUpdateHandler(pTimerHandler);
                    sceneGameSplash.detachChildren();
                    sceneGameSplash.clearTouchAreas();
                    sceneManagerUnjumble.loadMain();

                }
            }));


    System.out.println("6");
    return sceneGameSplash;
}

nullPointerExceptionがある理由について何か考えがある人

4

0 に答える 0