Reputation: 6687
I have a TextView in the xml and from the OnCreate method I call its SetContentView. I have also written a handler to get message from other classes. Upon receiving the messages, the handler has to update the TextView with the message. But the TextView is null inside the handler. What to do/?. Please help
Upvotes: 2
Views: 513
Reputation: 2111
How are you getting a reference to the TextView? Something like this?
final TextView myTextView = (TextView) findViewById(R.id.my_text_view);
Then depending on what you need, you can do one of many things, including:
Create a "listener" interface that has an onUpdate(String msg) method and store an instance of this in the handler, then implement
public void onUpdate(String msg) {
myTextView.setText(msg);
}
in your Activity class and call listener.onUpdate(msg) from your handler class.
I'd probably prefer doing something like #2 because it keeps the UI-specific code out of the handler which would make that class easier to test.
Upvotes: 1