从List中删除时Android中的UnsupportedOperationException

时间:2014-01-20 21:59:36

标签: java android exception

首先,这是给我带来麻烦的类的代码(它是在类中定义的类):

public static class MAbilitiesParseStruct{
    private List<CardView> cards;
    private List<String> abilities;
    private List<Boolean> ask;
    private List<Integer> regtype; //Used for regitering events.
    //private List<Integer> pos;
    private int current = 0;

    //public MAbilitiesParseStruct(List<CardView> cvs, List<String> abs, List<Boolean> asks, List<Integer> p){
    public MAbilitiesParseStruct(List<CardView> cvs, List<String> abs, List<Boolean> asks, List<Integer> regt){
        cards = cvs;
        abilities = abs;
        ask = asks;
        regtype = regt;
        //pos = p;
        current = 0;
    }

    public List<CardView> getCardList(){return cards;}
    public List<String> getAbilities(){return abilities;}
    public List<Boolean> getAsk(){return ask;}
    public List<Integer> getRegTypes(){return regtype;}

    public void addAbility(CardView v, String ability, boolean askq, int rtype){
        cards.add(v);
        abilities.add(ability);
        ask.add(askq);
        regtype.add(rtype);
    }

    public void appendAnotherMaps(MAbilitiesParseStruct another){
        cards.addAll(another.getCardList());
        abilities.addAll(another.getAbilities());
        ask.addAll(another.getAsk());
        regtype.addAll(another.getRegTypes());
    }

    public boolean done(){if (current == cards.size()) return true; else return false;}
    public void next(){ //It is assumed that this is called once current ability has finished being parsed.         
        if (cards.size() > 0){
            cards.remove(0);
            abilities.remove(0);
            ask.remove(0);
            regtype.remove(0);              
        }
    }

    public void purgeOneShots(){
        int i = 0;
        System.err.println("Purge de 1 shot");
        while (i < cards.size()){
            if (regtype.get(i) == Aux.REG_ONESHOT){
                System.err.println("Purge card");
                cards.remove(i);
                System.err.println("Purge ability");
                abilities.remove(i);
                System.err.println("Purge ask");
                ask.remove(i);
                System.err.println("Purge regtype");
                regtype.remove(i);                      
            }
            else{
                i++;
            }
        }
    }

    public void purgeTurns(){
        int i = 0;
        while (i < cards.size()){
            if (regtype.get(i) == Aux.REG_TURN){
                cards.remove(i);
                abilities.remove(i);
                ask.remove(i);
                regtype.remove(i);                      
            }
            else{
                i++;
            }
        }
    }       

    public String currentAbility(){return abilities.get(current);}
    public boolean currentAsk(){return ask.get(current);}
    public CardView currentCard(){return cards.get(current);}
    public void reset() {current = 0;}

    public void clearRemainingEvents(){ //This is used for when a certain card cancels an event.
        for (int i = current+1; i < cards.size(); i++){
            cards.remove(i);
            abilities.remove(i);
            ask.remove(i);
            regtype.remove(i);              
        }
    }




}

这是抛出异常的调用代码(RegisteredAbilities是SparseIntArray):

    MAbilitiesParseStruct temp = RegisteredAbilities.get(event,null);       
    if (temp != null){
        MAPS.appendAnotherMaps(temp);
        RegisteredAbilities.get(event).purgeOneShots();
    }

代码失败,在最后一行删除了不支持的操作,我不能理解为什么。是因为我正在从SparseIntArray调用该操作吗?

感谢您的帮助

编辑:

这里要求的是堆栈跟踪。

01-20 18:45:28.010: E/AndroidRuntime(29696): FATAL EXCEPTION: main
01-20 18:45:28.010: E/AndroidRuntime(29696): java.lang.UnsupportedOperationException
01-20 18:45:28.010: E/AndroidRuntime(29696):    at java.util.AbstractList.remove(AbstractList.java:638)
01-20 18:45:28.010: E/AndroidRuntime(29696):    at auxdata.AuxClasses$MAbilitiesParseStruct.purgeOneShots(AuxClasses.java:209)
01-20 18:45:28.010: E/AndroidRuntime(29696):    at legen.dary.GameEngine.LaunchEvent(GameEngine.java:611)
01-20 18:45:28.010: E/AndroidRuntime(29696):    at legen.dary.GameEngine.newVillainEntered(GameEngine.java:319)
01-20 18:45:28.010: E/AndroidRuntime(29696):    at legen.dary.GameEngine.GameTurn(GameEngine.java:193)
01-20 18:45:28.010: E/AndroidRuntime(29696):    at legen.dary.MainActivity.LoadGamePressed(MainActivity.java:290)
01-20 18:45:28.010: E/AndroidRuntime(29696):    at legen.dary.ButtonBlockView$1.onClick(ButtonBlockView.java:54)
01-20 18:45:28.010: E/AndroidRuntime(29696):    at android.view.View.performClick(View.java:4084)
01-20 18:45:28.010: E/AndroidRuntime(29696):    at android.view.View$PerformClick.run(View.java:16966)
01-20 18:45:28.010: E/AndroidRuntime(29696):    at android.os.Handler.handleCallback(Handler.java:615)
01-20 18:45:28.010: E/AndroidRuntime(29696):    at android.os.Handler.dispatchMessage(Handler.java:92)
01-20 18:45:28.010: E/AndroidRuntime(29696):    at android.os.Looper.loop(Looper.java:137)
01-20 18:45:28.010: E/AndroidRuntime(29696):    at android.app.ActivityThread.main(ActivityThread.java:4745)
01-20 18:45:28.010: E/AndroidRuntime(29696):    at java.lang.reflect.Method.invokeNative(Native Method)
01-20 18:45:28.010: E/AndroidRuntime(29696):    at java.lang.reflect.Method.invoke(Method.java:511)
01-20 18:45:28.010: E/AndroidRuntime(29696):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:786)
01-20 18:45:28.010: E/AndroidRuntime(29696):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
01-20 18:45:28.010: E/AndroidRuntime(29696):    at dalvik.system.NativeStart.main(Native Method)

1 个答案:

答案 0 :(得分:1)

来自API:

Arrays.asList: Returns a fixed-size list backed by the specified array.

您无法添加;你无法从中删除。您无法在结构上修改列表。

<强>修正

创建一个LinkedList,支持更快的删除。

List<String> list = new LinkedList<String>(Arrays.asList(split));