我得到一個錯誤的線程錯誤,所以我想我需要從UI線程運行無效。通常我有這樣的:編輯代碼以在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);
}
你確定你說錯了嗎?如果你將它發佈到'viewHandler'並且在UI線程上創建'viewhandler',那麼它不應該拋出這樣的錯誤。 –
你能在這裏發佈錯誤信息嗎? –
這裏是錯誤:http://i.imgur.com/s4mTt.png – Paul