Android : java.lang.IllegalStateException even updating from UI thread through broadcast

on Tuesday, October 28, 2014


I wanna understand why im getting this error and my app crashes, cause im using notifyDataSetChanged correctly from main ui thread through broadcast.


10-24 14:13:36.563: E/AndroidRuntime(24830): java.lang.IllegalStateException: The content of the adapter has changed but ListView did not receive a notification. Make sure the content of your adapter is not modified from a background thread, but only from the UI thread. Make sure your adapter calls notifyDataSetChanged() when its content changes. [in ListView(2131099732, class android.widget.ListView) with Adapter(class com.example.irclient2.adapter.ConversaAdapter)]


When i receive a message (and i need to update the ui about it), i send a broadcast for it after adding the message to the data set;



public void receiveChannelUserMessage(User user, String msg,
CategoriaMSG category) {

// creates the message
Mensagem mensagem = new Mensagem(user.createSnapshot(),
Colors.removeFormattingAndColors(msg), category, nickcolor);

// add the message
canalConversa.addMessage(mensagem);

// inform UI from broadcast
Intent it = new Intent(ChatFragment.ACTION_CONVERSASETCHANGED);
it.addCategory(MyService.CANAL);
LocalBroadcastManager.getInstance(MyService.this).sendBroadcast(it);

}


This is the receiver in the fragment where the listview is in:



private BroadcastReceiver BR_notifyData = new BroadcastReceiver() {

@Override
public void onReceive(Context context, Intent intent) {
if (listview != null) {
((ConversaAdapter) listview.getAdapter())
.notifyDataSetChanged();
}
}
};


The receiver is registered at onResume() and unregistered at onPause(); What shoud i do about all of it? Thanks in advance.


0 comments:

Post a Comment