2013-05-08 56 views
0

這是我的代碼:(一些隨機文本完成問題osdifhgsoid hgodfhgo hsdhoigifdshgnvfa oidvojd nobndisfn vbjobsf)。如何從AsynkTask更新TextView?

private class DownloadFilesTask extends AsyncTask<String, Integer, Long> { 
    protected Long doInBackground(String... urls) { 
     try{ 
      Listen(); 
     } 
     catch (Exception x) 
     { 
      textIn.setText("shit! " + x.toString()); 
     } 
     long i = 10; 
     return i; 
    } 
} 

(再次一些隨機的文字,完成問題(愚蠢的系統)dpfgojd ipgsdigjsidoignsdog

public void Listen(){ 
    int count = 0; 
    TextView msg = MyActivity.msg; 
    ServerSocket server; 
    Socket client; 
    try { 
     server = new ServerSocket(9797); 
     Log.d("My log", "server started"); 
     Log.d("My log", "waiting for connnections"); 
     while (started) { 
      try{ 
       msg.setText("waiting for connection"); <=== here crashing 
       client = server.accept(); 
       count++; 
       Log.d("My Log", "Connected"); 
       Log.d("My Log", "aha" + count); 
       int i = 0; 
       String data = null; 
       byte[] bytes = new byte[1024]; 
       InputStream is = client.getInputStream(); 
       OutputStream os = client.getOutputStream(); 
       while (is.available() == 0) { 
        try{ 
         Thread.sleep(50); 
        }catch (Exception cc){} 
       } 
       is.read(bytes, 0, is.available()); 
       os.write("hala".getBytes()); 
       client.close(); 
      }catch (Exception cc) 
      { 
       cc.toString(); 
      } 
     } 

    } catch (Exception el) { 
     el.printStackTrace(); 
    } 
} 

(一些隨機的文字,完成問題)。請幫助

+0

我不太確定爲什麼你的AsyncTask預計一個Long作爲返回值,如果你不打算使用它。更好的方法是將Void作爲返回值。我會更新我自己的答案。 – britzl 2013-05-08 12:15:04

回答

3

通過onPostExecute方法改變它!

+0

我在DownloadFilesTask裏面添加了onPostExecute擴展AsyncTask並且粘貼了msg.setText(action),這不起作用。 如何調用此方法? – Halabella 2013-05-08 12:47:34

-1

我猜runOnUiThread。你不能從UI線程以外的任何其他線程更新UI。

+0

AsyncTask的目的是不必擔心runOnUiThread之類的問題。覆蓋onPostExecute()並在那裏更新。 – britzl 2013-05-08 12:00:49

+0

啊,那裏好點,我還是個初學者,所以我不知道'AsyncTask'可以這樣使用。感謝您指出它;) – 2013-05-08 12:07:52

0

是的,因爲您試圖在doInBackground()方法內設置TextView,並且這是不允許的。

因此,如果您想在doInBackground()方法內設置TextView,請在runOnUiThread方法內執行UI更新操作。

否則,建議您在onPostExecute()方法內執行所有與UI顯示/更新相關的操作,而不是您的AsyncTask類的doInBackground()方法。

+0

但它使用互聯網!它如何在UI線程中運行? – Halabella 2013-05-08 12:54:47

0

好主意會返回一個字符串在doInBackground(),說exceptionCatched。您可以將它設置爲Exception標題catch()塊,然後在onPostExecuted()只需檢查if(!TextUtils.isEmpty(exceptionCatched)) textIn.setText(exceptionCatched);就是這樣!

+0

更好的方法是將異常保存在成員變量中(因爲他是繼承AsyncTask)並在onPostExecute()方法中檢索它 – britzl 2013-05-08 12:10:27

+0

不確定它是好還是壞,但它也是一種好方法;) – lomza 2013-05-08 12:15:48

1

AsyncTask的目的是在單獨的線程中執行長時間運行的任務,然後通過onPostExecute()將結果傳回給UI線程。

另外,我不確定爲什麼你使用Long作爲你的返回值,因爲你似乎沒有使用它。一個更好的解決辦法是有空隙的返回值,並保存異常,並使用它作爲一個指標,如果出了什麼差錯:

private class DownloadFilesTask extends AsyncTask<String, Integer, Void> { 

    private Exception exception = null; 

    @Override 
    protected Void doInBackground(String... urls) { 
     try{ 
      Listen(); 
     } 
     catch (Exception x) { 
      exception = x; 
     } 
    } 

    @Override 
    public void onPostExecute(Void result) { 
     if(exception != null) { 
      textIn.setText("shit! " + exception.toString()); 
     } 
     else { 
      // long running task was completed successfully 
     } 
    } 
} 
+0

OnPost ..不能@Overriden,並且在我的測試過程中未調用此方法.. – Halabella 2013-05-08 12:52:14

+0

對不起,但你錯了。看看這個文檔:http://developer.android.com/reference/android/os/AsyncTask。html – britzl 2013-05-08 17:54:35

0
private class DownloadFilesTask extends AsyncTask<Void, Void,Long>{ 

    @Override 
    protected Long doInBackground(Void... params) { 
     publishProgress(progress); 
     //calculate progress and value from your downloading logic 
    try { 

     } catch (Exception e) { 
      return (long) 0; 
     } 
     return null; 
    } 
    @Override 
    protected void onProgressUpdate(Void... values) { 
     super.onProgressUpdate(values); 
     //dis method run deafult on UI thread , so every time u publish ur onProgressUpdate will be called and update ur text here 
    } 


    @Override 
    protected void onPostExecute(Long result) { 
     super.onPostExecute(result); 
     if(result==0){ 
      //error occured 
     } 
    } 

//異常的情況下返回結果,只要值promt onPostExceute()

+0

爲什麼要使用0作爲錯誤的指示符,並使用null作爲任務成功的指示符? – britzl 2013-05-08 12:08:54

+0

這只是爲了您的使用,我只是簡單地爲他的問題 – 2013-05-08 12:12:06

+0

提供了正確的實現,如果不出現任何異常並且用戶希望看到進度可能在textview中,那麼他必須發佈它。 – 2013-05-08 12:13:36