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, AdSize.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:
You create your SurfaceView entirely in Java (as in the example you posted)
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:game_layout.xml: