Android SurfaceView:ClassCastException,无法启动活动
试图让Android 2.2应用程序以SurfaceView作为基本视图启动,并在屏幕底部附近的顶部放置一个按钮。到目前为止,没有运气。它每次试图发射时都会崩溃。我已检查以确保该活动已在清单中注册 以下是我的java代码:Android SurfaceView:ClassCastException,无法启动活动,android,android-layout,surfaceview,Android,Android Layout,Surfaceview,试图让Android 2.2应用程序以SurfaceView作为基本视图启动,并在屏幕底部附近的顶部放置一个按钮。到目前为止,没有运气。它每次试图发射时都会崩溃。我已检查以确保该活动已在清单中注册 以下是我的java代码: public class Dragable extends Activity { /** Called when the activity is first created. */ @Override public void onCreate(Bun
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:无法启动活动组件信息{com.example/com.example.Dragable}:java.lang.ClassCastException:android.view.SurfaceView
似乎找不到任何与搜索相关的东西。如果以前有人问过我,我会向你道歉。现在我明白了问题所在。不是容器,我的意思是它不扩展
视图组
,因此不能在表面视图
中放置按钮
。您可以做的是将SurfaceView
和按钮
包装在RelativeLayout
中,而不是使用任何容器来放置组件。使用此选项并进行更改
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<SurfaceView
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_gravity="bottom"
/>
</SurfaceView>
</FrameLayout>
您的活动就只有这些吗?如果没有,请粘贴完整的活动代码好吗?@Cristian就是这样。里面没什么可看的了,谢谢。这让我很恼火。你不需要使用任何容器。例如,您可以使用TextView
作为XML布局的根元素(当然,除此之外,没有其他内容),这样就可以正常工作。顺便说一下,粘贴的代码也会失败。