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

     今天编写一个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

  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值