Chain Cross
Chain Cross

Reputation: 351

TextView setText() does nothing

I do not understand why setText does nothing to the TextView, and I do not seem to be getting any error at all.

One reason why i'm trying to set the text is I'm trying to see if I successfully made the connection with the server.

Here is my code :

public class MainActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        final EditText textField = findViewById(R.id.textField);
        final Button submitBtn = findViewById(R.id.submitBtn);
        final TextView debugLbl = findViewById(R.id.debugLbl);
        final TextView debug2Lbl = findViewById(R.id.debug2);

        submitBtn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                final String text = textField.getText().toString();
                textField.setText("");
                Thread thread = new Thread(new Runnable() {

                    @Override
                    public void run() {
                        try  {
                            try {
                                Socket socket = new Socket("10.0.2.2", 9723);
                                OutputStream os = socket.getOutputStream();
                                OutputStreamWriter osw = new OutputStreamWriter(os);
                                BufferedWriter bw = new BufferedWriter(osw);
                                bw.write(text);
                                bw.flush();
                                debug2Lbl.setText("Successfully sent message : "+ text + " to server");
                                socket.close();
                            } catch (Exception e) {
                                e.printStackTrace();
                                debugLbl.setText("error");
                            }
                        } catch (Exception e) {
                            e.printStackTrace();
                        }
                    }
                });
                thread.start();
            }
        });
    }
}

Upvotes: 2

Views: 288

Answers (4)

Haj Ali
Haj Ali

Reputation: 93

do this:

runOnUiThread(new Runnable() {
                            @Override
                            public void run() {
                                debug2Lbl.setText("yourtext");
                            }
                        });

Upvotes: 0

ehsan mohajeri
ehsan mohajeri

Reputation: 90

This is because of you're not setting text at ui thread.it means your started thread cannot do some ui commands like setText ,notifyDataSetChanged....

there are several solutions:

1-copy this code instead of your debug2Lbl.setText("Successfully sent message : "+ text + " to server");

Handler mhandler=new Handler(Looper.getMainLooper);
mhandler.post(new Runnable() {
            @Override
            public void run() {
                debug2Lbl.setText("Successfully sent message : "+ text + " to server");
            }
        });

2-run your setText at uiThread.it's recommended:

YourActivityName.this.runOnUiThread(new Runnable() {
                            @Override
                            public void run() {
                                debug2Lbl.setText("Successfully sent message : "+ text + " to server");
                            }
                        });

and do it for setText at catch state too. note :i offer you to read synchronized class too.

dont forget to check as worked answer;)

Upvotes: 1

Khemraj Sharma
Khemraj Sharma

Reputation: 58964

As @waqaslam said you can not set UI items with non-UI thread like AsyncTask or other Thread.

Here is a snippet how you will you use setText inside AsyncTask.

 runOnUiThread(new Runnable() {
            @Override
            public void run() {
                debug2Lbl.setText("Successfully sent message : "+ text + " to server");
            }
        });

Upvotes: 0

waqaslam
waqaslam

Reputation: 68177

You are manipulating views on a non-UI thread. Instead, you should make use of AsyncTask or call debug2Lbl.post method to inject your changes on UI thread.

Upvotes: 1

Related Questions