title: "System services not available to Activities before onCreate()" Error message?
body: <p>When the user hits an icon in my app, I want the app first to check if the device is connected to the internet and then do something depending on the result it receives (for know it's just popping up a dialog, informing whether the device is connected or not). So I wrote this code:</p>
  
  <pre><code>public class MainActivity extends Activity {
  
  // SOME CONSTANTS WILL BE DEFINED HERE
  
  AlertDialog.Builder builder = new AlertDialog.Builder(this);
  
  @Override
  public void onCreate(Bundle savedInstanceState) {
      super.onCreate(savedInstanceState);
      setContentView(R.layout.activity_main);
  
      findViewById(R.id.icoMyIcon).setOnClickListener(listener);
  }
  
  
  private OnClickListener listener = new OnClickListener() {
  
      public void onClick(View v) {
          if (isNetworkConnected()) {
              builder.setMessage("Internet connected!").setCancelable(false)
              .setPositiveButton("OK", null);
              builder.create().show();
          } else {
              builder.setMessage("Internet isn\'t connected!")
              .setCancelable(false)
              .setPositiveButton("OK", null);
              builder.create().show();
          }
  
      }
  };
  
  
  // Check if the device is connected to the Internet
  private boolean isNetworkConnected() {
      ConnectivityManager cm = (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE);
      NetworkInfo ni = cm.getActiveNetworkInfo();
      if (ni == null) {
          // There are no active networks.
          return false;
      } else
          return true;
  }
  
  @Override
  public boolean onCreateOptionsMenu(Menu menu) {
      getMenuInflater().inflate(R.menu.activity_main, menu);
      return true;
  }
  
  }
  </code></pre>
  
  <p>When I'm trying to run this App on the emulator it keeps crushing and I'm getting this Error messages in LogCat:</p>
  
  <pre><code>07-24 22:59:45.034: E/AndroidRuntime(894): FATAL EXCEPTION: main
  07-24 22:59:45.034: E/AndroidRuntime(894): java.lang.RuntimeException: Unable to 
      instantiate activity ComponentInfo{com.my.app/com.my.app.MainActivity}: 
      java.lang.IllegalStateException: System services not available to Activities before onCreate()
  07-24 22:59:45.034: E/AndroidRuntime(894):  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2585)
  07-24 22:59:45.034: E/AndroidRuntime(894):  at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2679)
  07-24 22:59:45.034: E/AndroidRuntime(894):  at android.app.ActivityThread.access$2300(ActivityThread.java:125)
  07-24 22:59:45.034: E/AndroidRuntime(894):  at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2033)
  07-24 22:59:45.034: E/AndroidRuntime(894):  at android.os.Handler.dispatchMessage(Handler.java:99)
  07-24 22:59:45.034: E/AndroidRuntime(894):  at android.os.Looper.loop(Looper.java:123)
  07-24 22:59:45.034: E/AndroidRuntime(894):  at android.app.ActivityThread.main(ActivityThread.java:4627)
  07-24 22:59:45.034: E/AndroidRuntime(894):  at java.lang.reflect.Method.invokeNative(Native Method)
  07-24 22:59:45.034: E/AndroidRuntime(894):  at java.lang.reflect.Method.invoke(Method.java:521)
  07-24 22:59:45.034: E/AndroidRuntime(894):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868)
  07-24 22:59:45.034: E/AndroidRuntime(894):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626)
  07-24 22:59:45.034: E/AndroidRuntime(894):  at dalvik.system.NativeStart.main(Native Method)
  07-24 22:59:45.034: E/AndroidRuntime(894): Caused by: java.lang.IllegalStateException: System services not available to Activities before onCreate()
  07-24 22:59:45.034: E/AndroidRuntime(894):  at android.app.Activity.getSystemService(Activity.java:3526)
  07-24 22:59:45.034: E/AndroidRuntime(894):  at com.android.internal.app.AlertController$AlertParams.&lt;init&gt;(AlertController.java:743)
  07-24 22:59:45.034: E/AndroidRuntime(894):  at android.app.AlertDialog$Builder.&lt;init&gt;(AlertDialog.java:273)
  07-24 22:59:45.034: E/AndroidRuntime(894):  at com.my.app.MainActivity.&lt;init&gt;(MainActivity.java:24)
  07-24 22:59:45.034: E/AndroidRuntime(894):  at java.lang.Class.newInstanceImpl(Native Method)
  07-24 22:59:45.034: E/AndroidRuntime(894):  at java.lang.Class.newInstance(Class.java:1429)
  07-24 22:59:45.034: E/AndroidRuntime(894):  at android.app.Instrumentation.newActivity(Instrumentation.java:1021)
  07-24 22:59:45.034: E/AndroidRuntime(894):  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2577)
  07-24 22:59:45.034: E/AndroidRuntime(894):  ... 11 more
  </code></pre>
  
  <p>Why is it happening and how do I fix it? I'm a novice at this, so... please be gentle! :)</p>