Android:AsyncTask不会调用onProgressUpdate

时间:2013-05-31 00:26:29

标签: android android-asynctask

所以我创建了一个成功运行AsyncTask的{​​{1}}类。我调试了它,整个doInBackground()方法正确运行:

doInBackground()

正如您所看到的,我这样做了public class MyClass extends AsyncTask<String , String , Boolean> { protected Boolean doInBackground(String... strings) { ArrayList<String[]> stories = new ArrayList<String[]>(); ...*irrelevant code that works*... String[][] storiesA = new String[stories.size()][2]; ...*irrelevant code that works*... ArrayList<Integer> displayed = new ArrayList<Integer>(); //calls publishProgress() every 10 seconds with a random String[2] from storiesA for (int x = 0 ; x < storiesA.length ; x++) { if (displayed.size() >= storiesA.length) displayed.clear(); Random gen = new Random(); int rand; rand = gen.nextInt(storiesA.length); while (displayed.contains(rand)) { rand = gen.nextInt(storiesA.length); } displayed.add(rand); publishProgress(storiesA[rand][0] , storiesA[rand][1]); long t0 , t1; t0=System.currentTimeMillis(); do { t1=System.currentTimeMillis(); } while (t1-t0<10000); } return true; } } 参数应该是onProgressUpdate() ...,就像在我的用户界面中的方法一样:

String

如果相关,我还定义了:

protected void onProgressUpdate(String... stories) {
    TextView title = (TextView)findViewById(R.id.bulletinBoardTitle);
    TextView body = (TextView)findViewById(R.id.bulletinBoardText);

    String titleText = stories[0];
    String bodyText = stories[1];

    title.setText(titleText);
    body.setText(bodyText);
}

所以我运行的所有调试器都会出现问题。而整个protected void onPostExecute(Boolean result) { //will never call this method; it is deprecated in this context TextView title = (TextView)findViewById(R.id.bulletinBoardTitle); TextView body = (TextView)findViewById(R.id.bulletinBoardText); title.setText("Refreshing..."); body.setText("Murica!"); } 代码(在这里执行:)

doInBackground()

运行良好并且完全运行,当它调用protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.media); new MyClass().execute(); } 时,publishProcess()方法永远不会运行。

我是一名非常新手的Android程序员,所以如果你知道这里可能出现什么问题,请记住这一点。

感谢您的帮助!

0 个答案:

没有答案