代码之家  ›  专栏  ›  技术社区  ›  wheaties

SurfaceView:ClassCastException,无法启动活动

  •  2
  • wheaties  · 技术社区  · 15 年前

    尝试让Android2.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: Unable to start activity ComponentInfo{com.example/com.example.Dragable}: java.lang.ClassCastException: android.view.SurfaceView

    在搜索中似乎找不到任何相关内容。如果以前有人问过这个问题,我的申请表。

    2 回复  |  直到 15 年前
        1
  •  4
  •   Cristian    15 年前

    现在我看到问题了。 SurfaceView 不是容器,我的意思是它不会扩展 ViewGroup 所以你不能 Button 里面 曲面视图 . 你能做的就是包装 曲面视图 以及 纽扣 在一个 RelativeLayout .

        2
  •  -1
  •   David    15 年前

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

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