Search code examples
androidviewflipper

java.lang.IllegalStateException: System services not available to Activities before onCreate()


I am getting an exception :

java.lang.IllegalStateException: System services not available to Activities before onCreate()

while reinitializing layoutInflater in onResume() like :

layoutInflater = (LayoutInflater) context
                .getSystemService(Context.LAYOUT_INFLATER_SERVICE);

I am trying to updatethe The content of view flipper. Can anyone suggest me the corrections in this to resolve the exception.


Solution

  • Behold:

    public class MyActivity extends Activity {
    
        // LayoutInflater inflater = (LayoutInflater) context 
        //         .getSystemService(Context.LAYOUT_INFLATER_SERVICE); // NOT CORRECT
        LayoutInflater inflater; // correct
    
        @Override
        protected void onCreate(Bundle saved) {
            super.onCreate(saved);
            inflater = (LayoutInflater) context 
                .getSystemService(Context.LAYOUT_INFLATER_SERVICE); // correct
        }
    }