开发者

Got hit by an UnsupportedOperationException

开发者 https://www.devze.com 2023-03-04 08:38 出处:网络
I have the following code: private static ArrayAdapter<String> adapter; private static List<Chapter> chapters;

I have the following code:

    private static ArrayAdapter<String> adapter;
    private static List<Chapter> chapters;

    public void update(Book book) {
        adapter.clear();
        if (chapters != null) {
            chapters.clear();
        }
        chapters = DataBaseConnector.getChaptersFromBook(book.getID());
  开发者_如何学Python      for (Chapter chapter : chapters) {
            adapter.add(chapter.getTitle());
        }
        header.setText(book.getAbbreviation());
        subHeader.setText(book.getName() + " (" + book.getNumber() + ")");
        subHeader.setVisibility(View.VISIBLE);
    }

If I call the method update(Book book) and the variable chapters is not null, I get hit by an UnsupportedOperationException in the line chapters.clear(). Any hints how to solve the problem?


I would have to look at the API documentation, but presumably the DataBaseConnector.getChaptersFromBook(book.getID()) call is returning an Immutable list, so you can't modify it. It seems you will have to make a local copy.


According to the Android docs (and JavaDocs as well for the List interface):

"Throws UnsupportedOperationException if removing from this List is not supported."

My guess is the implementation that is being used does not support the clear() method.

0

精彩评论

暂无评论...
验证码 换一张
取 消