0

所以我做了一个成功AsyncTask运行的类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...,就像我的 UI 中的方法一样:

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()onProgressUpdate()方法时永远不会运行。

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

谢谢您的帮助!

4

0 回答 0