これはサイトでの私の最初の質問であり、あまり恥ずかしくないことを願っています。Androidゲームの[インベントリ]タブを作成しています。クラスの最初に静的変数を宣言し、変数が要件を満たしているかどうかに基づいて(インベントリに表示するには、変数が少なくとも1つである必要があります)、次のような文字列配列を生成します。インベントリに何があるかを示すリスト。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に挿入できます。私はしばらくの間これにぶつかり、ifsの代わりにswitch / caseステートメントを作成しようとしましたが、正しく機能しませんでした(条件のために複数の変数を考慮しませんでした)。If-else-ifが機能しなかったのは、真の値が見つかった場合、ステートメントが前の条件に進まないためです。
私はこれがとても新しいので、どのように先に進むべきかわかりません。アイテムが(ゲーム内に)「存在する」かどうかを条件付きで評価せずに、インベントリリストにデータを入力する別の方法を考えることはできません。