java.lang.IllegalArgumentException: Wrong state classs

      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;
      }

     再次执行程序。问题得到解决

原文地址:https://www.cnblogs.com/clnchanpin/p/6798024.html