user1634451
user1634451

Reputation: 5212

Wrong state class, expecting View State but

I am getting this error

  java.lang.IllegalArgumentException: Wrong state class, expecting View State but received class android.widget.AbsListView$SavedState instead. This usually happens when two views of different type have the same id in the same hierarchy. This view's id is id/unique111123234. Make sure other views do not use the same id.

This error happens when I switch between tabs in my view pager. I am using a custom compound view below

public class RefreshableListView extends SwipeRefreshLayout {
    private ListView listView;
    private boolean disabled = false;
    private Context context = null;
    private AttributeSet attributes = null;
    private RelativeLayout layout;
public RefreshableListView(Context context) {
    super(context);
    this.context = context;
    initView();
}

public RefreshableListView(Context context, AttributeSet attrs) {
    super(context, attrs);
    this.context = context;
    this.attributes = attrs;
    initView();
}

private void initListView() {
    listView = new ListView(context, attributes);
    layout = new RelativeLayout(context);
    this.addView(layout);
    layout.addView(listView);
}

private void initView() {
    initListView();
    setColorScheme(android.R.color.holo_blue_bright,
            android.R.color.holo_green_light,
            android.R.color.holo_orange_light,
            android.R.color.holo_red_light);
}

public void setAdapter(ListAdapter adapter) {
    listView.setAdapter(adapter);
}

public void setOnItemClickListener(AdapterView.OnItemClickListener listener) {
    listView.setOnItemClickListener(listener);
}


@Override
public boolean canChildScrollUp() {
    boolean canScroll = listView.getFirstVisiblePosition() != 0;
    if (disabled) {
        canScroll = true;
    }
    return canScroll;
}

public Object getItemAtPosition(int position) {
    return listView.getItemAtPosition(position);
}

public void setDisabled(boolean disable) {
    disabled = disable;
}

public boolean isDisabled() {
    return disabled;
}

public void setEmptyView(View emptyView) {
    layout.addView(emptyView);
    listView.setEmptyView(emptyView);
}

public void setSelection(int selection) {
    listView.setSelection(selection);
}

public int getFirstVisiblePosition() {
    return listView.getFirstVisiblePosition();
}

public Adapter getAdapter() {
    return listView.getAdapter();
}

public int getCount() {
    return listView.getCount();
}
}

I am stumped does anyone have an incite?

Upvotes: 4

Views: 7713

Answers (2)

Entreco
Entreco

Reputation: 12900

Alternatively, you can set Id's yourself to avoid conflicts. I had the exact same error when I was adding views programatically to my custom compound widget!

In your case, the fix would be to:

private void initListView() {
    listView = new ListView(context, attributes);
    layout = new RelativeLayout(context);

    // Set Id's to ensure they are not the same
    listView.setId(1000);
    layout.setId(10001);

    this.addView(layout);
    layout.addView(listView);
}

I guess when you inflate resources from XML, android assigns a random Id to the inflated views

Upvotes: 5

user1634451
user1634451

Reputation: 5212

I found that if I inflate the views in initListView() from xml it works.

Upvotes: 1

Related Questions