我收到错误的线程错误,所以我想我需要从UI线程运行invalidate。通常我有类似的东西:
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);
}
答案 0 :(得分:1)
您无需为其命名。如果您想发布自己,可以使用this
关键字:
viewHandler.postDelayed(this, 1000);
<强>更新强>
由mSession.write(dataReceived);
引起的错误的线程错误。 updateView
不会导致任何问题。尝试将mSession.write
包装为runnable并在ui线程上调用它。
答案 1 :(得分: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);
}