简体   繁体   English

从列表中删除时,Android中出现UnsupportedOperationException

[英]UnsupportedOperationException in Android when removing from List

First here is the code of the class that is bringing me trouble (its a class defined in a class): 首先,这是给我带来麻烦的类的代码(它是在类中定义的类):

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




}

And here is the invocation code that throws the exception (RegisteredAbilities is a SparseIntArray): 这是引发异常的调用代码(RegisteredAbilities是SparseIntArray):

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

The code fails with unsupported operation for remove on the last line and I can't undersand why. 代码失败,并在最后一行执行了不支持的删除操作,我不能理解为什么。 Is it becuase I'm calling the operation from an SparseIntArray? 是因为我要从SparseIntArray调用该操作吗?

Thanks for any help 谢谢你的帮助

EDIT: 编辑:

As requested here is the stack trace. 根据要求,这里是堆栈跟踪。

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)

From the API: 从API:

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

You can't add to it; 您不能添加到它; you can't remove from it. 您无法将其删除。 You can't structurally modify the List. 您不能在结构上修改列表。

Fix 固定

Create a LinkedList, which supports faster remove. 创建一个LinkedList,它支持更快的删除。

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 使用 ItemTouchHelper 从 RecyclerView 中删除项目时引发 UnsupportedOperationException - UnsupportedOperationException is thrown when removing item from RecyclerView using ItemTouchHelper 从 map 中删除键集值时出现 UnSupportedOperationException - Getting UnSupportedOperationException when removing keyset values from a map 尝试从另一个列表中删除一个项目列表时出现UnsupportedOperationException - UnsupportedOperationException when trying to remove a list of items from another list 尝试从 Array.asList 返回的列表中删除时出现 UnsupportedOperationException - UnsupportedOperationException when trying to remove from the list returned by Array.asList 从ArrayList中删除 <Form> 抛出UnsupportedOperationException - Removing from ArrayList<Form> throws an UnsupportedOperationException 从列表中删除值 <String> 在java中抛出java.lang.UnsupportedOperationException - removing a value from a list<String> in java throws java.lang.UnsupportedOperationException 在列表上调用.sort时AbstractList UnsupportedOperationException - AbstractList UnsupportedOperationException when calling .sort on a List 调用list.remove(0)时出现奇怪的UnsupportedOperationException - Strange UnsupportedOperationException when calling list.remove(0) java.lang.UnsupportedOperationException用于从javafx tableview中删除行 - java.lang.UnsupportedOperationException for removing a row from the javafx tableview 从成员列表中删除时,创建新对象(扩展线程)会导致 UnsupportedOperationException<E> - Creating new Object (extending Thread) results in UnsupportedOperationException when deleting from member List<E>
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM