SurfaceView:ClassCastException,无法启动活动

时间:2010-11-29 17:09:11

标签: android android-layout surfaceview

尝试使用SurfaceView作为基本视图启动Android 2.2应用程序,并在屏幕底部附近放置一个按钮。到目前为止,没有运气。它每次尝试启动时都会崩溃。我已经检查过以确保活动已在清单中注册。

这是我的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: Unable to start activity ComponentInfo{com.example/com.example.Dragable}: java.lang.ClassCastException: android.view.SurfaceView

似乎无法找到任何与SO相关的搜索相关内容。如果以前曾经问过这个问题我的意见。

2 个答案:

答案 0 :(得分:4)

现在我看到了问题。 SurfaceView不是容器,我的意思是它不会扩展ViewGroup,因此您无法将Button放入SurfaceView。您可以做的是将SurfaceViewButton包裹在RelativeLayout内。

答案 1 :(得分:-1)

您没有使用任何容器来放置组件。使用此并进行更改

 <?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>