java.lang.IllegalArgumentException: Wrong state classs

出處:http://blog.csdn.net/yilip/article/details/44345655

java.lang.IllegalArgumentException: Wrong state class, expecting View State but received class cn.etouch.ecalendar.waterfallview.StaggeredGridView$GridListSavedState instead. This usually happens when two views of different type have the same id in the same hierarchy. This view's id is id/refresh_gridView. Make sure other views do not use the same id.


 按照Log的提示,是id起了衝突。但是我經過仔細查看XML佈局文件,並沒有起衝突的ID,事實上,在xml佈局文件中經常有重名的id。網上還有一些說通過clean項目,這個也解決不了問題。

   我遇到的情形是:FragmentA 中包括FragmentA1,FragmentA2,FragmentA3,FragementA3中有一個自定義的GridView,當A1,A2和A3之間切換時,程序就會崩潰,並報上述的錯誤。

   id相同?確實可能是ID相同。因爲,當A1,A2和A3切換的時候,將A3的gridView狀態保存了,當然id也保存下來了。下次再切換到A3就可能出現id相同的情形。(不知道理解的對不對?)

   這時候需要重寫GridView中的onRestoreInstanceState函數

     默認:

           @Override
    protected void onRestoreInstanceState(Parcelable state) {
            super.onRestoreInstanceState(state); }

      修改爲:

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

     再次運行程序,問題得到解決


發佈了31 篇原創文章 · 獲贊 43 · 訪問量 21萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章