我想在Internet连接可用但出错时显示Toast
我的密码

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 <-- ");

    }

}

这是日志中的错误
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

最佳答案

吐司应该在ui线程上,
替换,

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 <-- ");

08-04 04:11