您的位置:首页 > 其它

xml用到surfaceview控件 遇到unable to start activity componentInfo类问题

2011-11-18 09:43 288 查看
今天编写一个android加入surfaceview的问题,xml中的代码如下
<?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"
>
<Game.escape.GameService
android:id="@+id/myview"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
</Game.escape.GameService>
</LinearLayout>

在程序中代码代码只有一句就是加载xml:setContentView(R.layout.main);一点点简单的程序,就报错了:unable to start activity componentInfo。很明显就是说acvitity启动这个surfaceview启动不了。但是为什么呢,以前我用过的啊,上网上找了一会,有一篇外国的帖子有解决办法原文如下http://www.coderanch.com/t/521231/Android/Mobile/reading-XML-layout-contains-SurfaceView
勉强看懂了,原来是GameService这个继承SurfaceView的类只有(Context context)这个构造函数,而没有(Context context, AttributeSet attrs)这个构造函数,原因如下:


如果你的activity显示内容就只是一个surfaceview的话,那么你只重写public mysurfaceview(Context context)就可以,然后在activity的oncreate方法中使用类似于下面的代码加载:

setContentView(new MySurfaceView(this));

如果你的SurfaceView是放在一个xml布局文件中比如main.xml中,那么你的SurfaceView中只需要重写public mysurfaceview(Context context, AttributeSet attrs),加载时就要这样:

setContentView(R.layout.main);

来源:http://www.eoeandroid.com/thread-73010-1-1.html
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐