2012-08-31 96 views
1

我試圖駁回的AsyncTask的onPostExecute()對話框,並設置TextView的文本,但TextView中不會改變,我得到了「已聚焦窗口,忽視聚焦增益...」 。更新的TextView

下面的代碼:顯示

protected void onPreExecute() { 
    dialog = ProgressDialog.show(mContext, "","Loading...", true); 

} 

protected void onPostExecute(Object result) { 
    dialog.dismiss(); 
    tv.setText("some text"); 

} 

進度對話框,當我的後臺工作完成後,它被駁回,但有向的TextView沒有變化。沒有進度對話框,文本視圖被更新。

任何想法,解決這個問題?

謝謝!

+0

你有沒有試圖把dialog.dismiss()你tv.setText()線下? –

+0

剛纔:)同樣的問題... – newman555p

+0

還有很多類似的問題。 [這個人說他在乾淨的項目之後就像你一樣工作](http://stackoverflow.com/q/11680631/1134705)。 – jnthnjns

回答

2

嘗試這段代碼在調用dialog.dismiss()

  if (!dialog.isShowing()) 
      { 
       TextView tv = (TextView) findViewById(R.id.textView); 
       tv.setText("some text"); 
      } 
+0

好的,我剛剛意識到,即使沒有你的解決方案,我的「一些文本」,實際上是從doInBackground()結果,textview更新:(當我使用我在doInBackground()中處理的實際數據時,它不起作用。正如我所提到的那樣,在對話框中,一切都很好,所以我還有一個問題,無論如何你都要付出代價。 – newman555p

+0

**「當我使用我在doInBackground()中處理的實際數據時,它不起作用」** ,你能解釋一下嗎?你現在面臨什麼問題? – Swayam

+0

對不起,我還不清楚。我試着用我的原始代碼實際上使用「一些文本」和「文本」顯示在文本視圖中。使用我從doInBackground()得到的字符串,該字符串不會顯示在文本視圖中,我試着用這個和不用你的代碼 – newman555p

0
public class MyActivity extends Activity 
    { 
/** 
* Called when the activity is first created. 
*/ 
ProgressBar progressBar; 

@Override 
public void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.main); 
    progressBar = (ProgressBar) findViewById(R.id.pb); 
    TextView view=(TextView) findViewById(R.id.myt); 
    new Task(view).execute(); 


} 

通過傳遞TextView的實例在構造函數中,我們可以在進度使用

private class Task extends AsyncTask<Void, Integer, Void> { 
    TextView view; 
    private Task(TextView textView) { 
     this.view= textView; 
    } 

    int myProgress; 

    @Override 
    protected void onPreExecute() { 
     myProgress = 0; 
     super.onPreExecute(); 
    } 

    @Override 
    protected void onPostExecute(Void aVoid) { 
     Toast.makeText(MyActivity.this, 
       "onPostExecute", Toast.LENGTH_LONG).show(); 

     super.onPostExecute(aVoid); 
    } 

    @Override 
    protected void onProgressUpdate(Integer... values) { 

     progressBar.setProgress(values[0]); 
     view.setText("Value"+myProgress); 
     super.onProgressUpdate(values); 
    } 

    @Override 
    protected void onCancelled() { 
     super.onCancelled(); //To change body of overridden methods use File | Settings | File Templates. 
    } 


    @Override 
    protected Void doInBackground(Void... voids) { 
     while (myProgress < 100) { 
      myProgress++; 

      publishProgress(myProgress); 
      SystemClock.sleep(100); 
     } 
     return null; 
    } 
} 

    }