2010-11-29 29 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に関連する検索は何も見つかりませんでした。これが以前に尋ねられている場合は、私の考え方。

+0

これはあなたのアクティビティにはありますか?いいえの場合は、完全なアクティビティコードを貼り付けることができますか? – Cristian

+0

それはクリスチャンです。そこには何も見えません。 – wheaties

答えて

4

今問題が発生します。 SurfaceViewはコンテナではありません。つまり、ViewGroupを拡張しないので、ButtonSurfaceViewに入れることはできません。あなたができることは、SurfaceViewButtonRelativeLayoutにラップすることです。

+0

ありがとうございました。これは私を壁に追いやっていた。 – wheaties

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

コンテナを使用する必要はありません。例えば、XMLレイアウトのルート要素として 'TextView'を使うことができます(もちろん、それ以外は何もありません)。うまくいくでしょう。ところで、貼り付けたコードも失敗します。 – Cristian

関連する問題