I have a class "MessageParser" which extends
AbstractParser
and a method "onMessagePacketReceived" which calls a method:
conversation.add(message);
add is a method of entity "Conversation" which extends
AbstractEntity
and adds messages received like below:
public void add(Message message) {
message.setConversation(this);
synchronized (this.messages) {
this.messages.add(message);
}
}
What I want to do is to display an alert dialog which has an input like below:
AlertDialog.Builder builder = new AlertDialog.Builder(context);
builder.setTitle(R.string.input_pass);
View dialogView = getLayoutInflater().inflate(R.layout.set_password_dialog, null);
builder.setView(dialogView);
builder.setNegativeButton(R.string.cancel, null);
builder.setPositiveButton(R.string.next, null);
final AlertDialog dialog = builder.create();
dialog.show();
dialog.getButton(AlertDialog.BUTTON_POSITIVE).setOnClickListener(
new View.OnClickListener() {
@Override
public void onClick(final View v) {
EditText edit = (EditText) dialog.findViewById(R.id.account_password);
String pass = edit.getText().toString();
if (pass.equals(accountList.get(0).getPassword())) {
conversation.add(message);
dialog.dismiss();
} else {
edit.setError(getString(R.string.incorrect_pass));
}
}
});
which replaces conversation.add(message). I am having troubles with this. It displays
Can't create handler inside thread that has not called Looper.prepare()
I have seen this answer but doesn't help me.
The problem is that you are trying the create and show the dialog within a non-UI thread (that's what the error says, as usually only the main UI Thread calls
Looper.prepare()
). To fix it, use this code:EDIT:
In the code above, the
activity
variable should be passed by an activity at the creation.If that doesn't work, please post the whole log cat.