2

I have two classes, one is Activity, and one is Thread class:

Activity:

public class MainActivity extends AppCompatActivity {

    ObjectInputStream inFromServer = null;
    ObjectOutputStream outToServer=null;
    Socket clientSocket = null;
    String userName;

    EditText message;
    static TextView textView;

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

        StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder().permitAll().build();
        StrictMode.setThreadPolicy(policy);

        message = (EditText)findViewById(R.id.txtMessage);
        textView = (TextView)findViewById(R.id.textView);

        try {
            clientSocket = new Socket("10.0.0.41", 10002);

            outToServer =
                    new ObjectOutputStream(clientSocket.getOutputStream());

            inFromServer =
                    new ObjectInputStream((clientSocket.getInputStream()));

        } catch (IOException e) {
            e.printStackTrace();
        }

        ReceiveMessages receiveMessages = new ReceiveMessages(inFromServer);
        receiveMessages.start();
    }

    public void onClickSend(View view) throws IOException {
        outToServer.writeObject("Eliran!" + message.getText().toString());
    }

    public void btnUserName(View view) throws IOException {
        EditText editText = (EditText)findViewById(R.id.userName);
        userName = editText.getText().toString();
        outToServer.writeObject(userName + "!");
    }
}

Thread class:

public class ReceiveMessages extends Thread {

    ObjectInputStream inFromServer = null;

    public ReceiveMessages(ObjectInputStream inFromServer)
    {
        this.inFromServer = inFromServer;
    }

    public void run() {
        try {
            while (true) {
                String message = (String) inFromServer.readObject();
            }
        } catch (IOException e) {
            e.printStackTrace();
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        }
    }
}

I want to set text in the text view of the activity in the Thread class. How can I do it? InflateLayout isn't helping I think.

Pang
  • 9,564
  • 146
  • 81
  • 122
Eliran2211
  • 53
  • 4
  • You can't change UI elements using the Thread class, you should use AsyncTask. Check out this link: http://stackoverflow.com/questions/6964011/handler-vs-asynctask-vs-thread – Michael Apr 12 '17 at 01:04

2 Answers2

0

I would go about it by creating a Callback that gets called when the String is received from the aerver.

interface ReceiveMessageListener {
    void onMessageReceived (String message);
}

Then have my Activity implement the interface and set the text in the onMessageReceived call back.

@Override
public void onMessageReceived (String message) {
    runOnUiThread(new Runnable() {
        @Override
        public void run () {
            textView.setText(message);
        }
    });
}

You'll also have to change the constructor for your MessageReceive class to take a MessageReceiveListener.

From your Activity:

ReceiveMessages receive = new ReceiveMessages(input, this);
jburn2712
  • 167
  • 1
  • 7
0

Only the UI thread can interact with ui elements (such textview). Try using runOnOiThread or mainHandler.post ()

ins1der12111991
  • 146
  • 1
  • 4