当我从列表中删除项目时,我得到了不受支持的操作异常。
new AlertDialog.Builder(getActivity())
.setMessage(result.getString("Message"))
.setPositiveButton("OK", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
miPaymentModel.remove(index);
})
.show();
}
堆栈跟踪就在这里
01-11 16:42:37.610: E/AndroidRuntime(25930): FATAL EXCEPTION: main
01-11 16:42:37.610: E/AndroidRuntime(25930): java.lang.UnsupportedOperationException
01-11 16:42:37.610: E/AndroidRuntime(25930): at java.util.AbstractList.remove(AbstractList.java:638)
01-11 16:42:37.610: E/AndroidRuntime(25930): at com.paksoft.people.Fragment.MiPaymentFragment$DeleteCardAccount$1.onClick(MiPaymentFragment.java:395)
01-11 16:42:37.610: E/AndroidRuntime(25930): at com.android.internal.app.AlertController$ButtonHandler.handleMessage(AlertController.java:166)
01-11 16:42:37.610: E/AndroidRuntime(25930): at android.os.Handler.dispatchMessage(Handler.java:99)
01-11 16:42:37.610: E/AndroidRuntime(25930): at android.os.Looper.loop(Looper.java:137)
01-11 16:42:37.610: E/AndroidRuntime(25930): at android.app.ActivityThread.main(ActivityThread.java:4499)
01-11 16:42:37.610: E/AndroidRuntime(25930): at java.lang.reflect.Method.invokeNative(Native Method)
01-11 16:42:37.610: E/AndroidRuntime(25930): at java.lang.reflect.Method.invoke(Method.java:511)
01-11 16:42:37.610: E/AndroidRuntime(25930): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:788)
01-11 16:42:37.610: E/AndroidRuntime(25930): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:555)
01-11 16:42:37.610: E/AndroidRuntime(25930): at dalvik.system.NativeStart.main(Native Method)
答案 0 :(得分:15)
在Arrays.asList
上返回固定大小的列表
来自Class API文档here
:
Arrays.asList
:返回指定数组支持的固定大小列表。
你不能add
到它;你不能remove
。您无法在结构上修改List
。
支持此类操作
创建LinkedList
,支持更快remove
。
List<String> list = new LinkedList<String>(Arrays.asList(split));
Ref Answer
:https://stackoverflow.com/a/2965808/2123594并且积分将转到@polygenelubricants
答案 1 :(得分:2)
试试这个..
您无法从Arrays.asList
返回的列表中删除项目,因为该列表由原始数组支持
List<String> miPaymentModel = new LinkedList<String>(Arrays.asList(array_name));
来自
的参考资料答案 2 :(得分:0)
抽象列表是一个带有一些方法的适配器,默认情况下它们不是抽象的,而是抛出UnsupportedOperationException&#39;
/**
* {@inheritDoc}
*
* <p>This implementation always throws an
* {@code UnsupportedOperationException}.
*
* @throws UnsupportedOperationException {@inheritDoc}
* @throws IndexOutOfBoundsException {@inheritDoc}
*/
public E remove(int index) {
throw new UnsupportedOperationException();
}
罪魁祸首是Arrays实现中的以下几行。
它不返回java.util.ArrayList
,这是一个具有误导性名称java.util.Arrays.ArrayList
的抽象列表的本地实现,它也是私有的。这是非常可怕的编码。
public static <T> List<T> asList(T... a) {
return new ArrayList<>(a);
}
/**
* @serial include
*/
private static class ArrayList<E> extends AbstractList<E>
implements RandomAccess, java.io.Serializable
{
private static final long serialVersionUID = -2764017481108945198L;
private final E[] a;
ArrayList(E[] array) {
a = Objects.requireNonNull(array);
}
@Override
public int size() {
return a.length;
}
@Override
public Object[] toArray() {
return a.clone();
}
@Override
@SuppressWarnings("unchecked")
public <T> T[] toArray(T[] a) {
int size = size();
if (a.length < size)
return Arrays.copyOf(this.a, size,
(Class<? extends T[]>) a.getClass());
System.arraycopy(this.a, 0, a, 0, size);
if (a.length > size)
a[size] = null;
return a;
}
@Override
public E get(int index) {
return a[index];
}
@Override
public E set(int index, E element) {
E oldValue = a[index];
a[index] = element;
return oldValue;
}
@Override
public int indexOf(Object o) {
if (o==null) {
for (int i=0; i<a.length; i++)
if (a[i]==null)
return i;
} else {
for (int i=0; i<a.length; i++)
if (o.equals(a[i]))
return i;
}
return -1;
}
@Override
public boolean contains(Object o) {
return indexOf(o) != -1;
}
@Override
public void forEach(Consumer<? super E> action) {
Objects.requireNonNull(action);
for (E e : a) {
action.accept(e);
}
}
@Override
public void replaceAll(UnaryOperator<E> operator) {
Objects.requireNonNull(operator);
E[] a = this.a;
for (int i = 0; i < a.length; i++) {
a[i] = operator.apply(a[i]);
}
}
@Override
public Spliterator<E> spliterator() {
return Spliterators.spliterator(a, Spliterator.ORDERED);
}
}