java.lang.IllegalArgumentException: Wrong state class, expecting View State but received

今天在開發過程中遇到了以下的錯誤:
java.lang.IllegalArgumentException: Wrong state class, expecting View State but received class android.support.v7.widget.RecyclerView$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/dynamic_rv. Make sure other views do not use the same id.
因爲我的首頁是用viewpager結合fragment做的幾個可以左右滑動的TAB頁面,A頁面又用了github上開源的下拉刷新控件,當離開A頁面後再從其他頁面返回時,便出現了以上錯誤,大致意思就是layout的ID重複了,至於爲什麼會重複,就沒有深入瞭解下去了。
解決方式是找到自定義控件的類,比如我的是這樣的

        public class NRecyclerView extends BaseLayout {

    private RecyclerView.LayoutManager layoutManager;
    private InnerAdapter adapter;
    private final RecyclerView.AdapterDataObserver mDataObserver = new DataObserver();

    public NRecyclerView(Context context, AttributeSet attrs) {
        super(context, attrs);
    }

    private ViewGroup AdtureView;
    private ViewGroup BottomView;
    ........

在此控件中重寫onRestoreInstanceState方法,

    @Override
    protected void onRestoreInstanceState(Parcelable state) {
        try {
            super.onRestoreInstanceState(state);
        }catch (Exception e) {}
        state=null;
    }

解決問題。

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章