2

I want to display Toast when the internet connection is available but it makes error.

my code

public class MainActivity extends ActionBarActivity {
    EditText et, pass;
    ConnectivityManager cm;
    NetworkInfo ni;

    ProgressDialog dialog = null;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        Log.i("LOG", "Start <-- ");


        et = (EditText) findViewById(R.id.username);
        pass = (EditText) findViewById(R.id.password);

    }


    public void onClick(View v) {
        Log.i("LOG", "click button <-- ");
        if(!et.getText().toString().matches("") &&
         !pass.getText().toString().matches(""))
        {
            Log.i("LOG", "check network <-- ");
            isNetworkConnected();   

        }else{
            Toast.makeText(getBaseContext(),"Enter user & pass",
                           Toast.LENGTH_SHORT).show();

        }}


    private boolean isNetworkConnected() {
        Log.i("LOG", "start check network <-- ");
        cm = (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE);
        ni = cm.getActiveNetworkInfo();
        if (ni == null) {

            Toast.makeText(getApplicationContext(),
                           "Make sure you connect to the Internet!", Toast.LENGTH_SHORT).show();

            return false;
        }else {
            waiting();
            return true;
        }   


    }
    void waiting(){
        dialog = ProgressDialog.show(MainActivity.this, "",
                                     "Please wait a moment....", true);
        new Thread(new Runnable() {
                public void run() {
                    ShowToast(MainActivity.this); 
                    }
            }).start();
    }

    void ShowToast(Context context) {

            runOnUiThread(new Runnable() {
                public void run() {
                    dialog.dismiss();
                }
            });

            Log.i("LOG", "1 <-- ");
            Toast.makeText(context,"Connect OK",
                           Toast.LENGTH_SHORT).show();
            Log.i("LOG", "2 <-- ");

    }

}

and this is error in log

08-04 23:24:45.476: I/LOG(1169): 1 <-- 
08-04 23:24:45.476: W/dalvikvm(1169): threadid=14: thread exiting with uncaught exception (group=0x414c4700)
08-04 23:24:45.506: E/AndroidRuntime(1169): FATAL EXCEPTION: Thread-99
08-04 23:24:45.506: E/AndroidRuntime(1169): java.lang.RuntimeException: Can't create handler inside thread that has not called Looper.prepare()
08-04 23:24:45.506: E/AndroidRuntime(1169):     at android.os.Handler.<init>(Handler.java:197)
08-04 23:24:45.506: E/AndroidRuntime(1169):     at android.os.Handler.<init>(Handler.java:111)
08-04 23:24:45.506: E/AndroidRuntime(1169):     at android.widget.Toast$TN.<init>(Toast.java:324)
08-04 23:24:45.506: E/AndroidRuntime(1169):     at android.widget.Toast.<init>(Toast.java:91)
08-04 23:24:45.506: E/AndroidRuntime(1169):     at android.widget.Toast.makeText(Toast.java:238)
08-04 23:24:45.506: E/AndroidRuntime(1169):     at ir.sheikhoo.registerproject.MainActivity.ShowToast(MainActivity.java:101)
08-04 23:24:45.506: E/AndroidRuntime(1169):     at ir.sheikhoo.registerproject.MainActivity$1.run(MainActivity.java:87)
08-04 23:24:45.506: E/AndroidRuntime(1169):     at java.lang.Thread.run(Thread.java:841)
08-04 23:24:45.577: W/ActivityManager(286):   Force finishing activity ir.sheikhoo.registerproject/.MainActivity
08-04 23:24:45.596: W/WindowManager(286): Screenshot failure taking screenshot for (164x246) to layer 21015
hata
  • 11,633
  • 6
  • 46
  • 69
sadegh
  • 1,720
  • 1
  • 16
  • 30

4 Answers4

4

Toast should be on UI thread,

replace,

runOnUiThread(new Runnable() {
                public void run() {
                    dialog.dismiss();
                }
            });

            Log.i("LOG", "1 <-- ");
            Toast.makeText(context,"Connect OK",
                           Toast.LENGTH_SHORT).show();
            Log.i("LOG", "2 <-- ");

with 

   runOnUiThread(new Runnable() {
                public void run() {
                    dialog.dismiss();
                     Toast.makeText(context,"Connect OK",
                           Toast.LENGTH_SHORT).show();
                }
            });

            Log.i("LOG", "1 <-- ");

            Log.i("LOG", "2 <-- ");
Ankit
  • 101
  • 5
3

According to the error, you're calling a toast from a worker thread but you should call it from a function that deals with the UI thread. Check the same problem here: Can't create handler inside thread that has not called Looper.prepare()

Community
  • 1
  • 1
SaNtoRiaN
  • 2,212
  • 2
  • 15
  • 25
3

You can only show Toast in UI thread. Not sure why you were creating a new thread there.

void waiting(){
    dialog = ProgressDialog.show(MainActivity.this, "",
                                 "Please wait a moment....", true);
    new Thread(new Runnable() {
            public void run() {
                ShowToast(MainActivity.this); 
                }
        }).start();
}

void ShowToast(Context context) {

        runOnUiThread(new Runnable() {
            public void run() {
                dialog.dismiss();
                Log.i("LOG", "1 <-- ");
                Toast.makeText(context,"Connect OK",
                       Toast.LENGTH_SHORT).show();
                Log.i("LOG", "2 <-- ");
            }
        });
}
songchenwen
  • 1,362
  • 9
  • 14
1

you can use handler to handleMessage by call ShowToast(context)

or you can call ShowToast like this ShowToast(getApplicationContext());

user5164480
  • 119
  • 7