0

我收到了错误的线程错误,所以我想我需要从 UI 线程运行无效。通常我有类似的东西:

public void run() {

                runOnUiThread(new Runnable() {
}});

但是我需要命名这个 runnable 以从方法中引用它。我如何将 runOnUiThread 纳入其中?

Handler viewHandler = new Handler();
Runnable updateView = new Runnable() {
@Override

public void run() {

    mEmulatorView.invalidate();

    if (statusBool == true) {
        for (int i = 1; i < dataReceived.length() - 1; i++) {

            if (dataReceived.charAt(i) == '>') {

                Log.d(TAG, "found >");
                deviceStatus = 0;
            }
            if (dataReceived.charAt(i) == '#'
                    && dataReceived.charAt(i - 1) != ')') {

                Log.d(TAG, "found #");
                deviceStatus = 1;
            }
            if ((i + 1) <= (dataReceived.length())
                    && dataReceived.charAt(i) == ')'
                    && dataReceived.charAt(i + 1) == '#') {

                Log.d(TAG, "found config )#");
                deviceStatus = 2;
            }

        }
        statusBool = false;
        viewHandler.postDelayed(updateView, 1000);

    }
}
};

调用它:

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

        dataReceived = new String(data);
        ((MyBAIsWrapper) bis).renew(data);
        mSession.write(dataReceived);
        viewHandler.post(updateView);
}
4

2 回答 2

1

你不需要命名它。如果你想发布自己,你可以使用this关键字:

viewHandler.postDelayed(this, 1000);

更新

错误的线程错误由mSession.write(dataReceived);. updateView不会造成任何问题。尝试包装mSession.write为可运行并在 ui 线程上调用它。

于 2013-01-18T11:21:24.423 回答
1

得到它的工作,我只需要在 UI 线程中运行该写入行,即使我认为它是?!

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);
    }
于 2013-01-18T12:12:03.173 回答