2

我有一个最终的字符串列表(它必须是一个最终列表才能在 SetonClickListener 中使用它),当我想将项目添加到这个列表时,它会抛出一个“UnSoppurtedOperationException”

这是日志猫:

04-06 09:05:35.585: E/AndroidRuntime(1179): FATAL EXCEPTION: main
04-06 09:05:35.585: E/AndroidRuntime(1179): java.lang.UnsupportedOperationException
04-06 09:05:35.585: E/AndroidRuntime(1179):     at java.util.AbstractList.add(AbstractList.java:404)
04-06 09:05:35.585: E/AndroidRuntime(1179):     at java.util.AbstractList.add(AbstractList.java:425)
04-06 09:05:35.585: E/AndroidRuntime(1179):     at com.example.sale.GroupActivity$MyCustomAdapter$1.onClick(GroupActivity.java:385)
04-06 09:05:35.585: E/AndroidRuntime(1179):     at android.view.View.performClick(View.java:4204)
04-06 09:05:35.585: E/AndroidRuntime(1179):     at android.view.View$PerformClick.run(View.java:17355)
04-06 09:05:35.585: E/AndroidRuntime(1179):     at android.os.Handler.handleCallback(Handler.java:725)
04-06 09:05:35.585: E/AndroidRuntime(1179):     at android.os.Handler.dispatchMessage(Handler.java:92)
04-06 09:05:35.585: E/AndroidRuntime(1179):     at android.os.Looper.loop(Looper.java:137)
04-06 09:05:35.585: E/AndroidRuntime(1179):     at android.app.ActivityThread.main(ActivityThread.java:5041)
04-06 09:05:35.585: E/AndroidRuntime(1179):     at java.lang.reflect.Method.invokeNative(Native Method)
04-06 09:05:35.585: E/AndroidRuntime(1179):     at java.lang.reflect.Method.invoke(Method.java:511)
04-06 09:05:35.585: E/AndroidRuntime(1179):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
04-06 09:05:35.585: E/AndroidRuntime(1179):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)
04-06 09:05:35.585: E/AndroidRuntime(1179):     at dalvik.system.NativeStart.main(Native Method)
04-06 09:05:38.784: I/Process(1179): Sending signal. PID: 1179 SIG: 9
04-06 09:05:39.694: E/Trace(1425): error opening trace file: No such file or directory (2)

这是我的代码:

          String name="a";

          String freindsAccepted = groupFreindsAcceptedInside.get(position);

            final List<String> accepted = Arrays.asList(freindsAccepted.split(","));
            final String[] arrayAccepted = accepted.toArray(new String[accepted.size()]);

acceptBtn.setOnClickListener(new View.OnClickListener() {
              @Override
              public void onClick(View arg0) {
                  Thread thread = new Thread()
                  {
                      @Override
                      public void run() {

                          //perform something
                      }
                  };

                  thread.start();

                  accepted.add(name);
                  String[] arrayAcceptedInside = accepted.toArray(new String[accepted.size()]);

                    if(accepted.size()>1) {
                        groupAccepted.setText("Accepted: "+accepted.get(0)+","+ accepted.get(1)+"...see More");
                    }
                    else {
                        groupAccepted.setText("Accepted: "+accepted.get(0));

                    }
              }
          });

其中第 385 行是:

          accepted.add(name);

多谢

4

2 回答 2

8

问题不在于它是最终变量 - 问题在于对象是 的返回值Arrays.asList

final List<String> accepted = Arrays.asList(freindsAccepted.split(","));

Arrays.asList(强调我的)的文档中:

返回由指定数组支持的固定大小的列表。(将返回的列表“写入”到数组的更改。)

听起来您可能想创建一个新的ArrayList(允许添加):

final List<String> accepted = new ArrayList<String>(
    Arrays.asList(freindsAccepted.split(","));

(顺便说一下,不清楚为什么你有arrayAcceptedandarrayAcceptedInside变量 - 它们只是用于诊断吗?)

于 2013-04-06T09:24:32.163 回答
1

这不是因为变量是 final 的,它是因为 Arrays.asList 返回一个固定大小的列表:

http://docs.oracle.com/javase/1.5.0/docs/api/java/util/Arrays.html#asList%28T...%29

您可以将其包装在新的 ArrayList 中以解决问题:

    final List<String> accepted = new ArrayList(Arrays.asList(freindsAccepted.split(",")));
于 2013-04-06T09:26:24.987 回答