2016-08-13 6 views
0

My Clientクラスは、x_atual、y_atualなどの値を与える必要があります...コードの最後の2行をコメントしてコードをデバッグしようとしましたが、settextは機能しません。 TextViewにはまだ同じ致命的なテキストがあります。Java Android SetText Does Not Work

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(); 
} 


} 

私は複数の断片を持っていますが、それはそれだとは思いません。

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); 
    } 

} 
+0

あなたのクライアントクラスも投稿できますか? –

+0

sureeeeeeeeeeeeeee –

+1

一般的に、ビューをAsyncTaskに渡すのは設計が貧弱です。ここでこの提案に従うことができます。 http://stackoverflow.com/questions/12575068/how-to-get-the-result-of-onpostexecute-to-main-activity-because-asynctask-is-a –

答えて

0

setTextを呼び出すと、ビューの作成がまだ完了していないためです。

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

これをそれぞれ行います。

これは、ビューが終了するまで待ってから、setTextを実行します。