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

StackOverflow https://stackoverflow.com/questions/23441694

  •  14-07-2023
  •  | 
  •  

Question

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.

Was it helpful?

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
    }
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top