i'm writinig some small chat app, and i'm using AndroidAsync to get WebSocket client functionality in my application. So, the trouble is whenever i try to modify my listView from WebSocket.StringCallback().onStringAvailable(String)
it causes an exception:
android.view.ViewRoot$CalledFromWrongThreadException: Only the original thread that created a view hierarchy can touch its views.
at android.view.ViewRoot.checkThread(ViewRoot.java:2932)
at android.view.ViewRoot.focusableViewAvailable(ViewRoot.java:1712)
at android.view.ViewGroup.focusableViewAvailable(ViewGroup.java:452)
at android.view.ViewGroup.focusableViewAvailable(ViewGroup.java:452)
at android.view.ViewGroup.focusableViewAvailable(ViewGroup.java:452)
at android.view.ViewGroup.focusableViewAvailable(ViewGroup.java:452)
at android.view.ViewGroup.focusableViewAvailable(ViewGroup.java:452)
at android.view.View.setFlags(View.java:4614)
at android.view.View.setFocusableInTouchMode(View.java:3190)
at android.widget.AdapterView.checkFocus(AdapterView.java:694)
at android.widget.AdapterView$AdapterDataSetObserver.onChanged(AdapterView.java:789)
at android.database.DataSetObservable.notifyChanged(DataSetObservable.java:31)
at android.widget.BaseAdapter.notifyDataSetChanged(BaseAdapter.java:50)
at android.widget.ArrayAdapter.notifyDataSetChanged(ArrayAdapter.java:247)
at persilabtest.zulfigarov.com.chatapp.ChatActivity$1$1.onStringAvailable(ChatActivity.java:76)
at com.koushikdutta.async.http.WebSocketImpl$1.onMessage(WebSocketImpl.java:88)
at com.koushikdutta.async.http.HybiParser.emitFrame(HybiParser.java:420)
at com.koushikdutta.async.http.HybiParser.access$800(HybiParser.java:46)
at com.koushikdutta.async.http.HybiParser$5.onDataAvailable(HybiParser.java:197)
at com.koushikdutta.async.DataEmitterReader.handlePendingData(DataEmitterReader.java:24)
at com.koushikdutta.async.DataEmitterReader.onDataAvailable(DataEmitterReader.java:41)
at com.koushikdutta.async.Util.emitAllData(Util.java:22)
at com.koushikdutta.async.AsyncNetworkSocket.onReadable(AsyncNetworkSocket.java:146)
at com.koushikdutta.async.AsyncServer.runLoop(AsyncServer.java:788)
at com.koushikdutta.async.AsyncServer.run(AsyncServer.java:626)
at com.koushikdutta.async.AsyncServer.access$700(AsyncServer.java:41)
at com.koushikdutta.async.AsyncServer$13.run(AsyncServer.java:568)
I've already understood that I can't modify a view from non-UI threads. But i don't know how else i can update them in this situation. Here's my ChatActivity code:
public class ChatActivity extends ActionBarActivity
{
private static final String WS_ADDRESS = "ws://192.168.0.106:8084/TestChatServer/chat";
@InjectView(R.id.btnSend)
Button btnSend;
@InjectView(R.id.lvChat)
ListView lvChat;
@InjectView(R.id.etMsg)
EditText etMsg;
List<String> mMessages = new ArrayList<String>();
ArrayAdapter<String> adapter;
@Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_chat);
ButterKnife.inject(this);
adapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, mMessages);
lvChat.setAdapter(adapter);
AsyncHttpClient.getDefaultInstance()
.websocket(WS_ADDRESS, null, new AsyncHttpClient.WebSocketConnectCallback()
{
@Override
public void onCompleted(Exception ex, WebSocket webSocket)
{
if (ex != null)
{
ex.printStackTrace();
return;
}
Log.d("myLogs", "OK");
webSocket.setStringCallback(new WebSocket.StringCallback()
{
@Override
public void onStringAvailable(String s)
{
String newStr = s.replaceAll("(#+\\S+)", "<font color='#EE0000'>$1</font>");
Log.d("myLogs", s + "\n" + newStr);
mMessages.add(newStr);
//This line causes the exception
adapter.notifyDataSetChanged();
}
});
}
});
}
}
So, any ideas how can i get my listView updated every time the new message is delivered?
You will want to make sure that you are updating the views from the UI thread. You can do this by using the Activity.runOnUiThread() like this:
The reason for this error
Only the original thread that created a view hierarchy can touch its views
is because it detected that another thread (besides the thread that created the views) is trying to touch the views. It would cause threading issues if more than one resource was trying to make changes to something at the same time.