title: Yet another "Can't create handler inside thread that has not called Looper.prepare()" topic
body: <p>I have this code which is an Activity that when started will check for internet connection, if there is a connection, then life goes on. Else a dialog appears to turn on the connection. However I made a thread that each 10 seconds will check for connection and in case the connection was lost it will display the dialog again.</p>
  
  <pre><code>package greensmartcampus.eu.smartcampususerfeedbackapp;
  
  import android.app.AlertDialog;
  import android.content.DialogInterface;
  import android.content.Intent;
  import android.os.Bundle;
  import android.provider.Settings;
  import android.view.Menu;
  import android.view.MenuItem;
  import android.widget.Toast;
  
  import java.net.InetAddress;
  
  
  public class HomeScreen extends AbstractPortraitActivity {
  
      private static final int WIFI_REQUEST_CODE = 1;
      private boolean networkSettingsDialogOpened = false;
  
      @Override
      protected void onCreate(Bundle savedInstanceState) {
          super.onCreate(savedInstanceState);
          setContentView(R.layout.activity_home_screen);
          this.runOnUiThread(new Runnable() {
              @Override
              public void run() {
                  while (!HomeScreen.this.isInternetAvailable()) {
                      if (!networkSettingsDialogOpened)
                          HomeScreen.this.createNetErrorDialog();
                      try {
                          Thread.sleep(10000);
                      } catch (InterruptedException e) {
                          e.printStackTrace();
                      }
                  }
              }
          });
      }
  
  (...)
  
      private boolean isInternetAvailable() {
          try {
              final InetAddress ipAddr = InetAddress.getByName("google.com");
              if (ipAddr.equals("")) {
                  return false;
              } else {
                  return true;
              }
          } catch (Exception e) {
              return false;
          }
      }
  
      private void createNetErrorDialog() {
          networkSettingsDialogOpened = true;
          final AlertDialog.Builder builder = new AlertDialog.Builder(this);
          builder.setMessage("You need a network connection to use this application. Please turn on mobile network or Wi-Fi in Settings.")
                  .setTitle("Unable to connect")
                  .setCancelable(false)
                  .setPositiveButton("Settings",
                          new DialogInterface.OnClickListener() {
                              public void onClick(DialogInterface dialog, int id) {
                                  Intent i = new Intent(Settings.ACTION_WIRELESS_SETTINGS);
                                  startActivityForResult(i, WIFI_REQUEST_CODE);
                              }
                          }
                  )
                  .setNegativeButton("Cancel",
                          new DialogInterface.OnClickListener() {
                              public void onClick(DialogInterface dialog, int id) {
                                  HomeScreen.this.finish();
                              }
                          }
                  );
          final AlertDialog alert = builder.create();
          alert.show();
      }
  
      protected void onActivityResult(int requestCode, int resultCode, Intent data) {
          if (requestCode == WIFI_REQUEST_CODE) {
              if (resultCode == RESULT_OK) {
                  networkSettingsDialogOpened = false;
                  Toast.makeText(HomeScreen.this, "Returned Ok",
                          Toast.LENGTH_LONG).show();
              }
              if (resultCode == RESULT_CANCELED) {
                  networkSettingsDialogOpened = false;
                  Toast.makeText(HomeScreen.this, "Returned Canceled",
                          Toast.LENGTH_LONG).show();
              }
          }
      }
  }
  </code></pre>
  
  <p>However I am getting the following error:</p>
  
  <pre><code>02-03 18:13:14.525    2683-2699/greensmartcampus.eu.smartcampususerfeedbackapp E/AndroidRuntime﹕ FATAL EXCEPTION: Thread-193
      Process: greensmartcampus.eu.smartcampususerfeedbackapp, PID: 2683
      java.lang.RuntimeException: Can't create handler inside thread that has not called Looper.prepare()
              at android.os.Handler.&lt;init&gt;(Handler.java:200)
              at android.os.Handler.&lt;init&gt;(Handler.java:114)
              at android.app.Dialog.&lt;init&gt;(Dialog.java:108)
              at android.app.AlertDialog.&lt;init&gt;(AlertDialog.java:125)
              at android.app.AlertDialog$Builder.create(AlertDialog.java:967)
              at greensmartcampus.eu.smartcampususerfeedbackapp.HomeScreen.createNetErrorDialog(HomeScreen.java:97)
              at greensmartcampus.eu.smartcampususerfeedbackapp.HomeScreen.access$200(HomeScreen.java:15)
              at greensmartcampus.eu.smartcampususerfeedbackapp.HomeScreen$1.run(HomeScreen.java:29)
  </code></pre>
  
  <p><strong>Note:</strong> Line 97 is the one containing:</p>
  
  <pre><code>final AlertDialog alert = builder.create();
  </code></pre>
  
  <p>I googled alot, I am already using the cliche answer of <strong>runOnUiThread</strong>, but it doesn't fix it.</p>
  
  <p>What am I missing?</p>