-2

While calling notifydatasetchanged method from async task class in doinbackground method it is showing exception but when calling notifydatasetchanged method in onpostexecute it is working fine.

Code:

 protected String doInBackground(Void... voids) {


        String results = "";
        if (session != null) {

            Folder root = session.getRootFolder();
            ItemIterable<CmisObject> children = root.getChildren();

            int count = 0;
            map_id = new HashMap();
            map_name = new HashMap();
            for (CmisObject o : children) {
                Information current = new Information();
                current.iconId = icons;
                current.title = o.getName();
                current.folderId = o.getId();
                current.date="Modified: "+o.getLastModificationDate().get(o.getLastModificationDate().DATE)
                        +" "+returntext(+o.getLastModificationDate().get(o.getLastModificationDate().MONTH))+
                        " "+o.getLastModificationDate().get(o.getLastModificationDate().YEAR);
                map_id.put(count, current.folderId);
                map_name.put(count, current.title);
                count++;
                data_main.add(current);
                FilesFragment.adapter.notifyDataSetChanged();
            }

        }
        return results;
    }

    @Override
    protected void onPostExecute(String s) {

        pDialog.dismiss();
        FilesFragment.adapter.notifyDataSetChanged();

    }

Inside For loop calling FilesFragment.adapter.notifyDataSetChanged() and in recycler view adding data from list data_main.So what i was thing that if one data comes in my list i will call notifyDatasetchanged and further like this process will be repeated.

Error:

04-02 16:43:14.001 20776-20804/reva.irving E/AndroidRuntime: FATAL EXCEPTION: AsyncTask #1
                                                         Process: reva.irving, PID: 20776
                                                         java.lang.RuntimeException: An error occurred while executing doInBackground()
                                                             at android.os.AsyncTask$3.done(AsyncTask.java:325)
                                                             at java.util.concurrent.FutureTask.finishCompletion(FutureTask.java:354)
                                                             at java.util.concurrent.FutureTask.setException(FutureTask.java:223)
                                                             at java.util.concurrent.FutureTask.run(FutureTask.java:242)
                                                             at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:243)
                                                             at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1133)
                                                             at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:607)
                                                             at java.lang.Thread.run(Thread.java:760)
                                                          Caused by: android.view.ViewRootImpl$CalledFromWrongThreadException: Only the original thread that created a view hierarchy can touch its views.
                                                             at android.view.ViewRootImpl.checkThread(ViewRootImpl.java:6958)
                                                             at android.view.ViewRootImpl.requestLayout(ViewRootImpl.java:1085)
                                                             at android.view.View.requestLayout(View.java:19807)
                                                             at android.view.View.requestLayout(View.java:19807)
                                                             at android.view.View.requestLayout(View.java:19807)
                                                             at android.view.View.requestLayout(View.java:19807)
                                                             at android.view.View.requestLayout(View.java:19807)
                                                             at android.view.View.requestLayout(View.java:19807)
                                                             at android.support.constraint.ConstraintLayout.requestLayout(ConstraintLayout.java:1959)
                                                             at android.view.View.requestLayout(View.java:19807)
                                                             at android.view.View.requestLayout(View.java:19807)
                                                             at android.view.View.requestLayout(View.java:19807)
                                                             at android.support.v7.widget.RecyclerView.requestLayout(RecyclerView.java:3970)
                                                             at android.support.v7.widget.RecyclerView$RecyclerViewDataObserver.onChanged(RecyclerView.java:5060)
                                                             at android.support.v7.widget.RecyclerView$AdapterDataObservable.notifyChanged(RecyclerView.java:11540)
                                                             at android.support.v7.widget.RecyclerView$Adapter.notifyDataSetChanged(RecyclerView.java:6762)
                                                             at reva.irving.MainActivity$GetCabinets.doInBackground(MainActivity.java:158)
                                                             at reva.irving.MainActivity$GetCabinets.doInBackground(MainActivity.java:106)
                                                             at android.os.AsyncTask$2.call(AsyncTask.java:305)
                                                             at java.util.concurrent.FutureTask.run(FutureTask.java:237)
                                                             at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:243) 
                                                             at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1133) 
                                                             at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:607) 
                                                             at java.lang.Thread.run(Thread.java:760) 
Ashish yadav
  • 97
  • 1
  • 11

2 Answers2

1

As the error says

Only the original thread that created a view hierarchy can touch its views

Since view hierarchy can only be touch in main thread you get this error cause your async task doInBackground in done of course on background. onPostExecute is execute from main thread so it works there.

Samuel Eminet
  • 4,647
  • 2
  • 18
  • 32
  • So is there any way that I can update my data in recycler view, when one item added to list and further this process continues – Ashish yadav Apr 02 '18 at 11:30
  • Isn't FilesFragment.adapter.notifyDataSetChanged(); enough from postExecute?, I don't think you need to add this on your for loop – Samuel Eminet Apr 02 '18 at 11:33
  • Actually my list data is very huge so we have to wait till all the data added to list and then calling on post execute and it will populate the data – Ashish yadav Apr 02 '18 at 11:34
1

You have to move the portion of the background task / process that updates the UI on it's main thread. There is a simple piece of code.

runOnUiThread(new Runnable() {
     @Override
     public void run() {

       // Updates your UI here.

    }
});
Jay Rathod
  • 11,131
  • 6
  • 34
  • 58