Domanda

I have the following code:

//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);
    }   
}

As you can see the TimerTask runs in the onPostExecute method of the NewsWorkerTask

I get the following error when I do this:

FATAL EXCEPTION: Timer-0 android.view.ViewRootImpl$CalledFromWrongThreadException Only the original thread that created a view hierarchy can touch its views.

The reason I put the timer in the onPostExecute is because I need to execute the timer when I get the Data (GetTopNews()) GetTopNews basically gives me the top 10 latest news I want to display them inside a box that switches to the next news every 5 seconds.

È stato utile?

Soluzione

try using

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

inside timertask and update views inside the function above!

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top