SurfaceView:ClassCastException,无法启动活动
尝试让 Android 2.2 应用程序以 SurfaceView 作为基本视图启动,并在其顶部靠近屏幕底部的位置放置一个按钮。到目前为止,还没有运气。每次尝试启动时都会崩溃。我已检查以确保该活动已在清单中注册。
这是我的 java 代码:
public class Dragable extends Activity {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
}
这是我的 main.xml
:
<?xml version="1.0" encoding="utf-8"?>
<SurfaceView xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:id="@+id/surface_home"
>
<Button
android:id="@+id/add_new"
android:text="@string/add_new"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
/>
</SurfaceView>
和我的清单:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example"
android:versionCode="1"
android:versionName="1.0">
<application android:icon="@drawable/icon" android:label="@string/app_name">
<activity android:name=".Dragable"
android:label="@string/app_name">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
<uses-sdk android:minSdkVersion="8" />
</manifest>
这是我的错误:
11-29 11:58:52.620: ERROR/AndroidRuntime(512): java.lang .RuntimeException:无法启动活动 ComponentInfo{com.example/com.example.Dragable}:java.lang.ClassCastException:android.view.SurfaceView
似乎找不到任何与在 SO 上进行搜索相关的内容。如果之前有人问过这个问题,我深表歉意。
Trying to get an Android 2.2 application to start up with a SurfaceView as the base view with a button placed atop it located near the bottom of the screen. So far, no luck. It crashes every time it attempts to launch. I've checked to make sure that the activity is registered in the manifest.
Here's my java code:
public class Dragable extends Activity {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
}
and here's my main.xml
:
<?xml version="1.0" encoding="utf-8"?>
<SurfaceView xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:id="@+id/surface_home"
>
<Button
android:id="@+id/add_new"
android:text="@string/add_new"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
/>
</SurfaceView>
and my manifest:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example"
android:versionCode="1"
android:versionName="1.0">
<application android:icon="@drawable/icon" android:label="@string/app_name">
<activity android:name=".Dragable"
android:label="@string/app_name">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
<uses-sdk android:minSdkVersion="8" />
</manifest>
and here's my error:
11-29 11:58:52.620: ERROR/AndroidRuntime(512): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example/com.example.Dragable}: java.lang.ClassCastException: android.view.SurfaceView
Can't seem to find anything related doing searches on SO. My appologies if this has been asked before.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
现在我看到了问题所在。我的意思是
SurfaceView
不是容器它不扩展ViewGroup
,因此您不能将Button
放入SurfaceView
中。您可以做的是将SurfaceView
和Button
包装在RelativeLayout
中。Now I see the problem.
SurfaceView
is not a container, I mean it does not extendsViewGroup
, so you can't put aButton
inside aSurfaceView
. What you can do is wrapping theSurfaceView
and theButton
within aRelativeLayout
.您没有使用任何容器来放置组件。使用它并进行更改
You are not using any container to place components. Use this and make changes