我得到一个错误的线程错误,所以我认为我需要从UI线程运行invalidate。通常是这样的:
public void run() {
runOnUiThread(new Runnable() {
}});
但是我需要命名这个可运行的从一个方法引用它。我如何将runonuthread纳入其中?
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);
}
您不需要命名它。如果你想发布自己,你可以使用this
关键字:
viewHandler.postDelayed(this, 1000);
mSession.write(dataReceived);
导致线程错误。updateView
不会引起任何问题。尝试将mSession.write
包装为可运行的,并在ui线程上调用它。
让它工作,我只需要在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);
}