Update one (Textview) through another function

Asked

Viewed 1,254 times

0

I’m trying to update the text of a Textview through a function, but I’m getting an error right after the line Log. i("UPDATE","-1-"); (line 12) The code should work perfectly when I call the function upgrade() but for some reason of 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-");

        }

    }

Summarizing I need to update the text of this Textview msg.setText(chat) calling it through a function

  • I tried Put (View v) as parameter of the update() function but it did not work

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

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>

  • Check if the name of the Textview id is message... makes a Textview log msg, share ai tbm the layout where Textview is... this will help a lot to find the solution. *Do the debug and go from point to point to see, always post the msg of the logcat that there is potato.

  • The name is message yes, I copied the code from another part that was working cool... I have no idea why this error :(, Ready put the layout xml

  • Go to Window -> Showview -> Other -> Logcat. This window will show you where your application’s error is, as soon as you run it. Glue there for us.

1 answer

1


Even though it is little information, I believe, from what I deduce there, you are calling a UI inside a Thread and it cannot connect directly to its interface, so it gives the error. Do the following, run another thread to connect 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 the Activity if the form above is not available try: getActivity().runOnUiThread...

Browser other questions tagged

You are not signed in. Login or sign up in order to post.