0

当 readline!= null 时,我想在线程内使用 setText() 方法发布一些消息,但我发现错误。我无法显示 toast 并使用 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");
    }

}
4

5 回答 5

2

除了 UI 线程之外,您不能在任何线程中显示 toast 。您可以为此使用处理程序或 runOnUiThread 。

 activity.runOnUiThread(new Runnable() {
public void run() {
    Toast.makeText(youractivity.this, "Message= " + line , Toast.LENGTH_SHORT).show();
            msgbox.setText(line);
}
});
于 2012-05-28T07:01:52.877 回答
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 线程的事件队列中。

于 2012-05-28T07:02:52.457 回答
1

您不能从非主线程更新您的 UI。您要么使用处理程序和线程

或者干脆试试他的,像这样为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");
}

}
于 2012-05-28T07:01:11.137 回答
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");
}

}

于 2012-05-28T07:08:08.497 回答
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");
}

}
于 2012-05-28T07:15:13.513 回答