删除arrayList,java.lang.UnsupportedOperationException中的项

时间:2012-12-20 04:00:23

标签: android arraylist

我想删除数组列表中的项目选择位置

我的代码是

    List<String> historylist = Arrays.asList(history);
    if (historylist.size()>=3){
        System.out.print("*************************************************");
        Log.i(MyContants.StoredHistory_TAG,"StoredHistory size========="+ String.valueOf(historylist.size()));
        String removePosition=historylist.get(2);
        historylist.remove(removePosition);
    }

但我得到了一个Exception UnsupportedOperationException

错误

    12-20 09:16:27.680: I/StoredHistory Class(5715): StoredHistory size=========3
    12-20 09:16:27.680: D/AndroidRuntime(5715): Shutting down VM
    12-20 09:16:27.684: W/dalvikvm(5715): threadid=1: thread exiting with uncaught exception (group=0x4001d7d0)
    12-20 09:16:27.688: E/AndroidRuntime(5715): FATAL EXCEPTION: main
    12-20 09:16:27.688: E/AndroidRuntime(5715): java.lang.RuntimeException: Unable to start activity ComponentInfo{****PACKAGE NAME*****}: java.lang.UnsupportedOperationException
    12-20 09:16:27.688: E/AndroidRuntime(5715):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2663)
    12-20 09:16:27.688: E/AndroidRuntime(5715):     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2679)
    12-20 09:16:27.688: E/AndroidRuntime(5715):     at android.app.ActivityThread.access$2300(ActivityThread.java:125)
    12-20 09:16:27.688: E/AndroidRuntime(5715):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2033)
    12-20 09:16:27.688: E/AndroidRuntime(5715):     at android.os.Handler.dispatchMessage(Handler.java:99)
    12-20 09:16:27.688: E/AndroidRuntime(5715):     at android.os.Looper.loop(Looper.java:123)
    12-20 09:16:27.688: E/AndroidRuntime(5715):     at android.app.ActivityThread.main(ActivityThread.java:4627)
    12-20 09:16:27.688: E/AndroidRuntime(5715):     at java.lang.reflect.Method.invokeNative(Native Method)
    12-20 09:16:27.688: E/AndroidRuntime(5715):     at java.lang.reflect.Method.invoke(Method.java:521)
    12-20 09:16:27.688: E/AndroidRuntime(5715):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:858)
    12-20 09:16:27.688: E/AndroidRuntime(5715):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)
    12-20 09:16:27.688: E/AndroidRuntime(5715):     at dalvik.system.NativeStart.main(Native Method)
    12-20 09:16:27.688: E/AndroidRuntime(5715): Caused by: java.lang.UnsupportedOperationException
    12-20 09:16:27.688: E/AndroidRuntime(5715):     at java.util.AbstractList.remove(AbstractList.java:645)
    12-20 09:16:27.688: E/AndroidRuntime(5715):     at java.util.AbstractList$SimpleListIterator.remove(AbstractList.java:77)
    12-20 09:16:27.688: E/AndroidRuntime(5715):     at java.util.AbstractCollection.remove(AbstractCollection.java:230)
    12-20 09:16:27.688: E/AndroidRuntime(5715):     at ******.utills.StoredHistory.storedHistorylist(StoredHistory.java:24)
    12-20 09:16:27.688: E/AndroidRuntime(5715):     at *******.ScanResult.onCreate(ScanResult.java:53)
    12-20 09:16:27.688: E/AndroidRuntime(5715):     at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1049)
    12-20 09:16:27.688: E/AndroidRuntime(5715):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2627)
    12-20 09:16:27.688: E/AndroidRuntime(5715):     ... 11 more

如何解决这个问题..推进thanx dude

4 个答案:

答案 0 :(得分:6)

更改

List<String> historylist = Arrays.asList(history);

 List<String> historylist = new LinkedList (Arrays.asList(history));

并查看此帖子为什么在List:

的情况下会发生此错误

Why do I get an UnsupportedOperationException when trying to remove an element from a List?

答案 1 :(得分:5)

您无法在Arrays.asList返回的列表中添加或删除元素,因为该列表由原始数组支持。要创建可以修改的列表,可以执行以下操作:

List<String> historylist = new ArrayList<String>(Arrays.asList(history));

答案 2 :(得分:1)

我相信Arrays.asList()创建的是固定长度,因此不支持删除。

尝试使用支持删除类似ArrayList的实现。

答案 3 :(得分:0)

试试这个

 list.set(position,obj);