设置条件(小于大小)但仍然索引超出范围

时间:2018-03-14 19:13:48

标签: java android android-studio-3.0

我很困惑。我设置条件“if(mSecondCount< mUsedItem.size())”但仍然收到索引超出绑定的异常。异常指向的行是“if(mUsedItem.get(mSecondCount).getItemNum()== 5)”。 这些代码在多个设备上运行。 (多人实时在线游戏),但只有一个设备崩溃。 请帮助我解决它

private void messageViewDisplayItemAfterEffect(Room fallenRoom) {
    Log.i(TAG, "Triggering after effect Items: " + mUsedItem);
    if (mUsedItem.size()>0){
        if (mSecondCount<mUsedItem.size()){
            disableContinue();
            mMessageView.setVisibility(View.VISIBLE);
            mMessageView.setEnabled(false);
            if (mSecondCount==0){
                mMessageView.setText("Items have been used, Some Items will triggered after affect");
            } else {
                mMessageView.setText("Loading Information");
            }
            Handler handler = new Handler();
            handler.postDelayed(new Runnable() {
                @Override
                public void run() {
                    if (mUsedItem.get(mSecondCount).getItemNum()==5){
                        mMessageView.setText("The affect of hareware has expired, the blocked zombie has returned");
                        fallenRoom.zombieApproached();
                    } else if (mUsedItem.get(mSecondCount).getItemNum()==6){
                        mMessageView.setText("The affect of hidden has expired, the hidden character has returned");
                        fallenRoom.enter(gameBroad.matchGameCharacter(mPlayersUsedItem.get(mSecondCount),mUsedItem.get(mSecondCount).getAffectedGameCharacter().getName()));
                    } else if (mUsedItem.get(mSecondCount).getItemNum()==7){
                        mMessageView.setText("The affect of sprint has trigger, the left character has moved to the destination");
                        gameBroad.matchRoom(mUsedItem.get(mSecondCount).getAfteraffectedRoomNumber())
                                .enter(gameBroad.matchGameCharacter(mPlayersUsedItem.get(mSecondCount), mUsedItem.get(mSecondCount).getAffectedGameCharacter().getName()));
                    }
                    writeRoomIntoFireBase(fallenRoom);
                    updateRoom(MainActivity.this);
                    mMainActivityLayout.invalidate();
                    Handler handler1 = new Handler();
                    handler1.postDelayed(new Runnable() {
                        @Override
                        public void run() {
                            updateRoom(MainActivity.this);
                            mMainActivityLayout.invalidate();
                            mSecondCount++;
                            if (mMyPlayerID==getControlId()){
                                GameData gameData = new GameData(mCountPhase,mCountSetUp, mSecondCount,mThirdCount,mFourthCount,mFifthCount,mSixCount);
                                mDatabaseReference.child(GAMEDATA).setValue(gameData);
                                mDatabaseReference.child(TURN).setValue(-mSecondCount-1);
                            }
                        }
                    },DELAYEDSECONDSFORMESSAGEVIE*1000);
                }
            },DELAYEDSECONDSFORMESSAGEVIE*1000);
        } else if (mSecondCount==mUsedItem.size()){
            mMessageView.setText("Items after effect calculation finished");
            Handler handler1 = new Handler();
            handler1.postDelayed(new Runnable() {
                @Override
                public void run() {
                    updateRoom(MainActivity.this);
                    mMainActivityLayout.invalidate();
                    mFourthCount=5;
                    mCountSetUp=0;
                    mSecondCount=0;
                    mThirdCount=0;
                    if (mMyPlayerID==getControlId()){
                        GameData gameData = new GameData(mCountPhase,mCountSetUp, mSecondCount,mThirdCount,mFourthCount,mFifthCount,mSixCount);
                        mDatabaseReference.child(GAMEDATA).setValue(gameData);
                        mDatabaseReference.child(TURN).setValue(-20);
                    }

                }
            },DELAYEDSECONDSFORMESSAGEVIE*1000);
        }
    } else{
        mMessageView.setText("No Item will trigger after effect");
        Handler handler1 = new Handler();
        handler1.postDelayed(new Runnable() {
            @Override
            public void run() {
                updateRoom(MainActivity.this);
                mMainActivityLayout.invalidate();
                mFourthCount=5;
                mCountSetUp=0;
                mSecondCount=0;
                mThirdCount=0;
                if (mMyPlayerID == getControlId()){
                    GameData gameData = new GameData(mCountPhase,mCountSetUp, mSecondCount,mThirdCount,mFourthCount,mFifthCount,mSixCount);
                    mDatabaseReference.child(GAMEDATA).setValue(gameData);
                    mDatabaseReference.child(TURN).setValue(-40);
                }
            }
        },DELAYEDSECONDSFORMESSAGEVIE*1000);
    }
}

和例外情况如下

03-14 11:35:41.835 11603-11603/? E/AndroidRuntime: FATAL EXCEPTION: main
                                               Process: com.bignerdranch.android.mallofhorrorandroid, PID: 11603
                                               java.lang.IndexOutOfBoundsException: Index: 2, Size: 2
                                                   at java.util.ArrayList.get(ArrayList.java:411)
                                                   at com.bignerdranch.android.mallofhorrorandroid.MainActivity$57.run(MainActivity.java:2268)
                                                   at android.os.Handler.handleCallback(Handler.java:754)
                                                   at android.os.Handler.dispatchMessage(Handler.java:95)
                                                   at android.os.Looper.loop(Looper.java:163)
                                                   at android.app.ActivityThread.main(ActivityThread.java:6342)
                                                   at java.lang.reflect.Method.invoke(Native Method)
                                                   at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:880)
                                                   at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:770)

0 个答案:

没有答案