user2562568
user2562568

Reputation: 367

android TextView setText not working

I know there are a lot of similar threads but I've gone through them and still can't figure out the problem. My program reaches the Handler but it always returns the catch exception "Message isn't handled."

I declared the TextView private TextView chatbox;

Under onCreate I have:

public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        setUpViews();
        setUpListener();
    }

where setUpViews() snippet looks like:

 private void setUpViews() {
    chatbox = (TextView) findViewById(R.id.chatbox);
    }

Handler:

public Handler mHandler = new Handler(Looper.getMainLooper()){
        @Override
        public void handleMessage(Message msg){
            try{
                chatbox.setText("Got it!");
            }catch(Exception e){
                Log.i("MYLOG", "Message was not handled.");
            }

        }
    };

Snippet in main.xml file:

<TextView
        android:id="@+id/chatbox"
        android:layout_width="200dp"
        android:layout_height="200dp"
        android:layout_centerHorizontal="true"
        android:layout_centerVertical="true"
        android:textAppearance="?android:attr/textAppearanceLarge" />

Upvotes: 20

Views: 54893

Answers (7)

jiar wang
jiar wang

Reputation: 360

when the textColor is the same as View's background color ,the text also sees invisible ,so you can define a textColor to solve it

Upvotes: 1

Hudson
Hudson

Reputation: 869

For future reference, I had multiple fragments, one for each tab in my ViewPager. One TextView had the same id in two fragments and that created a conflict. I didn't realize I had the same id so everything seemed fine, no error message, but the text simply didn't change. Therefore I'd recommend using unique names for the id.

Upvotes: 56

Ahmed Nabil
Ahmed Nabil

Reputation: 18986

In my case, I found there are a TextWatcher and a Filter linked to my Textview, which both contain a logic that prevents the updating of my Textview value.
So additionally to other solutions, you can check if your Textview is linked to any TextWatcher or Filter and then trace it.

Upvotes: 3

Ken
Ken

Reputation: 31161

You haven't given us much to go on.

You should look at the exception stack trace, instead of printing a message to the console: e.printStackTrace();

But from here, if I had to guess, it looks like you're either setting the TextView's text off the main thread, or - and which appears unlikely based on what you've posted - your TextView has not been set and you have a null pointer exception.

Upvotes: 2

user1615651
user1615651

Reputation:

just like this is ok!

private void setUpViews() {
    chatbox = (TextView) findViewById(R.id.chatbox);
    chatbox.setText("Got it!");
}

Upvotes: 0

Su  Zhenpeng
Su Zhenpeng

Reputation: 224

have you send a message? for example:

            Message message=new Message();
        message.what=1;
        mHandler.sendMessage(message);

Upvotes: -1

Sushil
Sushil

Reputation: 8488

I hope you handler is running in UI Thread. Also try doing this: Assign your string to a variable and use that variable as it requires charsequence.

String temp = "Got it!";
chatbox.setText(temp);

Upvotes: 4

Related Questions