[英]Trying to create xml framelayout, android app keeps crashing
I was using a relativelayout in my xml for my android app, but have decided to switch over to framelayout so I can make an imageView a child of another imageView. 我在我的android应用程序的xml中使用了relativelayout,但已决定切换到framelayout,以便可以使imageView成为另一个imageView的子级。 The problem is, although the game was working so far before, now it crashes since I've edited my xml to be a framelayout.
问题是,尽管该游戏到目前为止还可以运行,但是由于我将xml编辑为帧布局,现在它崩溃了。 Can anyone tell why this is crashing now?
谁能说出现在为什么崩溃了? I'm sure I'm using something still in the imageView definitions that I shouldn't be, but can't figure out what it is.
我确定我在imageView定义中仍然使用了我不应该使用的东西,但无法弄清楚它是什么。
Here is my xml file: 这是我的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>
Here is the MainActivity: 这是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;
}
} }
Here is the Boat class: 这是船课:
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;
}
}
Here is the person class: 这是人类:
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;
}
}
Here is the 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)
The issue is 问题是
**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)
You are trying to cast Person object to Boat. 您正在尝试将Person对象投射到Boat。 Check your code again whether you are mapping the xml ids correctly.
再次检查您的代码是否正确映射了XML ID。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.