【问题标题】:AndEngine - Admob black screenAndEngine - Admob 黑屏
【发布时间】:2012-11-05 11:50:36
【问题描述】:

我有一个 AndEngine 游戏,其子类来自

public class BaseActivity extends SimpleBaseGameActivity {

}

游戏运行良好,现在当我尝试向其中添加 Admob 视图时,我从 SO 上的海报中了解到要覆盖 onSetContentView() 方法。

我喜欢这样:

@Override
protected void onSetContentView() {

    //Creating the parent frame layout:
    final FrameLayout frameLayout = new FrameLayout(this);

    //Creating its layout params, making it fill the screen.
    final FrameLayout.LayoutParams frameLayoutLayoutParams = new FrameLayout.LayoutParams(FrameLayout.LayoutParams.FILL_PARENT, FrameLayout.LayoutParams.FILL_PARENT);

    //Creating the banner view.
    AdView adView = new AdView(this, AdSize.BANNER, AdMobPubId);

    // Initiate a generic request to load it with an ad
    adView.loadAd(new AdRequest());

    // set the layout properties
    final FrameLayout.LayoutParams adViewLayoutParams = new FrameLayout.LayoutParams(FrameLayout.LayoutParams.WRAP_CONTENT, FrameLayout.LayoutParams.WRAP_CONTENT, Gravity.TOP | Gravity.CENTER_HORIZONTAL);


    // Creating AndEngine's view - Reference made
    this.mRenderSurfaceView = new RenderSurfaceView(this);
    mRenderSurfaceView.setRenderer(mEngine, null);


    //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, adViewLayoutParams);

    //Setting content view
    this.setContentView(frameLayout, frameLayoutLayoutParams);
}

广告已加载,但屏幕为黑色。我的游戏没了。 屏幕仍然记录触摸(正如我在调试器中看到的那样)

为什么引擎没有显示在下视图?!?!

编辑:终于搞定了

使用了这段代码,突然就可以了,不知道为什么之前没有

@Override
    protected void onSetContentView() {
        this.mRenderSurfaceView = new RenderSurfaceView(this);
        this.mRenderSurfaceView.setRenderer(this.mEngine, this);
        final android.widget.FrameLayout.LayoutParams surfaceViewLayoutParams = new FrameLayout.LayoutParams(super.createSurfaceViewLayoutParams());

        //Creating the banner view.
        AdView adView = new AdView(this, AdSize.BANNER, AdMobPubId);
        adView.loadAd(new AdRequest());
        final FrameLayout.LayoutParams adViewLayoutParams = new FrameLayout.LayoutParams(FrameLayout.LayoutParams.WRAP_CONTENT, FrameLayout.LayoutParams.WRAP_CONTENT, Gravity.RIGHT | Gravity.CENTER_HORIZONTAL);


        final FrameLayout frameLayout = new FrameLayout(this);
        final FrameLayout.LayoutParams frameLayoutLayoutParams = new FrameLayout.LayoutParams(FrameLayout.LayoutParams.FILL_PARENT, FrameLayout.LayoutParams.FILL_PARENT);

        frameLayout.addView(this.mRenderSurfaceView,surfaceViewLayoutParams);
        frameLayout.addView(adView,adViewLayoutParams);
        this.setContentView(frameLayout, frameLayoutLayoutParams);

    }

【问题讨论】:

  • 哦,别忘了,我添加了 admob sdk,进行了构建路径和命令导出,一切就绪,一切顺利......只是黑屏......
  • 为了确定,尝试将android:id="@+id/adView" 更改为android:id="@+id/game_adView" 之类的东西

标签: java android admob andengine


【解决方案1】:

它不起作用,因为您在充气后更改了布局。在您已调用 setContentView 并膨胀布局后,您将无法添加视图。

您所要做的就是在 AndEngine 调用 setContentView 之前创建 AdView 并将其添加到布局中。你可以看到一个这样做的例子here

我在这个例子中使用FrameLayout,它取自我的游戏,其中FrameLayout 效果最好。您的 onSetContentView 方法会更简单 - 您所要做的就是实例化 RenderSurfaceView 对象和 AdView 对象,然后将它们添加到新的 LinearLayout 并调用 setContentView

【讨论】:

  • 如果我这样做,我的屏幕会变黑(不过我确实收到了广告)
【解决方案2】:

我认为你做不到

LinearLayout layout = (LinearLayout)findViewById(R.id.game_rendersurfaceview);

因为R.id.game_rendersurfaceview 被声明为org.anddev.andengine.opengl.view.RenderSurfaceView

那些不是苹果/苹果。

就我个人而言,我只是在您的 XML 中的主 LinearLayout 中添加另一个字段来保存 adView。


我无法具体说明,因为我已经有几年没有使用广告了。您应该能够找到在 XML 中声明 adView 的示例。会是这样的

    <?xml version="1.0" encoding="utf-8"?>
    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:orientation="vertical"
        android:layout_width="fill_parent" 
        android:layout_height="fill_parent">

        <org.anddev.andengine.opengl.view.RenderSurfaceView android:id="@+id/game_rendersurfaceview"
            android:layout_width="fill_parent" 
            android:layout_height="fill_parent"
            android:layout_gravity="center" />

    <adView decalaration here
android:id="@+id/game_adview" />
    </LinearLayout>

一定要给它一个id,然后在你的代码中使用那个id

adView layout = (adView)findViewById(R.id.game_adview);

【讨论】:

  • 我还没有那么熟悉 java。我该怎么做?以及如何附加视图?
  • 我明白,但我必须将该视图附加到父视图,不是吗?我虽然我将广告视图附加到渲染表面视图。还是我想错了?
  • 我想要 2 个视图 - 1 个用于 RenderSurfaceView 和 1 个用于广告 - 也许是我想错了。正如我所说,我已经有几年没有使用过广告了——我想我忘记的比我所知道的还要多。抱歉,我没有提供太多帮助 - 希望更熟悉将 AdMob 和 AndEngine 结合在一起的人会出现(然后我们都可以学到一些东西)。
  • 不是父视图,因为您指的是 LinearLayout 而不是 RenderSurfaceView?
  • 我想是的,我已经更新了问题,因为我尝试了 xml 版本......仍然无法让它工作
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多