title: Detecting if android is connected to internet
body: <p>I have a big problem: I want control if <strong>3G</strong> or <strong>WiFi</strong> are activated.
  This is my code:</p>
  
  <pre><code>//controllo se è accesa la connessione 
          ConnectivityManager cm =
                  (ConnectivityManager)this.getSystemService(Context.CONNECTIVITY_SERVICE);
  
          NetworkInfo activeNetwork = cm.getActiveNetworkInfo();
          boolean isConnected = activeNetwork.isConnectedOrConnecting();
          //controllo se sono connesso
          if(isConnected==false){
              final AlertDialog.Builder builder=new AlertDialog.Builder(this);
          builder.setTitle("Attenzione!");
          builder.setMessage("L'applicazione senza la connessione ad internet non può funzionare. La preghiamo di attivarla.");
          builder.setIcon(android.R.drawable.ic_dialog_alert);
          builder.setPositiveButton("OK", new OnClickListener() {
  
              public void onClick(DialogInterface dialog, int which) {
  
              }
          });
          builder.show();}
  </code></pre>
  
  <p>It's into the oncreate. When I open the activity I have a force close. The cause is this:</p>
  
  <p>Caused by: java.lang.NullPointerException</p>
  
  <p>I don't understand where is the problem :(</p>