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 hand开发者_运维知识库ler has to update the TextView with the message. But the TextView is null inside the handler. What to do/?. Please help
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:
- Pass this to your handler class
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.
精彩评论