Issue
I make a simple game with socket.io. It well execute but, when change UI view like textview it call Thread error. So, I decide to use AsyncTask when socket.on function. But before socket event come, onPostExecute is already done. How I can do this?
This is my code
private class JoinAsync extends AsyncTask<Void,Void,Boolean> {
Boolean isDone = false;
JSONObject object;
@Override
protected Boolean doInBackground(Void... voids) {
socket.on("join", new Emitter.Listener() {
@Override
public void call(Object... args) {
object = (JSONObject)args[0];
isDone = true;
onPostExecute(true);
}
});
return false;
}
@Override
protected void onPostExecute(Boolean done) {
if (done) {
btnPlay.setEnabled(true);
turnChanged();
try {
playertwoinfo.setText(object.get("nickname").toString() + "\n" + object.get("win").toString() + "승 " + object.get("lose").toString() + "패");
} catch (JSONException e) {
e.printStackTrace();
}
}
}
}
Solution
- Don't invoke methods of AsyncTask manually, they will be
invoked.Remove
onPostExecute(true);
and it will work. - doInBackground() is invoked from a background thread which can not edit properties of views. Views can be changed from UI/main thread.
Answered By - Thracian
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.