Android画布游戏问题
我使用android SDK示例月球着陆器的模板编写了一个游戏,但我发现模板中有一个错误。经过多次尝试,我无法按照我的意愿修复它。所以我将游戏模板更改为 Chris pruett 的 SpriteMethodTest http:// /code.google.com/p/apps-for-android/source/browse/trunk/SpriteMethodTest 在此模板中我遇到了一些问题。
问题: 在 CanvasTestActivity.java onCreate()方法: 代码使用以下方法初始化 SurfaceView:
mCanvasSurfaceView = new CanvasSurfaceView(this); ... setContentView(mCanvasSurfaceView);
我想使用布局文件来获取 SurfaceView,因此我创建了一个如下所示的布局文件:
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"/>
我更改了 oncreate() 方法中的代码:
mCanvasSurfaceView = (CanvasSurfaceView) findViewById(R.id.gameview); setContentView(R.layout.main);
findViewById 总是返回 null!我不知道为什么!真的需要帮助!
顺便说一句,如何在 Android 应用程序中添加 admob 广告,使用: mCanvasSurfaceView = new CanvasSurfaceView(this); setContentView(mCanvasSurfaceView);
我在网上找不到任何示例:(
最好的问候, 杨子
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
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您确定在
findViewById
之前调用setContentView(R.layout.main)
吗?要添加 AdMob 广告,最简单的方法是创建relativelayout,使用
addContentView
将其添加到窗口,然后使用 Gravity.BOTTOM 布局参数将 admob 中的 AdView 添加到相对布局的底部。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.