我是使用Java和Android Studio的初学者,这个代码是一个项目的一部分,用于点击鼹鼠游戏。点击一定量的痣会增加水平,之后可以作为高分提交。游戏将每个玩家的高分作为生成按钮保存,我遇到的问题是我必须按照达到的最高等级对高分进行排序。 LevelArray包含玩家达到的最高级别,并且我试图让他们处于某种状态,这样我就可以对生成的按钮进行排序。如果我的想法不起作用,有人可以提供另一种方法来排序这些按钮吗?下面我包含了我所知道的全局页面enter code here
包含循环的页面,并包含了我在进入带循环的页面时收到的错误。
错误
FATAL EXCEPTION: main
Process: com.example.dellstudio.projectone, PID: 20875
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.dellstudio.projectone/com.example.dellstudio.projectone.page3}: java.lang.ArrayIndexOutOfBoundsException: length=1; index=1
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2416)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2476)
at android.app.ActivityThread.-wrap11(ActivityThread.java)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1344)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:148)
at android.app.ActivityThread.main(ActivityThread.java:5417)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)
Caused by: java.lang.ArrayIndexOutOfBoundsException: length=1; index=1
at java.util.Vector.arrayIndexOutOfBoundsException(Vector.java:907)
at java.util.Vector.elementAt(Vector.java:328)
at java.util.Vector.get(Vector.java:442)
at com.example.dellstudio.projectone.global.getLevelArray(global.java:56)
at com.example.dellstudio.projectone.page3.onCreate(page3.java:47)
at android.app.Activity.performCreate(Activity.java:6237)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1107)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2369)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2476)
at android.app.ActivityThread.-wrap11(ActivityThread.java)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1344)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:148)
at android.app.ActivityThread.main(ActivityThread.java:5417)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)
有循环条件的页面
Button hs;
int numhold=0;
String namehold ;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.pagethree);
hs = (Button) findViewById(R.id.hsReturn);
hs.setOnClickListener(sListener);
LinearLayout l = (LinearLayout) findViewById(R.id.lay);
global gN = (global)getApplication();
for (int i = 0; i < gN.getArray().size(); i++) {
for (int j = 0; j < gN.getArray().size() - i; j++) {
if (gN.getLevelArray(j) < gN.getLevelArray(j + 1)) {
/* numhold = gN.getLevelArray(j);
gN.levelArray.set(j, gN.getLevelArray(j + 1));
gN.levelArray.set(j + 1, numhold);
namehold = gN.getNameArray(j);
gN.nameArray.set(j, gN.getNameArray(j + 1));
gN.nameArray.set(j + 1, namehold);
*/
}
}
}
for (int i = 0; i < gN.getArray().size(); i++) {
Button newButton = new Button(this);
newButton.setId(i);
newButton.setText(gN.getNameArray(i) + " Level: " + gN.getLevelArray(i));
newButton.setLayoutParams(new ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT));
// if(gN.name.equals(gN.nameArray.get(i))) {
// newButton.setOnClickListener(userListener);
// }
l.addView(newButton);
}
}
全局
List<Integer> levelArray = new Vector();
List<String> nameArray = new Vector();
public String getName (){
return this.name;
}
public void setName(String s){
this.name = s;
}
public String getPass (){
return this.pass;
}
public void setPass(String s){
this.pass = s;
}
public String getAge (){
return this.age;
}
public void setAge(String s){ this.age = s;
}
public Vector getArray (){return (Vector)this.nameArray;}
public String getNameArray (int i){return this.nameArray.get(i);}
public void setNameArray(String j){this.nameArray.add(j);}
public int getLevelArray (int i){return this.levelArray.get(i);}
public void setLevelArray(int i){this.levelArray.add(i);}
答案 0 :(得分:0)
看起来你有两个数组但是因为它们具有相同数量的元素而对它们进行处理。
也许level数组的子数少于name数组?