在doinbackground方法中从异步任务类调用notifydatasetchanged方法时它显示异常但是在onpostexecute中调用notifydatasetchanged方法时它工作正常。
代码:
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循环调用FilesFragment.adapter.notifyDataSetChanged()并在循环器视图中添加数据列表data_main.So我是什么东西,如果一个数据进入我的列表我将调用notifyDatasetchanged进一步像这个过程将重复。
错误:
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)
答案 0 :(得分:1)
正如错误所说
只有创建视图层次结构的原始线程才能触及其视图
由于视图层次结构只能在主线程中触摸,因此您会收到此错误,导致异步任务doInBackground
在后台完成。 onPostExecute
从主线程执行,因此它在那里工作。
答案 1 :(得分:1)
您必须移动后台任务/进程的一部分,以更新其主要线程上的UI。有一个简单的代码。
runOnUiThread(new Runnable() {
@Override
public void run() {
// Updates your UI here.
}
});