Android eclipse中的Toast错误

Android eclipse中的Toast错误,android,eclipse,Android,Eclipse,当我祝酒时,应用程序崩溃了,但我不明白为什么。代码如下: @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); mvc = (Button) findViewById(R.id.button1); mvc.setOnClickListener(new View.On

当我祝酒时,应用程序崩溃了,但我不明白为什么。代码如下:

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);
    mvc = (Button) findViewById(R.id.button1);

    mvc.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View v) {
            // TODO Auto-generated method stub
            Toast.makeText(getApplicationContext(),"START!" ,Toast.LENGTH_SHORT).show();
            new Thread(new Runnable() {
                public void run() {
                    long startTime = System.currentTimeMillis();
                    while( startTime + 5000 >  System.currentTimeMillis())
                    {
            //      }
            //      while (progressStatus < 1000) {

                        if (k > progressStatus){
                            progressStatus = k;
                        }
                        else {
                            progressStatus = progressStatus;
                        }
                        // Update the progress bar and display the current value in the text view
                        handler.post(new Runnable() {
                            public void run() {
                                progressBar.setProgress(progressStatus);

                            }
                        });
                        try {
                            // Sleep for 10 milliseconds. Just to display the progress slowly
                            Thread.sleep(10);
                        } catch (InterruptedException e) {
                            e.printStackTrace();
                        }
                    }
                    Toast mioToast = Toast.makeText(BluetoothChat.this, 
                            "STOP!", 
                            Toast.LENGTH_LONG);

                            mioToast.show();

                }

            }).start();

        }

    });
@覆盖
创建时的公共void(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
mvc=(按钮)findViewById(R.id.button1);
mvc.setOnClickListener(新视图.OnClickListener(){
@凌驾
公共void onClick(视图v){
//TODO自动生成的方法存根
Toast.makeText(getApplicationContext(),“开始!”,Toast.LENGTH\u SHORT.show();
新线程(newrunnable()){
公开募捐{
long startTime=System.currentTimeMillis();
而(startTime+5000>System.currentTimeMillis())
{
//      }
//而(进度状态<1000){
如果(k>progressStatus){
progressStatus=k;
}
否则{
progressStatus=progressStatus;
}
//更新进度条并在文本视图中显示当前值
handler.post(新的Runnable(){
公开募捐{
progressBar.setProgress(progressStatus);
}
});
试一试{
//睡眠10毫秒。只是为了缓慢地显示进度
睡眠(10);
}捕捉(中断异常e){
e、 printStackTrace();
}
}
Toast mioToast=Toast.makeText(BluetoothChat.this,
“停!”,
吐司长度(长);
miotoots.show();
}
}).start();
}
});
第一个吐司完美地执行了它,但在第二个吐司中,如果有“STOP”这个词,应用程序就会崩溃。这是怎么回事?
谢谢。

尝试将“getApplicationContext()”替换为“this”或“your_class_name”。这“

您试图在后台线程中显示一个
Toast
。只能从UI线程修改UI,这会使应用程序崩溃

改用这个:

handler.post(new Runnable() {
    public void run() {
        Toast.makeText(BluetoothChat.this, 
                        "STOP!", 
                        Toast.LENGTH_LONG).show();
    }
};
这将在UI线程上运行,因此不会使应用程序崩溃