Android里面有个异常,叫SuperNotCalledException,如果子类没有通过super.XXX()调用父类的方法,则报此异常,请问这是怎么实现的。

解决方案 »

  1.   

    可以参考Fragment.java,Activity.java等代码,以Fragment.java来说明,
        void performResume() {        mCalled = false;
            onResume();
            if (!mCalled) {
                throw new SuperNotCalledException("Fragment " + this
                        + " did not call through to super.onResume()");
            }    }    public void onResume() {
            mCalled = true;
        }
     
    在调用onResume之前先设置变量mCalled 为false,然后调用onResume,而这个mCalled 是在父类(也就是当前调用onResume的类)的onResume中设置为true的,如果子类没有写super.onResume(),自然就不会置为true,然后就可以抛出异常了。
      

  2.   

    比如onPause处理,在ActivityThread.java中,final Bundle performPauseActivity(ActivityRecord r, boolean finished, boolean saveState) {
    ......
          if (!r.activity.mCalled) {
              throw new SuperNotCalledException(
                  "Activity " + r.intent.getComponent().toShortString() +
                  " did not call through to super.onPause()");
          }
    ......
    }如果对象的mCalled没有被置为true,就抛出SuperNotCalledException异常