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)

我使用 onSetContentView 的方式与 Jong 建议的方式完全相同。此外,我尝试调试,我意识到我的游戏中的第一个场景已经运行,但在进入第二个场景之前它崩溃了。我的第一个场景的代码如下

@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