发布消息在线程内使用toast和setText()

时间:2012-05-28 06:56:25

标签: android

我想在readline!= null时在线程内部用setText()方法发布一些消息,但是我发现了错误。我无法显示吐司并使用setText()方法。

public void run(){

    Looper.myLooper();
    Looper.prepare();

    try {           
        while(!((line = in.readLine()).equalsIgnoreCase("quit"))){              
            if(line.isEmpty()){
                System.out.println("there is no message");
            }else{                      

     Toast.makeText(this, "Message= " + line , Toast.LENGTH_SHORT).show();
                msgbox.setText(line);
            }               
        }           
    } catch (IOException e) {
        e.printStackTrace();
        System.out.println("Gagal mengirim ACK ke server");
    }

}

5 个答案:

答案 0 :(得分:2)

你不能在UI线程以外的任何线程中显示toast ..你可以为此使用处理程序或runOnUiThread ..

 activity.runOnUiThread(new Runnable() {
public void run() {
    Toast.makeText(youractivity.this, "Message= " + line , Toast.LENGTH_SHORT).show();
            msgbox.setText(line);
}
});

答案 1 :(得分:2)

你的错误是什么?

这段代码可以解决吗?

    try {       

            final StringBuffer strMessage = new StringBuffer();

        while(!((line = in.readLine()).equalsIgnoreCase("quit"))){              
            if(line.isEmpty()){
                System.out.println("there is no message");
            }else{                      

            strMessage.append(line)

            this.runOnUiThread(new Runnable() {
        public void run() {
            Toast.makeText(YourActivity.this, "Message= " + line , Toast.LENGTH_SHORT).show();
             msgbox.setText(strMessage.toString());
            //...
                    }
                });

            }               
        }           
    } catch (IOException e) {
        e.printStackTrace();
        System.out.println("Gagal mengirim ACK ke server");
    }

runOnUiThread(...)在UI线程上运行指定的操作。如果当前线程是UI线程,则立即执行该操作。如果当前线程不是UI线程,则该操作将发布到UI线程的事件队列中。

答案 2 :(得分:1)

您无法从非主线程更新UI。您已使用handlers and Threads

或者只是尝试一下,为这样的Activity创建一个obj,

Activity activityobj= yourActivity;并在run()中使用它,

public void run(){

Looper.myLooper();
Looper.prepare();

try {           
    while(!((line = in.readLine()).equalsIgnoreCase("quit"))){              
        if(line.isEmpty()){
            System.out.println("there is no message");
        }else{                      
     activityObj.runOnUiThread(new Runnable() {

        @Override
        public void run() {
Toast.makeText(this, "Message= " + line ,Toast.LENGTH_SHORT).show();
            msgbox.setText(line);

        }
    });


        }               
    }           
} catch (IOException e) {
    e.printStackTrace();
    System.out.println("Gagal mengirim ACK ke server");
}

}

答案 3 :(得分:0)

试试这个,

1)在班级中创建一个处理程序:

private Handler mHandler = new Handler() {

    public void handleMessage(android.os.Message msg) {

        Toast.makeText(this, "Message= " + line ,Toast.LENGTH_SHORT).show();
        msgbox.setText(line);
    }
};

2)修改您的代码,如下所示:

public void run(){

Looper.myLooper();
Looper.prepare();

try {           
    while(!((line = in.readLine()).equalsIgnoreCase("quit"))){              
        if(line.isEmpty()){
            System.out.println("there is no message");
        }else{                      
           mHandler.sendEmptyMessage(0);
        }               
    }           
} catch (IOException e) {
    e.printStackTrace();
    System.out.println("Gagal mengirim ACK ke server");
}

}

答案 4 :(得分:0)

这是

Handler handler = new Handler();    
public void run(){
Looper.myLooper();
Looper.prepare();
try {           
    while(!((line = in.readLine()).equalsIgnoreCase("quit"))){              
        if(line.isEmpty()){
            System.out.println("there is no message");
        }else{                      
  handler.post(new Runnable() {

                    @Override
                    public void run() {
                         Toast.makeText(this, "Message= " + line , Toast.LENGTH_SHORT).show();
            msgbox.setText(line);
                    }
                });


        }               
    }           
} catch (IOException e) {
    e.printStackTrace();
    System.out.println("Gagal mengirim ACK ke server");
}

}