0

我有以下代码:

//Task that runs in background thread and posts results
private class NewsWorkerTask extends AsyncTask<Void, Void, List<NewsData>> {

    @Override
    protected void onPreExecute() { 
    }

    @Override
    protected List<NewsData> doInBackground(Void... params) {           

        if (NewsDataProvider==null || NewsDataProvider.PageNumber ==0)
        {
            //Get New Data and initialize
            NewsDataProvider = new NewsProvider(getActivity());
            return NewsDataProvider.GetTopNews();
        }
        else
        {
            List<NewsData> tempDataList = NewsDataProvider.GetTopNews();

            // Merge new page
            for (NewsData item : tempDataList) {

                TopNewsDataList.add(item);
            }
        }

        return null;
    }

    /*
     * The system calls this to perform work in the UI thread and delivers
     * the result from doInBackground()
     */
    @Override
    protected void onPostExecute(List<NewsData> data) {

        final List<NewsData> tempDataList = data;

        //Declare the timer
        Timer t = new Timer();

        t.scheduleAtFixedRate(new TimerTask() {

            @Override
            public void run() {
                //Called each time when 1000 milliseconds (1 second) (the period parameter)
                if (tempDataList != null && tempDataList.size() > 0) {

                    if (tempDataList.size() == 1) {
                        TextView txtNewsTitle = (TextView)getView().findViewById(R.id.txtNewsTitle);
                        TextView txtNewsDate = (TextView)getView().findViewById(R.id.txtNewsDate);
                        txtNewsTitle.setText(tempDataList.get(0).DESCRIPTION);
                        txtNewsDate.setText(tempDataList.get(0).NEWS_DATE);
                    }
                    else {
                        TextView txtNewsTitle = (TextView)getView().findViewById(R.id.txtNewsTitle);
                        TextView txtNewsDate = (TextView)getView().findViewById(R.id.txtNewsDate);
                        txtNewsTitle.setText(tempDataList.get(newsIndex).DESCRIPTION);
                        txtNewsDate.setText(tempDataList.get(newsIndex).NEWS_DATE);

                        newsIndex++;

                        if (newsIndex == (tempDataList.size() - 1)) {
                            newsIndex = 0;
                        }
                    }
                }
            }
        },
        //Set how long before to start calling the TimerTask (in milliseconds)
        0,
        //Set the amount of time between each execution (in milliseconds)
        5000);
    }   
}

如您所见,TimerTask在 NewsWorkerTask 的onPostExecute方法中运行

执行此操作时出现以下错误:

致命异常:Timer-0 android.view.ViewRootImpl$CalledFromWrongThreadException 只有创建视图层次结构的原始线程才能接触其视图。

我将计时器放在 onPostExecute 中的原因是因为我需要在获取数据时执行计时器 (GetTopNews())每 5 秒。

4

1 回答 1

1

尝试使用

    yourview.post(new Runnable() {
        public void run() {
//change your defined view here
    }
    });

在 timertask 中并在上面的函数中更新视图!

于 2013-07-07T16:57:35.623 回答