2010-11-29 93 views
2

試圖讓一個Android 2.2應用程序以SurfaceView作爲基本視圖啓動,並在上面放置一個按鈕,位於屏幕底部附近。到目前爲止,沒有運氣。它每次嘗試啓動時都會崩潰。我已經檢查過以確保活動已在清單中註冊。SurfaceView:ClassCastException,無法啓動活動

這裏是我的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相關的搜索操作。我的appologies,如果這已經問過。

+0

這就是您在活動中所擁有的一切嗎?如果不是,請粘貼完整的活動代碼嗎? – Cristian 2010-11-29 17:19:41

+0

@Cristian就是這樣。沒有什麼可以看到的。 – wheaties 2010-11-29 17:23:27

回答

4

現在我看到了問題。 SurfaceView不是容器,我的意思是它沒有擴展ViewGroup,所以你不能把Button放在SurfaceView裏面。你可以做的是將SurfaceViewButton包裝在RelativeLayout之內。

+0

非常感謝。這讓我感動了一堵牆。 – wheaties 2010-11-29 17:34:22

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