Toast 未显示并出错

Toast not being displayed and making error

我想在 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.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

根据错误,您正在从工作线程调用 toast,但您应该从处理 UI 线程的函数调用它。 在这里检查同样的问题: Can't create handler inside thread that has not called Looper.prepare()

Toast 应该在 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 <-- ");

您只能在 UI 话题中显示 Toast。不确定你为什么要在那里创建一个新线程。

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

您可以使用处理程序通过调用 ShowToast(context) 来处理消息

或者您可以像这样调用 ShowToast ShowToast(getApplicationContext());