无法在oncreate中看到吐司

时间:2014-03-02 16:27:14

标签: android multithreading

import android.widget.Toast;
public class Security extends MainActivity {

    Socket sock,sock1;
    @Override
    public void onCreate(Bundle savedInstanceState) {
        // TODO Auto-generated method stub
        super.onCreate(savedInstanceState);
        setContentView(R.layout.security);

        new Thread(new Runnable()
        {
            public void run()
            {
            try{
                sock=new Socket(ip,port1);
                BufferedReader br=new BufferedReader(new InputStreamReader(sock.getInputStream()));
                String a=br.readLine();
                Thread.sleep(2500);
                Toast.makeText(getApplicationContext(),"Please Enter the result of operation for no "+a,Toast.LENGTH_LONG).show();


            }
            catch(UnknownHostException e)
            {
                e.printStackTrace();
            }
            catch (IOException e) {
                 e.printStackTrace();
                }
            catch(InterruptedException e)
            {
                e.printStackTrace();
            }

            }

        }).start();

    }


}

安全性开始时无法查看吐司。我不知道为什么?就像它应该在onclicklistener中一样。?

2 个答案:

答案 0 :(得分:1)

您只能在主UI线程中显示祝酒词。

如果您需要在后台线程的UI线程上执行操作,请查看runOnUiThread()

答案 1 :(得分:1)

runOnUiThread(new Runnable() {
    public void run() {
        Toast.makeText(getApplicationContext(),"Please Enter the result of operation for no "+a,Toast.LENGTH_LONG).show();
    }
});