简体   繁体   中英

Fixing Fatal Exception: main process Errors

I am a beginner to using Java and Android Studio, this code is part of a project from class for a point and click mole game. Clicking the mole a certain amount increases the level and after can be submitted as a high score. The game saves each players highscore as a generated button, the problem I'm having is the condition I have to sort the highscores by highest level reached. The LevelArray contains the the highest levels players have reached and I'm trying to get them in a condition so I can sort the generated buttons. If my idea does not work can someone offer another method for sorting these buttons? Below I included the global page I have wit enter code here h the page containing the loop, and included the errors I received when entering the page with the loop.

Errors

 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)

Page with loop condition

 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);
    }


}

Global

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);}

Looks like you have 2 arrays but treat them as they have equal number of elements.

Perhaps the level array has less children than the name array?

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM