我会尽量做到具体。我创建了正在填充 xml 文件的自定义对象类。这些课程是针对机器人的,机器人有武器和盔甲等。我将机器人的每次迭代都存储在一个数组列表中。然后,我将使用有关机器人的信息填充列表视图(自定义列表适配器)。当您单击机器人的不同部分时,它会显示哪些武器装甲等附加到这些组件上。这一切都很好,并且不知何故随机事情停止了工作。它几乎就像应用程序在活动之间传递时丢失数据或 GC 正在删除一些数据一样。我一直在努力弄清楚为什么它突然显示错误的数据或数据丢失。我真的希望我可以在这里发布代码,但实际上我的整个应用程序现在都遇到了这个问题。我想我的问题是。我是否将自定义对象正确存储在数组列表中。或者是他们不同的方法。我什至不知道从哪里开始调试它,因为它曾经可以工作。
private Mech loadMDF(Mech mech) {
MechComponents mechComponents = null;
String mechfile = "";
String name = "";
float HP = 0;
int maxSlots = 0;
try {
XmlPullParserFactory factory;
factory = XmlPullParserFactory.newInstance();
factory.setNamespaceAware(true);
XmlPullParser xpp = factory.newPullParser();
// FileInputStream fis = new FileInputStream(file);
// set the input for the parser using an InputStreamReader
// xpp.setInput(new InputStreamReader(fis));
xpp.setInput(getResources().getAssets().open(mech.mdf), null);
int eventType = xpp.getEventType();
while (eventType != XmlPullParser.END_DOCUMENT) {
if (eventType == XmlPullParser.START_DOCUMENT) {
} else if (eventType == XmlPullParser.START_TAG) {
if (xpp.getName().equals("MechDefinition")) {
for (int i = 0; i < xpp.getAttributeCount(); i++) {
if (xpp.getAttributeName(i).equals("HardpointPath")) {
boolean found = false;
String[] theFilename = xpp.getAttributeValue(i)
.split("/");
for (int n = 0; n < MechHardPointFiles.size(); n++) {
if (MechHardPointFiles.get(n).equals(
theFilename[theFilename.length - 1])) {
found = true;
}
}
if (!found) {
MechHardPointFiles
.add(theFilename[theFilename.length - 1]);
loadMechFile(theFilename[theFilename.length - 1]);
}
mechfile = theFilename[theFilename.length - 1];
}
}
}
if (xpp.getName().equals("Mech")) {
for (int i = 0; i < xpp.getAttributeCount(); i++) {
if (xpp.getAttributeName(i).equals("MaxTons")) {
mech.MaxTons = Float.parseFloat(xpp.getAttributeValue(i));
}
if (xpp.getAttributeName(i).equals("BaseTons")) {
mech.BaseTons = Float
.parseFloat(xpp.getAttributeValue(i));
}
if (xpp.getAttributeName(i).equals("CanEquipECM")) {
mech.setCanEquipECM(xpp.getAttributeValue(i));
}
if (xpp.getAttributeName(i).equals("MaxJumpJets")) {
mech.MaxJumpJets = xpp.getAttributeValue(i);
}
if (xpp.getAttributeName(i).equals("LeftArmAim")) {
// mech.MaxTons =
// Float.parseFloat(xpp.getAttributeValue(i));
}
if (xpp.getAttributeName(i).equals("RightArmAim")) {
// mech.MaxTons =
// Float.parseFloat(xpp.getAttributeValue(i));
}
if (xpp.getAttributeName(i).equals("MinEngineRating")) {
mech.MinEngineRating = Integer.parseInt(xpp
.getAttributeValue(i));
}
if (xpp.getAttributeName(i).equals("MaxEngineRating")) {
mech.MaxEngineRating = Integer.parseInt(xpp
.getAttributeValue(i));
}
if (xpp.getAttributeName(i).equals("MaxJumpJets")) {
mech.setCanEquipECM(xpp.getAttributeValue(i));
}
}
}
if (xpp.getName().equals("Component")) {
mechComponents = new MechComponents();
for (int i = 0; i < xpp.getAttributeCount(); i++) {
if (xpp.getAttributeName(i).equals("Name")) {
name = helperObjects.checkForSpace(xpp
.getAttributeValue(i));
}
if (xpp.getAttributeName(i).equals("HP")) {
HP = Float.parseFloat(xpp.getAttributeValue(i));
}
if (xpp.getAttributeName(i).equals("Slots")) {
maxSlots = Integer.parseInt(xpp.getAttributeValue(i));
}
}
}
if (xpp.getName().equals("Internal")) {
for (int i = 0; i < xpp.getAttributeCount(); i++) {
if (xpp.getAttributeName(i).equals("Name")) {
mechComponents.addToSlot(xpp.getAttributeValue(i));
}
}
}
if (xpp.getName().equals("Hardpoint")) {
int hpType = 0;
int hpSlots = 0;
for (int i = 0; i < xpp.getAttributeCount(); i++) {
if (xpp.getAttributeName(i).equals("Type")) {
hpType = Integer.parseInt(xpp.getAttributeValue(i));
}
if (xpp.getAttributeName(i).equals("ID")) {
for (int f = 0; f < MechHardPoints.size(); f++) {
if (MechHardPoints.get(f).hpFile.equals(mechfile)) {
if (MechHardPoints.get(f).id == Integer
.parseInt(xpp.getAttributeValue(i))) {
hpSlots = MechHardPoints.get(f).Slots;
break;
}
}
}
}
}
mechComponents.addHardPointRestriction(new HardPointRestriction(
hpType, hpSlots));
}
} else if (eventType == XmlPullParser.END_TAG) {
if (xpp.getName().equals("Component")) {
mechComponents.name = name;
mechComponents.HP = HP;
mechComponents.maxSlots = maxSlots;
mech.components.add(mechComponents);
Log.i("mwo_app",
"Hard Point Check: "
+ Integer.toString(mechComponents
.getHardPointRestrictions().size()));
}
} else if (eventType == XmlPullParser.TEXT) {
}
eventType = xpp.next();
}
} catch (XmlPullParserException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
return mech;
}
这是我用来切换活动的另一个片段
inte = new Intent(MainActivity2.this, MechView.class);
Bundle b = new Bundle();
b.putSerializable("com.typhonpacific.mwoapp.mech",
mechListAdapter.getItem(arg2));
inte.putExtras(b);
startActivity(inte);