本文介绍了Android的UI线程问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我连接到终端仿真器在Android中使用库,这个连接串行设备(交换机),并表明我发送/接收数据。我在经由终端下方的文本框的连接或通过在终端本身打字和击球键盘在这两种情况下对输入发送数据。我还可以通过pressing按钮发送命令。

I am connecting to a terminal emulator using a library in android, this connects to a serial device (a switch) and shows me sent/received data. I send data over the connection via a text box below the terminal or by typing in the terminal itself and hitting enter on the keyboard in both cases. I can also send commands by pressing buttons.

我的问题是每当终端本身被突出显示的用户界面并不更新,因为在与来自串行设备接收到的按钮,或数据发送的命令。我可以输入终端本身和人物上来。如果选择了EDITTEXT全部更新罚款。所以,如果我选择在终端和发送一个命令,它只会一次我选择EDITTEXT显示在屏幕上。很多的时间后,我选择它仍然没有更新,直到我再次preSS终端的EDITTEXT。很奇怪。所以我的无效不工作呢?抑或是我使用,并不能真正理解这个notifyUpdate方法?

My problem is whenever the terminal itself is highlighted the UI does not update, as in the commands sent with the buttons, or data received from the serial device. I can type in the terminal itself and the characters come up. If the editText is selected everything updates fine. So if I select the terminal and send a command, it will only be displayed to the screen once I select the editText. A lot of the time after I select the editText it still does not update until I press the terminal again. Very strange. So my invalidate is not working well? Or is it this notifyUpdate method that I am using and don't really understand?

我目前正在做的是在​​接收数据时调用一个方法,这个方法调用句柄所更新屏幕每1000毫秒。然而,这并不工作,选择了终端的时候,这是为什么?我得到的错误:只有创建一个视图层次可以触摸其观点原来的线程

What I currently do is call a method when data is received, this method calls a handler which updates the screen every 1000ms. This however does not work when the terminal is selected, why is this? I am getting the error: "Only the original thread that created a view hierarchy can touch its views."

截图:

    //terminal view
    EmulatorView view = (EmulatorView) findViewById(R.id.emulatorView);
    mEmulatorView = view;

    public void onDataReceived(int id, byte[] data)
    {
    dataReceived = new String(data);
    ((MyBAIsWrapper) bis).renew(data);
    mSession.write(dataReceived);
    mSession.notifyUpdate();
    viewHandler.post(updateView);
    }

    Handler viewHandler = new Handler();
    Runnable updateView = new Runnable() {
    @Override
    public void run() {
    mEmulatorView.invalidate();
    viewHandler.postDelayed(updateView, 1000);

        }
    }
};

这是我使用的库相关code:

Relevant code from library I'm using:

 [code]protected void notifyUpdate() {
        if (mNotify != null) {
            mNotify.onUpdate();
        }
    }[/code]

/**
 * Generic callback to be invoked to notify of updates.
 */
public interface UpdateCallback {
    /**
     * Callback function to be invoked when an update happens.
     */
    void onUpdate();
}

这是方法notifyUpdate()的说明 - 类jackpal.androidterm.emulatorview.TermSession方法,通知setUpdateCallback注册UpdateCallback在屏幕改变了

This is the description of the method notifyUpdate() - Method in class jackpal.androidterm.emulatorview.TermSession Notify the UpdateCallback registered by setUpdateCallback that the screen has changed.

我想这可能是mSession.notifyUpdate();在我的code,但显然如果我评论说出来出现同样的错误,该行似乎无论如何无所事事我。我不知道哪里出错解决它,使我的画面总是被更新,不管我在哪里pressing。

I thought it might be mSession.notifyUpdate(); in my code, but apparently if I comment that out the same error occurs, that line seems to be doing nothing for me anyway. I'm not sure where the error is to fix it so that my screen is always being updated no matter where I am pressing.

推荐答案

得到它的工作,我只是能够运行在一个UI线程写线,甚至认为我虽然是?!

Got it working, I just had to run that write line in a UI thread, even thought I though it was?!

public void onDataReceived(int id, byte[] data) {

        dataReceived = new String(data);
        ((MyBAIsWrapper) bis).renew(data);

        runOnUiThread(new Runnable(){
            @Override
            public void run() {
                mSession.write(dataReceived);
            }});

        viewHandler.post(updateView);
    }

这篇关于Android的UI线程问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-01 18:19