开发者

Android - Admob SurfaceView

I am new to android development, but I am trying to add admob to my app which is using a surfaceview (panelView - GameView).

I tried following http://rx-games.com/admob-adverts-on-surfaceview-no-xml-tutorial/ but I must be doing something wrong, as rl.addView(panelView); adds a nullPointerException. Any help is greatly appreciated.

public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    requestWindowFeature(Window.FEATURE_NO_TITLE);
    //setContentView(R.layout.main);
    AdView adView = new AdView(this, Ad开发者_C百科Size.BANNER, "a14ded47ad3779e");
    panelView = (GameView) findViewById(R.id.gameScreen);

    RelativeLayout rl = new RelativeLayout(this);
    rl.addView(panelView);
    rl.addView(adView);

    setContentView(rl);

    threadView = panelView.getThread();
}


You are inflating the GameView class from an xml layout and add it to a root View that is not the one defined in that same xml. You have two options:

  1. You create your SurfaceView entirely in Java (as in the example you posted)

  2. You include the AdView in the xml layout (I would prefer this option over the other one)

If you name the xml file game_layout.xml, you use it as:

setContentView(R.layout.game_layout.xml);

game_layout.xml:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
  xmlns:ads="http://schemas.android.com/apk/lib/com.google.ads"
  android:orientation="vertical"
  android:layout_width="fill_parent"
  android:layout_height="fill_parent"
  >
  <com.google.ads.AdView 
    android:id="@+id/adView"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    ads:adUnitId="@string/admob_id"
    ads:adSize="BANNER"
    ads:loadAdOnCreate="true"
    android:layout_alignParentBotom="true"
    />
  <your.package.GameView 
    android:id="@+id/gameScreen"
    android:layout_alignParentTop="true"
    android:layout_above="@id/adView"
  />
</RelativeLayout>
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜