当我尝试在我的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());
}
这是什么意思?
答案 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>();
除了ArrayList
,LinkedList
是支持添加元素的AbstractList
的另一个实现。大多数情况下,程序员使用ArrayList
。