我的应用
我想制作一个简单的游戏开始。为此,我生成了房屋,然后希望将其显示给玩家。
我的问题
当我尝试访问应存储房屋类别的ArrayList时,出现异常(请参见下文)。在调试器中,我可以看到在尝试访问类之前已正确生成了类。
我的代码
Houses.java(这是我在App启动时生成我的房屋,然后尝试访问它们的地方)
public class Houses{
ArrayList<Integer> classes = new ArrayList<>();
int[][] NewHouseProps = new int[20][10];
int[][] HouseProps;
public Houses(){
}
public ArrayList<Integer> getClasses()
{
for(int id=0;id<=8;id++){
Log.d("getClasses()","ID: "+id+" CLASSES: "+classes.get(id)); //Exception
}
return classes;
}
public void GenerateHouses(){
Log.d("Generate Houses","!!");
for(int id=0; id<=8; id++){
Random r = new Random();
int h_class, garage = 0, rooms = 0, furn = 0, balkon = 0, pool = 0;
h_class = r.nextInt(4); //Klasse
if(h_class == 0){
rooms = r.nextInt(4)+1;
balkon = r.nextInt(1);
}else if(h_class == 1){
rooms = r.nextInt(3)+3;
garage = r.nextInt(1)+1;
pool = r.nextInt(1);
balkon = r.nextInt(2);
}else if(h_class == 2){
rooms = r.nextInt(3)+2;
garage = r.nextInt(2)+2;
pool = r.nextInt(1);
}else if(h_class == 3){
rooms = r.nextInt(8)+6;
garage = r.nextInt(6)+4;
pool = r.nextInt(1);
balkon = r.nextInt(3);
}
furn = r.nextInt(1);
int price = GenerateHousePrice(id, h_class, garage, rooms, furn, balkon, pool);
NewHouseProps[id][0] = h_class;
NewHouseProps[id][1] = price;
NewHouseProps[id][2] = rooms;
NewHouseProps[id][3] = furn;
NewHouseProps[id][4] = balkon;
NewHouseProps[id][5] = pool;
NewHouseProps[id][6] = garage;
classes.add(h_class);
Log.d("GenerateHouses","ID: "+id+" CLASS: "+h_class);
Log.d("GenerateHouses","CLASSES: "+classes.get(id));
Log.d("GenerateHouses","PROPS: "+NewHouseProps[id][0]);
}
}
public int GenerateHousePrice(int id, int h_class, int garage, int rooms, int furn, int balkon, int pool){
int price = 0, pricep = 0;
Random r = new Random();
if(h_class == 0){
price = r.nextInt(150000)+50000; // 50 - 200 tausend
price += rooms * 7500;
}
else if(h_class == 1){
price = r.nextInt(165000)+85000; //85 - 250 tausend
price += rooms * 12500;
}
else if(h_class == 2){
price = r.nextInt(300000)+100000; //100 - 400 tausnd
price += rooms * 20000;
}
else if(h_class == 3){
price = r.nextInt(800000)+500000; // 500 - 1.3 Mill.
price += rooms * 15000;
}
if(garage > 0){
pricep += price*0.20;
pricep += garage * 500;
}
if(furn == 1){
pricep += price*0.25;
}
if(balkon > 0){
pricep += price*0.1;
pricep += balkon*500;
}
if(pool == 1){
pricep += 20000;
}
price += pricep;
return price;
}
}
BuyHousesActivity(这是我要显示它的位置,当我尝试获取类时会崩溃)
public class BuyHousesActivity extends FragmentActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_buy_houses);
Houses h = new Houses();
ArrayList<Integer> classes = h.getClasses();
for(int id=0;id<=8;id++){
Log.d("OnCreate Classes: ","Classes: "+classes.get(id)); //Exception
}
ViewPager viewPager = findViewById(R.id.vpPager);
viewPager.setAdapter(new MyViewPagerAdapter(getSupportFragmentManager(), classes));
}
protected class MyViewPagerAdapter extends FragmentStatePagerAdapter {
int clicker = 0;
private List<Integer> pclasses;
private ArrayList<Integer> threeclasses;
MyViewPagerAdapter(FragmentManager fm, ArrayList<Integer> classes) {
super(fm);
pclasses = classes;
if(clicker == 0){
for(int id=0;id<=2;id++) {
threeclasses.add(classes.get(id));
}
}else if(clicker == 1){
for(int id=3;id<=5;id++) {
threeclasses.add(classes.get(id));
}
}else if(clicker == 2){
for(int id=6;id<=8;id++){
threeclasses.add(classes.get(id));
}
clicker = 0;
return;
}
clicker++;
return;
}
@Override
public int getCount() {
return pclasses.size()/3;
}
@Override
public Fragment getItem(int position) {
return FragmentBuyHouse.newInstance(threeclasses);
}
}
例外情况(上面标记的行):
E/AndroidRuntime: FATAL EXCEPTION: main
Process: at.simonic.realtoroffice, PID: 4913
java.lang.RuntimeException: Unable to start activity ComponentInfo{at.simonic.realtoroffice/at.simonic.realtoroffice.BuyHousesActivity}: java.lang.IndexOutOfBoundsException: Index: 0, Size: 0
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2913)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3048)
at android.app.servertransaction.LaunchActivityItem.execute(LaunchActivityItem.java:78)
at android.app.servertransaction.TransactionExecutor.executeCallbacks(TransactionExecutor.java:108)
at android.app.servertransaction.TransactionExecutor.execute(TransactionExecutor.java:68)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1808)
at android.os.Handler.dispatchMessage(Handler.java:106)
at android.os.Looper.loop(Looper.java:193)
at android.app.ActivityThread.main(ActivityThread.java:6669)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:493)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:858)
Caused by: java.lang.IndexOutOfBoundsException: Index: 0, Size: 0
at java.util.ArrayList.get(ArrayList.java:437)
at at.simonic.realtoroffice.BuyHousesActivity.onCreate(BuyHousesActivity.java:31)
at android.app.Activity.performCreate(Activity.java:7136)
at android.app.Activity.performCreate(Activity.java:7127)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1271)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2893)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3048)
at android.app.servertransaction.LaunchActivityItem.execute(LaunchActivityItem.java:78)
at android.app.servertransaction.TransactionExecutor.executeCallbacks(TransactionExecutor.java:108)
at android.app.servertransaction.TransactionExecutor.execute(TransactionExecutor.java:68)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1808)
at android.os.Handler.dispatchMessage(Handler.java:106)
at android.os.Looper.loop(Looper.java:193)
at android.app.ActivityThread.main(ActivityThread.java:6669)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:493)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:858)
我希望您能帮助我了解数据丢失的过程,并帮助我了解其背后的问题。
答案 0 :(得分:0)
您没有在GenerateHouses
类构造函数上调用Houses
。检查您的BuyHousesActivity
onCreate
。您正在做的是:
Houses h = new Houses(); // This will create an new object
在此对象的构造函数中,您什么也不做:
public Houses(){}
因此,该对象classes
参数在您访问时将为空:
ArrayList<Integer> classes = h.getClasses();
这就是您得到例外的原因。
因此解决方案是其中之一:
GenerateHouses
对象构造函数中调用Houses
GenerateHouses
变量之前调用classes