Launch Exception in the onCreate of an Activity

0

I'm working on the development and refactoring of some Activity's that has similar behaviors in our projects, so to avoid code repetitions, for common functionalities, I've created some Activity's base, which are inherited by the other Activity's. >

My problem is this, since what I am creating is class base ), which can be used by several others, I need to ensure that whoever uses it will use it, correct it. Therefore, I would like to check for nonconformities in the% base% parameters and to cast a class if there are nonconformities, so that these errors are caught in the development and testing phase.

This does not seem to be possible, here's what you'd like to do:

@Override
protected void onCreate(Bundle savedInstanceState) {
    userHelper = new UserHelper(this);

    super.onCreate(savedInstanceState);
    setContentView(R.layout.container_for_fragment);

    if (savedInstanceState == null) {
        if (mFragment != null) {
            getFragmentManager().beginTransaction()
                    .add(R.id.container, mFragment).commit();
        }else{
            // essa linha não compila: "Unhandled exception type Exception"
            throw new Exception("mFragment, deve ser setado antes da chamada do onCreate() da class base \"ListAbstractActivity\"");
        }
    }
}

Questions

  • Is there any way to do something?
  • What am I trying to do right?
  • Is there any plausible alternative to this situation?
  • asked by anonymous 24.03.2015 / 19:59

    1 answer

    1

    In the case of the Unhandled exception type Exception error that you mentioned, this code will not compile because Exception is a checked exception , that is, that the lance is bound to declare that he can cast it:

    onCreate(...) throws Exception {
    

    As the compiler will probably not let you declare this (since it would change the signature of the onCreate method), instead throw a RuntimeException or one of its subclasses that you consider most appropriate.

        
    24.03.2015 / 20:20