I wrote a game used the android SDK sample lunar lander’s template, but I found a bug in the template. Through a number of attempts I can’t fix it as I want. So I change the game template to Chris pruett’s SpriteMethodTest http://code.google.com/p/apps-for-android/source/browse/trunk/SpriteMethodTest In this template I got some problems.
Problem:
In CanvasTestActivity.java onCreate() method:
The code initialize the surfaceview using:
mCanvasSurfaceView = new CanvasSurfaceView(this);
…
setContentView(mCanvasSurfaceView);
I want to use layout file to take my surfaceview, so I create a layout file like this:
main.xml
<?xml version="1.0" encoding="utf-8"?>
<me.codeand.xxx.CanvasSurfaceView
android:id="@+id/gameview"
android:layout_width="fill_parent"
android:layout_height="fill_parent"/>
I changed the code in oncreate() method:
mCanvasSurfaceView = (CanvasSurfaceView) findViewById(R.id.gameview);
setContentView(R.layout.main);
findViewById always return null! I don’t know why ! really need help!
BTW,How to add the admob ads in android app that using :
mCanvasSurfaceView = new CanvasSurfaceView(this);
setContentView(mCanvasSurfaceView);
I can’t find any sample in the web 🙁
Best Regards,
czYang
are you sure you’re calling
setContentView(R.layout.main)beforefindViewById?To add AdMob ads, the easiest way is to create a RelativeLayout, add that to the window with
addContentView, and add the AdView from admob to the bottom of the relative layout using the Gravity.BOTTOM layout parameter.