我在我的 xml 中为我的 android 应用程序使用 relativelayout,但决定切换到 framelayout,以便我可以将 imageView 设为另一个 imageView 的子级。问题是,虽然游戏之前一直在运行,但现在它崩溃了,因为我已经将我的 xml 编辑为框架布局。谁能说出为什么现在崩溃了?我确定我仍然在 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>
这是主要活动:
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;
}
}
这是堆栈跟踪:
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)