English 中文(简体)
将信件张贴到信件时使用举举举和设置为Text () 内线
原标题:Posting message use toast and setText() inside thread
  • 时间:2012-05-28 06:56:24
  •  标签:
  • android

当读行! = 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");
    }

}
最佳回答

你犯了什么错?

这个密码能解决吗?

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

runOnUitread (...) 运行 UI 线索上的指定动作。 如果当前线索是 UI 线索, 则立即执行操作。 如果当前线索不是 UI 线索, 则该动作被张贴到 UI 线索的事件队列中 。

问题回答

您无法在 UI 线索以外的任何线索中显示土司 。 您可以为此使用一个处理器, 或者为此运行 OnUitread 。

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

您无法用非主线更新您的 UI 。 您要么使用 < a href=" http://www.vogella. com/ articles/ AndroidPerformatance/article. html" rel = “ nofollow” > handlers and Threads 。

或者只是尝试一下,为这种活动创建一个折叠式的活动,

activity activityobj= 您的活动; 并在运行中使用 (),

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

}

试试这个

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

时 时

就是这里了

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

}




相关问题
Android - ListView fling gesture triggers context menu

I m relatively new to Android development. I m developing an app with a ListView. I ve followed the info in #1338475 and have my app recognizing the fling gesture, but after the gesture is complete, ...

AsyncTask and error handling on Android

I m converting my code from using Handler to AsyncTask. The latter is great at what it does - asynchronous updates and handling of results in the main UI thread. What s unclear to me is how to handle ...

Android intent filter for a particular file extension?

I want to be able to download a file with a particular extension from the net, and have it passed to my application to deal with it, but I haven t been able to figure out the intent filter. The ...

Android & Web: What is the equivalent style for the web?

I am quite impressed by the workflow I follow when developing Android applications: Define a layout in an xml file and then write all the code in a code-behind style. Is there an equivalent style for ...

TiledLayer equivalent in Android [duplicate]

To draw landscapes, backgrounds with patterns etc, we used TiledLayer in J2ME. Is there an android counterpart for that. Does android provide an option to set such tiled patterns in the layout XML?

Using Repo with Msysgit

When following the Android Open Source Project instructions on installing repo for use with Git, after running the repo init command, I run into this error: /c/Users/Andrew Rabon/bin/repo: line ...

Android "single top" launch mode and onNewIntent method

I read in the Android documentation that by setting my Activity s launchMode property to singleTop OR by adding the FLAG_ACTIVITY_SINGLE_TOP flag to my Intent, that calling startActivity(intent) would ...

From Web Development to Android Development

I have pretty good skills in PHP , Mysql and Javascript for a junior developer. If I wanted to try my hand as Android Development do you think I might find it tough ? Also what new languages would I ...

热门标签