Update a (TextView) through another function

0

I'm trying to update the text of a TextView through a function, but I'm getting an error just after the line Log.i ("UPDATE", "- 1 -"); (line 12) The code should work perfectly when I called the update () function but for some reason the error.

public void atualizar(){
        Log.i("ATUALIZAR","-0-"+Usuario.retorneINFO());
        if(Usuario.retorneINFO() != null){
            JSONObject object = (JSONObject) new JSONTokener(Usuario.retorneINFO()).nextValue();
            String chat = object.getString("1");

            chat = chat.replaceAll("!hx01", " "); // troca todos os !hx01 por um espaço em branco
            chat = chat.replaceAll("!hx02", "'");
            chat = chat.replaceAll("!hx03", "\"");

            Log.i("ATUALIZAR","-1-");
            TextView msg = (TextView) findViewById(R.id.message);
            msg.setText(chat);  
            Log.i("ATUALIZAR","-2-");
            new Connect().execute("http://animesslife.engine001.com/chat/get_chat.php");
            Log.i("ATUALIZAR","-3-");

        }

    }

In short, I need to update the text of this TextView msg.setText (chat) by calling it through a function

  • I tried to put (View v) as parameter of function update () but did not work

Is it possible to do this or within a function called by .xml? Thanks

EDIT: This is my xml

  

<Button
    android:id="@+id/Enviar"
    style="?android:attr/buttonStyleSmall"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentBottom="true"
    android:layout_alignParentRight="true"
    android:onClick="Send"
    android:text="Enviar" />

<EditText
    android:id="@+id/AreaDeTexto"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentBottom="true"
    android:layout_alignParentLeft="true"
    android:layout_toLeftOf="@+id/Enviar"
    android:inputType="text" >

    <requestFocus />
</EditText>

<ScrollView

    android:id="@+id/scrollView1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_above="@+id/Enviar"
    android:layout_alignParentLeft="true"
    android:layout_alignParentRight="true"
    android:layout_alignParentTop="true" >

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical" >

        <TextView
            android:id="@+id/message"
            android:layout_width="match_parent"
            android:layout_height="wrap_content" />
    </LinearLayout>
</ScrollView>
     

    
asked by anonymous 22.05.2014 / 20:44

1 answer

1

Even though there is little information, I believe, from what I gather there, you are calling a UI inside a Thread and it can not connect directly to your interface, so it gives the error. Do the following, run another thread to connect to the interface:

  runOnUiThread(new Runnable() {

        @Override
        public void run() {
            TextView msg = (TextView) findViewById(R.id.message);
            msg.setText(chat);
        }
    });

This type of Thread is responsible for making this type of connection. It is part of Activity if the above form is not available try: getActivity().runOnUiThread...

    
22.05.2014 / 21:10