问题
I am working on an Android project in which I am working on chat functionality. In this, whenever a new message is received, I have to touch(literally) the screen, then only the new message can be seen. Also, for the first time, I get the below mentioned error. How can I add the object to already existing list. Any help would be nice. Thank you.
I get the latest message in ChatListener
, which then is forwarded to a method which willtry to add it to the adapter.
Entire stacktrace :
03-31 14:30:15.578 21164-21484/mycompany.app W/System.err: Handhskare complete[HttpClient@260236492-4456] INFO org.cometd.bayeux.client.ClientSession - Exception while invoking listener mycompany.app.Activity.ChatMessagesActivity$ChatListener@9b922a9
03-31 14:30:15.578 21164-21484/mycompany.app W/System.err: android.view.ViewRootImpl$CalledFromWrongThreadException: Only the original thread that created a view hierarchy can touch its views.
03-31 14:30:15.582 21164-21484/mycompany.app W/System.err: at android.view.ViewRootImpl.checkThread(ViewRootImpl.java:6556)
03-31 14:30:15.582 21164-21484/mycompany.app W/System.err: at android.view.ViewRootImpl.requestLayout(ViewRootImpl.java:907)
03-31 14:30:15.582 21164-21484/mycompany.app W/System.err: at android.view.View.requestLayout(View.java:18722)
03-31 14:30:15.582 21164-21484/mycompany.app W/System.err: at android.view.View.requestLayout(View.java:18722)
03-31 14:30:15.582 21164-21484/mycompany.app W/System.err: at android.view.View.requestLayout(View.java:18722)
03-31 14:30:15.582 21164-21484/mycompany.app W/System.err: at android.view.View.requestLayout(View.java:18722)
03-31 14:30:15.582 21164-21484/mycompany.app W/System.err: at android.view.View.requestLayout(View.java:18722)
03-31 14:30:15.582 21164-21484/mycompany.app W/System.err: at android.view.View.requestLayout(View.java:18722)
03-31 14:30:15.582 21164-21484/mycompany.app W/System.err: at android.support.v4.widget.DrawerLayout.requestLayout(DrawerLayout.java:979)
03-31 14:30:15.582 21164-21484/mycompany.app W/System.err: at android.view.View.requestLayout(View.java:18722)
03-31 14:30:15.582 21164-21484/mycompany.app W/System.err: at android.widget.RelativeLayout.requestLayout(RelativeLayout.java:360)
03-31 14:30:15.582 21164-21484/mycompany.app W/System.err: at android.view.View.requestLayout(View.java:18722)
03-31 14:30:15.582 21164-21484/mycompany.app W/System.err: at android.widget.AbsListView.requestLayout(AbsListView.java:1975)
03-31 14:30:15.583 21164-21484/mycompany.app W/System.err: at android.widget.AdapterView$AdapterDataSetObserver.onChanged(AdapterView.java:833)
03-31 14:30:15.583 21164-21484/mycompany.app W/System.err: at android.widget.AbsListView$AdapterDataSetObserver.onChanged(AbsListView.java:6179)
03-31 14:30:15.583 21164-21484/mycompany.app W/System.err: at android.database.DataSetObservable.notifyChanged(DataSetObservable.java:37)
03-31 14:30:15.583 21164-21484/mycompany.app W/System.err: at android.widget.BaseAdapter.notifyDataSetChanged(BaseAdapter.java:50)
03-31 14:30:15.583 21164-21484/mycompany.app W/System.err: at mycompany.app.Activity.ChatMessagesActivity.recieveUpdatedMessage(ChatMessagesActivity.java:265)
03-31 14:30:15.583 21164-21484/mycompany.app W/System.err: at mycompany.app.Activity.ChatMessagesActivity$ChatListener.onMessage(ChatMessagesActivity.java:473)
03-31 14:30:15.583 21164-21484/mycompany.app W/System.err: at org.cometd.common.AbstractClientSession$AbstractSessionChannel.notifyOnMessage(AbstractClientSession.java:501)
03-31 14:30:15.583 21164-21484/mycompany.app W/System.err: at org.cometd.common.AbstractClientSession$AbstractSessionChannel.notifyMessageListeners(AbstractClientSession.java:491)
03-31 14:30:15.583 21164-21484/mycompany.app W/System.err: at org.cometd.common.AbstractClientSession.notifyListeners(AbstractClientSession.java:248)
03-31 14:30:15.583 21164-21484/mycompany.app W/System.err: at org.cometd.client.BayeuxClient.notifyListeners(BayeuxClient.java:1001)
03-31 14:30:15.583 21164-21484/mycompany.app W/System.err: at org.cometd.common.AbstractClientSession.receive(AbstractClientSession.java:241)
03-31 14:30:15.583 21164-21484/mycompany.app W/System.err: at org.cometd.client.BayeuxClient.processMessage(BayeuxClient.java:787)
03-31 14:30:15.583 21164-21484/mycompany.app W/System.err: at org.cometd.client.BayeuxClient$PublishTransportListener.processMessage(BayeuxClient.java:1193)
03-31 14:30:15.583 21164-21484/mycompany.app W/System.err: at org.cometd.client.BayeuxClient$ConnectTransportListener.processMessage(BayeuxClient.java:1260)
03-31 14:30:15.583 21164-21484/mycompany.app W/System.err: at org.cometd.client.BayeuxClient$PublishTransportListener.onMessages(BayeuxClient.java:1185)
03-31 14:30:15.583 21164-21484/mycompany.app W/System.err: at org.cometd.client.transport.LongPollingTransport$2.onComplete(LongPollingTransport.java:254)
03-31 14:30:15.584 21164-21484/mycompany.app W/System.err: at org.eclipse.jetty.client.ResponseNotifier.notifyComplete(ResponseNotifier.java:193)
03-31 14:30:15.584 21164-21484/mycompany.app W/System.err: at org.eclipse.jetty.client.ResponseNotifier.notifyComplete(ResponseNotifier.java:185)
03-31 14:30:15.584 21164-21484/mycompany.app W/System.err: at org.eclipse.jetty.client.HttpReceiver.terminateResponse(HttpReceiver.java:446)
03-31 14:30:15.584 21164-21484/mycompany.app W/System.err: at org.eclipse.jetty.client.HttpReceiver.responseSuccess(HttpReceiver.java:393)
03-31 14:30:15.584 21164-21484/mycompany.app W/System.err: at org.eclipse.jetty.client.http.HttpReceiverOverHTTP.messageComplete(HttpReceiverOverHTTP.java:265)
03-31 14:30:15.584 21164-21484/mycompany.app W/System.err: at org.eclipse.jetty.http.HttpParser.parseContent(HttpParser.java:1430)
03-31 14:30:15.584 21164-21484/mycompany.app W/System.err: at org.eclipse.jetty.http.HttpParser.parseNext(HttpParser.java:1272)
03-31 14:30:15.584 21164-21484/mycompany.app W/System.err: at org.eclipse.jetty.client.http.HttpReceiverOverHTTP.parse(HttpReceiverOverHTTP.java:156)
03-31 14:30:15.584 21164-21484/mycompany.app W/System.err: at org.eclipse.jetty.client.http.HttpReceiverOverHTTP.process(HttpReceiverOverHTTP.java:117)
03-31 14:30:15.584 21164-21484/mycompany.app W/System.err: at org.eclipse.jetty.client.http.HttpReceiverOverHTTP.receive(HttpReceiverOverHTTP.java:69)
03-31 14:30:15.584 21164-21484/mycompany.app W/System.err: at org.eclipse.jetty.client.http.HttpChannelOverHTTP.receive(HttpChannelOverHTTP.java:89)
03-31 14:30:15.584 21164-21484/mycompany.app W/System.err: at org.eclipse.jetty.client.http.HttpConnectionOverHTTP.onFillable(HttpConnectionOverHTTP.java:108)
03-31 14:30:15.584 21164-21484/mycompany.app W/System.err: at org.eclipse.jetty.io.AbstractConnection$2.run(AbstractConnection.java:540)
03-31 14:30:15.584 21164-21484/mycompany.app W/System.err: at org.eclipse.jetty.util.thread.QueuedThreadPool.runJob(QueuedThreadPool.java:635)
03-31 14:30:15.584 21164-21484/mycompany.app W/System.err: at org.eclipse.jetty.util.thread.QueuedThreadPool$3.run(QueuedThreadPool.java:555)
03-31 14:30:15.584 21164-21484/mycompany.app W/System.err: at java.lang.Thread.run(Thread.java:818)
Code :
public class ChatMessagesActivity extends ApplicationDrawerLoader {
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_chat_messages);
sendMessageButton = (Button) findViewById(R.id.sendMessageButton);
typeMessageField = (EditText) findViewById(R.id.sendMessageTextField);
typeMessageField.setHint("Type message here...");
sendMessageButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Map<String, Object> outputData = new HashMap<>();
outputData.put("text", typeMessageField.getText().toString());
outputData.put("timestamp", new Timestamp(System.currentTimeMillis()));
outputData.put("type", false);
outputData.put("name",StaticRestTemplate.firstName);
consoleChatClient.bayeuxClient.getChannel("/service/person/" + String.valueOf(conversationId)).publish(outputData);
}
public void recieveUpdatedMessage(String channelName, Map<String, Object> input) {
HashMap<String, String> insertMap = new HashMap<>();
insertMap.put(chatText, ((Map) input.get("data")).get("text").toString());
insertMap.put(firstName, ((Map) input.get("data")).get("firstname").toString());
chatMessagesAdapter.add(insertMap);
chatMessagesAdapter.notifyDataSetChanged();
chatList.scrollTo(0, chatList.getHeight());
}
public class getGroupChatsForUser extends AsyncTask<Void, Void, ResponseEntity<RestChatMessages[]>> {
ChatMessagesActivity chatMessagesActivity = null;
getGroupChatsForUser(ChatMessagesActivity chatMessagesActivity) {
this.chatMessagesActivity = chatMessagesActivity;
}
@Override
protected void onPostExecute(ResponseEntity<RestChatMessages[]> responseEntity) {
super.onPostExecute(responseEntity);
chatList = (ListView) findViewById(R.id.chatList);
chatMessagesAdapter = new ChatMessagesAdapter(chatMessagesActivity, chatMessagesHashMapList);
chatList.setAdapter(chatMessagesAdapter);
chatList.scrollTo(0, chatList.getHeight());
}
}
public class ChatListener implements ClientSessionChannel.MessageListener {
public void onMessage(ClientSessionChannel channel, Message message) {
try {
JSONObject jsonObject = new JSONObject(message).getJSONObject("data");
String result = jsonObject.getString("type");
if (result.equals("false")) {
recieveUpdatedMessage(channel.toString(), message);
}
} catch (JSONException e) {
e.printStackTrace();
}
}
}
}
回答1:
It will be much easier if you would paste all stacktrace, but I am assuming that you are changing views not from main thread.
Use this to when calling methods that are doing something with views:
activity.runOnUiThread(new Runnable() {
public void run() {
methodThatIsDoingViewStuff();
}
});
来源:https://stackoverflow.com/questions/36332040/android-object-cannot-be-added-to-an-existing-list-as-its-not-original-thread