0

My Client class should give the values of x_atual,y_atual etc... I tried to debug the code by commenting the last two lines of code and the settext doesnt even work. The TextViews still have the same defeault text.

public class SecondActivity extends AppCompatActivity {

TextView x_atual,y_atual,x_desejado,y_desejado,ola;
Cliente myClient;

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

    x_atual = (TextView) findViewById(R.id.x_atual);
    y_atual = (TextView) findViewById(R.id.y_atual);
    x_desejado = (TextView) findViewById(R.id.x_desej);
    y_desejado = (TextView) findViewById(R.id.y_desej);
    ola = (TextView) findViewById(R.id.textView12);

    x_atual.setText("0");
    y_atual.setText("0");
    x_desejado.setText("0");
    y_desejado.setText("0");
    ola.setText("ola");

    myClient = new Cliente(x_atual,y_atual,x_desejado,y_desejado);
    myClient.execute();
}


}

I have multiple fragments but I dont think it's that.

public class Cliente extends AsyncTask<Void,String,Void> {

    String dstAddress;
    int dstPort;
    TextView x_atual,y_atual,x_desejado,y_desejado;
    Scanner r;
    String[] valores = new String[4];

    public Cliente(String addr, int port) {
        //super();
        dstAddress = addr;
        dstPort = port;
    }

    public Cliente(TextView x_atual,TextView y_atual, TextView x_desejado, TextView y_desejado) {
        //super();
        this.x_atual = x_atual;
        this.y_atual = y_atual;
        this.x_desejado = x_desejado;
        this.y_desejado = y_desejado;
    }

    @Override
    protected Void doInBackground(Void... params) {

        Socket socket = null;

        try {
            socket = new Socket(dstAddress, dstPort);

            r = new Scanner(new InputStreamReader(socket.getInputStream()));

        } catch (UnknownHostException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }

        valores[0] = r.nextLine();
        valores[1] = r.nextLine();
        valores[2] = r.nextLine();
        valores[3] = r.nextLine();
        publishProgress(valores[0],valores[1],valores[2],valores[3]);
try {
    Thread.sleep(60);
} catch (InterruptedException e) {
    e.printStackTrace();
}
        return null;
    }

    @Override
    protected void onPostExecute(Void result) {
        x_atual.setText(valores[0]);
        y_atual.setText(valores[1]);
        x_desejado.setText(valores[2]);
        y_desejado.setText(valores[3]);
       super.onPostExecute(result);
    }

}
Danny Buonocore
  • 3,731
  • 3
  • 24
  • 46
  • Can you post your client class as well? – Danail Alexiev Aug 13 '16 at 00:07
  • sureeeeeeeeeeeeeee –  Aug 13 '16 at 00:13
  • 1
    Generally, passing views into an AsyncTask is a poor design. You could follow this suggestion here. http://stackoverflow.com/questions/12575068/how-to-get-the-result-of-onpostexecute-to-main-activity-because-asynctask-is-a – OneCricketeer Aug 13 '16 at 00:33
  • Your code not set dstAddress and dstPort. So NPE will caused at `r.nextLine();`. And I guess `onPostExecute` will never be called. – nshmura Aug 13 '16 at 00:41
  • I get dstadress and port from the labels. It's connecting well. –  Aug 13 '16 at 00:46
  • Mmm, there is no code that set `dstAddress' and `dstPort`. – nshmura Aug 13 '16 at 04:24
  • There is. On the MainActivity. It works correctly and I get the data from the server. Look my other post please http://stackoverflow.com/questions/38929109/android-java-asyntask-never-calls-onprogressupdate –  Aug 13 '16 at 04:27

1 Answers1

0

It is because when you call setText, the view creations have not been finished yet.

x_atual.post(new Runnable() {
        @Override
        public void run() {
            x_atual.setText("hi!");
        }
    });

Do this for each one.

This waits until the view is finished, then it will setText.

TWL
  • 6,228
  • 29
  • 65