试图创建xml framelayout,Android应用程序不断崩溃

时间:2013-10-14 23:37:41

标签: android xml android-framelayout

我在我的Android应用程序的xml中使用了relativelayout,但是我决定切换到framelayout,这样我就可以让imageView成为另一个imageView的孩子了。问题是,虽然游戏到目前为止工作,但现在它崩溃了,因为我已经编辑了我的xml作为framelayout。任何人都可以告诉为什么现在崩溃了吗?我确定我在imageView定义中仍然使用了一些我不应该使用的东西,但无法弄清楚它是什么。

这是我的xml文件:

<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@layout/activity_main"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="@drawable/background"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context=".MainActivity" >

<com.cannibal_photographer.Person
    android:id="@+id/personView1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_marginTop="335dp"
    android:src="@drawable/greensquare" />

<com.cannibal_photographer.Boat
    android:id="@+id/boatimageView"
    android:layout_width="78dp"
    android:layout_height="130dp"
    android:layout_marginLeft="105dp"
    android:layout_marginTop="180dp"
    android:src="@drawable/boat" />

</FrameLayout>

以下是MainActivity:

package com.cannibal_photographer;

import android.app.Activity;
import android.os.Bundle;
import android.view.Menu;

public class MainActivity extends Activity {

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);     
    Boat boatobject = (Boat)findViewById(R.id.boatimageView);   
    Person personobject = (Person)findViewById(R.id.personView1);
}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.main, menu);
    return true;
}

}

这是船类:

package com.cannibal_photographer;

import android.content.Context;
import android.util.AttributeSet;
import android.view.View;
import android.widget.ImageView;

public class Boat extends ImageView {

boolean state = true;

public Boat(Context context) {
    super(context);
    init();
}

 public Boat(Context context, AttributeSet attrs)
    {
        super(context, attrs);
        init();
    }

 public Boat(Context context, AttributeSet attrs, int defStyle)
    {
        super(context, attrs, defStyle);
        init();
    }

private void init()
{
    this.setOnClickListener(new OnClickListener() {
        @Override
        public void onClick (View v) {
            if (state) {
                moveBoat(-290);
            } else {
                moveBoat(290);
            }
        }
    });
}

//TranslateAnimation animation;
//TranslateAnimation animation2;

public void moveBoat(int amount){
    /*
    animation = new TranslateAnimation(0, 0, 0, amount);
    animation.setDuration(250);
    animation.setFillAfter(true);
    this.startAnimation(animation);
    */
    this.offsetTopAndBottom(amount);
    state = !state;
}
}

这是人类:

package com.cannibal_photographer;

import android.content.Context;
import android.util.AttributeSet;
import android.view.View;
import android.widget.ImageView;

public class Person extends ImageView {

boolean state = true;

public Person(Context context) {
    super(context);
    init();
}

 public Person(Context context, AttributeSet attrs)
    {
        super(context, attrs);
        init();
    }

 public Person(Context context, AttributeSet attrs, int defStyle)
    {
        super(context, attrs, defStyle);
        init();
    }

private void init()
{
    this.setOnClickListener(new OnClickListener() {
        @Override
        public void onClick (View v) {
            if (state) {
                movePerson(325,-230);
            } else {
                movePerson(-325,230);
            }
        }
    });


}

public void movePerson(int x, int y)
{
    this.offsetLeftAndRight(x);
    this.offsetTopAndBottom(y);

    state = !state;
}

}

这是stacktrace:

10-14 23:10:10.985: E/AndroidRuntime(932): FATAL EXCEPTION: main
10-14 23:10:10.985: E/AndroidRuntime(932): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.cannibal_photographer/com.cannibal_photographer.MainActivity}: java.lang.ClassCastException: com.cannibal_photographer.Person cannot be cast to com.cannibal_photographer.Boat
10-14 23:10:10.985: E/AndroidRuntime(932):  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2180)
10-14 23:10:10.985: E/AndroidRuntime(932):  at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2230)
10-14 23:10:10.985: E/AndroidRuntime(932):  at android.app.ActivityThread.access$600(ActivityThread.java:141)
10-14 23:10:10.985: E/AndroidRuntime(932):  at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1234)
10-14 23:10:10.985: E/AndroidRuntime(932):  at android.os.Handler.dispatchMessage(Handler.java:99)
10-14 23:10:10.985: E/AndroidRuntime(932):  at android.os.Looper.loop(Looper.java:137)
10-14 23:10:10.985: E/AndroidRuntime(932):  at android.app.ActivityThread.main(ActivityThread.java:5041)
10-14 23:10:10.985: E/AndroidRuntime(932):  at java.lang.reflect.Method.invokeNative(Native Method)

1 个答案:

答案 0 :(得分:0)

问题是

**java.lang.ClassCastException:** com.cannibal_photographer.Person cannot be cast to com.cannibal_photographer.Boat 10-14 23:10:10.985: E/AndroidRuntime(932): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2180)

您正在尝试将Person对象转换为Boat。无论是否正确映射xml ID,请再次检查代码。