尝试将值添加到List <integer>

时间:2015-08-06 13:30:20

标签: android database list sqlite

当我尝试在我的onResume()方法中向我的List添加一些值时出现此错误:

    java.lang.RuntimeException: Unable to resume activity {.../...MainActivity}: java.lang.UnsupportedOperationException
        at android.app.ActivityThread.performResumeActivity(ActivityThread.java:3096)
        at android.app.ActivityThread.handleResumeActivity(ActivityThread.java:3127)
        at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2482)
        at android.app.ActivityThread.-wrap11(ActivityThread.java)
        at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1345)
        at android.os.Handler.dispatchMessage(Handler.java:102)
        at android.os.Looper.loop(Looper.java:148)
        at android.app.ActivityThread.main(ActivityThread.java:5415)
        at java.lang.reflect.Method.invoke(Native Method)
        at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:725)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:615)
 Caused by: java.lang.UnsupportedOperationException
        at java.util.AbstractList.add(AbstractList.java:404)
        at java.util.AbstractList.add(AbstractList.java:425)
        at de.toeducate.tograde.MainActivity.onResume(MainActivity.java:151)
        at android.app.Instrumentation.callActivityOnResume(Instrumentation.java:1258)
        at android.app.Activity.performResume(Activity.java:6237) at android.app.ActivityThread.performResumeActivity(ActivityThread.java:3085)
            at android.app.ActivityThread.handleResumeActivity(ActivityThread.java:3127)
            at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2482)
            at android.app.ActivityThread.-wrap11(ActivityThread.java)
            at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1345)
            at android.os.Handler.dispatchMessage(Handler.java:102)
            at android.os.Looper.loop(Looper.java:148)
            at android.app.ActivityThread.main(ActivityThread.java:5415)
            at java.lang.reflect.Method.invoke(Native Method)
            at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:725)
            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:615)

这是我写的代码:

List<Integer> marks = Collections.emptyList();

    for (int i = 0; i < dataBase.getDataMark().size(); i++) {
        marks.add(dataBase.getDataMark().get(i).getValue());
    }

这是什么意思?

2 个答案:

答案 0 :(得分:5)

Collections.emptyList();会返回您无法添加的List(因此UnsupportedOperationException)。

使用

List<Integer> marks = new ArrayList<Integer>();

代替。

答案 1 :(得分:0)

Collections.emptyList()返回AbstractList的实例。这是由

行表示的
Caused by: java.lang.UnsupportedOperationException
    at java.util.AbstractList.add(AbstractList.java:404)

在你的异常堆栈跟踪中。这表示您无法在add列表中致电marks,因为add未实施。

使用允许添加元素的类来实例化marks,例如ArrayList

List<Integer> marks = new ArrayList<Integer>();

除了ArrayListLinkedList是支持添加元素的AbstractList的另一个实现。大多数情况下,程序员使用ArrayList