开发者

SurfaceView: ClassCastException, unable to start activity

Trying to get an Android 2.2 application to start up with a SurfaceView as the base view with a button placed atop it located near the bottom of the screen. So far, no luck. It crashes every time it attempts to launch. I've checked to make sure that the activity is registered in the manifest.

Here's my java code:

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);
    }

and here's my 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_conten开发者_JAVA百科t"
        android:layout_alignParentBottom="true"
    />
</SurfaceView>

and my manifest:

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

and here's my error:

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

Can't seem to find anything related doing searches on SO. My appologies if this has been asked before.


Now I see the problem. SurfaceView is not a container, I mean it does not extends ViewGroup, so you can't put a Button inside a SurfaceView. What you can do is wrapping the SurfaceView and the Button within a RelativeLayout.


You are not using any container to place components. Use this and make changes

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

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜