这是我在网站上的第一个问题,我希望它不会太尴尬。我正在为一款安卓游戏制作一个库存标签。我在课程开始时声明了静态变量,并根据变量是否满足要求(变量需要至少等于一个才能显示在清单中)我想生成一个填充的字符串数组一个清单,将向您显示您的库存中的内容。当我在 Eclipse 中编写代码时,它告诉我它没有错误,但是当我尝试在 Android 模拟器中运行它时,我得到一个错误并且必须强制退出。这是供参考的代码。
(抱歉,如果代码格式关闭。Nightly 有时是一个奇怪的浏览器。)
public class Inventory extends Activity {
public static String selection = null;
public static String IronS = "Iron";
public static String WoodS = "Wood";
public static String StoneS = "Stone";
public static String WaterS = "Water";
public static String ShieldRS = "Shield Rim";
public static String ShieldBS = "Shield Base";
public static String ShieldS = "Shield";
public static String BladeS = "Blade";
public static String SwordHS = "Sword Handle";
public static String SwordS = "Sword";
public static String BreastS = "Breastplate";
public static String GauntS = "Gauntlets";
public static String GreaveS = "Greaves";
public static String RuneS = "Rune(s)";
public static String FishingPS = "Fishing Pole";
public static String PickS = "Pickaxe";
public static String PickHS = "Pickaxe Head";
public static int water = 1;
public static int iron = 1;
public static int stone = 1;
public static int wood = 1;
public static int shieldr, shieldb, shield, sword, swordh, blade, breast, gaunt, greave, rune, fishingp, pick, pickh;
public static String[] CONTENTS;
public static String[] MENU;
public static int getWater() {
return water;
}
public static void setWater(int water) {
Inventory.water = water;
}
public static int getIron() {
return iron;
}
public static void setIron(int iron) {
Inventory.iron = iron;
}
public static int getWood() {
return wood;
}
public static void setWood(int wood) {
Inventory.wood = wood;
}
public static int getStone() {
return water;
}
public static void setStone(int stone) {
Inventory.stone = stone;
}
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
if (getWater()>1)
CONTENTS = new String []
{WaterS};
if (getIron()>1)
CONTENTS = new String []
{WaterS,IronS};
if (getWood()>1)
CONTENTS = new String []
{WaterS,IronS,WoodS};
if (getStone()>1)
CONTENTS = new String []
{WaterS,IronS,WoodS,StoneS};
if (shieldr>1)
CONTENTS = new String []
{WaterS,IronS,WoodS,WaterS,ShieldRS};
if (shieldb>1)
CONTENTS = new String []
{WaterS,IronS,WoodS,WaterS,ShieldRS,ShieldBS};
if (shield>1)
CONTENTS = new String []
{WaterS,IronS,WoodS,WaterS,ShieldRS,ShieldBS,ShieldS};
if (sword>1)
CONTENTS = new String []
{WaterS,IronS,WoodS,WaterS,ShieldRS,ShieldBS,ShieldS,SwordS};
if (swordh>1)
CONTENTS = new String []
{WaterS,IronS,WoodS,WaterS,ShieldRS,ShieldBS,ShieldS,SwordS,SwordHS};
if (blade>1)
CONTENTS = new String []
{WaterS,IronS,WoodS,WaterS,ShieldRS,ShieldBS,ShieldS,SwordS,SwordHS,BladeS};
if (breast>1)
CONTENTS = new String []
{WaterS,IronS,WoodS,WaterS,ShieldRS,ShieldBS,ShieldS,SwordS,SwordHS,BladeS,
BreastS};
if (gaunt>1)
CONTENTS = new String []
{WaterS,IronS,WoodS,WaterS,ShieldRS,ShieldBS,ShieldS,SwordS,SwordHS,BladeS,
BreastS,GauntS};
if (greave>1)
CONTENTS = new String []
{WaterS,IronS,WoodS,WaterS,ShieldRS,ShieldBS,ShieldS,SwordS,SwordHS,BladeS,
BreastS,GauntS,GreaveS};
if (rune>1)
CONTENTS = new String []
{WaterS,IronS,WoodS,WaterS,ShieldRS,ShieldBS,ShieldS,SwordS,SwordHS,BladeS,
BreastS,GauntS,GreaveS,RuneS};
if (fishingp>1)
CONTENTS = new String []
{WaterS,IronS,WoodS,WaterS,ShieldRS,ShieldBS,ShieldS,SwordS,SwordHS,BladeS,
BreastS,GauntS,GreaveS,RuneS,FishingPS};
if (pick>1)
CONTENTS = new String []
{WaterS,IronS,WoodS,WaterS,ShieldRS,ShieldBS,ShieldS,SwordS,SwordHS,BladeS,
BreastS,GauntS,GreaveS,RuneS,FishingPS,PickS};
if (pickh>1)
CONTENTS = new String []
{WaterS,IronS,WoodS,WaterS,ShieldRS,ShieldBS,ShieldS,SwordS,SwordHS,BladeS,
BreastS,GauntS,GreaveS,RuneS,FishingPS,PickS,PickHS};
if (stone>1)
CONTENTS = new String []
{WaterS,IronS,WoodS,WaterS,ShieldRS,ShieldBS,ShieldS,SwordS,SwordHS,BladeS,
BreastS,GauntS,GreaveS,RuneS,FishingPS,PickS,PickHS,StoneS};
Arrays.sort(CONTENTS);
}
}
当我删除 if 语句并离开数组时,没有问题。它像往常一样编译,我可以将数组插入 ListView。我一直在努力解决这个问题,并尝试使用 switch/case 语句而不是 ifs,但它不能正常工作(不会将多个变量考虑在内)。if-else-if 不起作用,因为如果找到真值,语句将不会继续前面的条件。
我对此很陌生,所以我不知道如何先行。如果没有条件评估项目是否“存在”(在游戏中),我想不出另一种填充库存列表的方法。