0

When I run this application it crashes if there is no Internet connectivity.
Why?

I have a separate Class in which I have a method to provide Internet details.
Internet permission is set in the Manifest

public class MainActivity extends Activity {

private WebView webView;
private AlertDialog.Builder alertDiaBuilder;
private ProgressDialog progressBar;
private int netState = 0;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    setContentView(R.layout.activity_main);

    webView = (WebView) findViewById(R.id.webView1);

    progressBar = new ProgressDialog(MainActivity.this);

    progressBar.setCancelable(false);

    progressBar.setTitle("Checking network connectivity");

    progressBar.setMessage("Wait for a movement!");

    progressBar.setProgressStyle(ProgressDialog.STYLE_SPINNER);

    progressBar.show();

    new Thread(new Runnable() {

        public void run() {

            netState = NetworkUtils.getConnectivityStatus(MainActivity.this);

            if (netState == 1 || netState == 2 || netState == 3
                    || netState == 4) {

                try {

                    Thread.sleep(5000);

                    progressBar.dismiss();

                    webView.loadUrl("http://www.yahoo.co.in");

                } catch (InterruptedException e) {

                    e.printStackTrace();
                }
            } else {

                alertDiaBuilder = new AlertDialog.Builder(MainActivity.this);
                alertDiaBuilder.setMessage("Not Connected to Internet");

                alertDiaBuilder.setCancelable(false);

                alertDiaBuilder.setNegativeButton("Close application",

                        new OnClickListener() {
                            @Override
                            public void onClick(DialogInterface dialog,
                                    int which) {

                                finish();
                                //dialog.cancel();
                            }
                        });

                alertDiaBuilder.setPositiveButton("Go offline",
                        new OnClickListener() {

                            @Override
                            public void onClick(DialogInterface dialog,
                                    int which) {

                                Toast.makeText(getApplicationContext(),
                                        "Ready to go offline",
                                        Toast.LENGTH_LONG).show();
                            }
                        });

                AlertDialog alertDialog = alertDiaBuilder.create();

                alertDialog.show();
            }
        }

    }).start();
}
}
Phantômaxx
  • 37,901
  • 21
  • 84
  • 115
Ramesh Kumar
  • 345
  • 1
  • 4
  • 10
  • 3
    This question is useless, and nobody can probably help you if you do not include your LogCat / error stacktrace... – Marko Dec 31 '15 at 08:45
  • java.lang.RuntimeException: Can't create handler inside thread that has not called Looper.prepare() – Ramesh Kumar Dec 31 '15 at 08:51
  • 1
    Are you kidding us? Is this your logcat? Really? – Phantômaxx Dec 31 '15 at 08:51
  • 1
    Google around the error you got and you will find your solution ([first](http://stackoverflow.com/questions/3614663/cant-create-handler-inside-thread-that-has-not-called-looper-prepare-inside-a), [second](http://stackoverflow.com/questions/3875184/cant-create-handler-inside-thread-that-has-not-called-looper-prepare), [third](http://stackoverflow.com/questions/6213538/cant-create-handler-inside-thread-that-has-not-called-looper-prepare))... The problem is that you need to work with UI elements on the main UI thread (like showing a AlertDialog), not on a worker thread, like you are doing now. – Marko Dec 31 '15 at 08:59
  • You should use AsyncTask instead by Thread. You can show dialog in onPreExecute() and hide it in onPostExecute() – GiapLee Dec 31 '15 at 09:02
  • 1
    again, please post you logcat – Linh Dec 31 '15 at 09:06
  • Try accessing your UI components in the MainThread and do your network related operations in a background thread. AsyncTask may be a best option. – Emil Dec 31 '15 at 09:22

0 Answers0