Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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" />
like image 290
user2562568 Avatar asked Aug 21 '13 00:08

user2562568


1 Answers

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.

like image 186
Hudson Avatar answered Sep 19 '22 18:09

Hudson