我对线程的概念并不熟悉,但是每次我尝试显示我的recyclerview时,我都会出现此错误: 只有创建视图层次结构的原始线程才能触摸其视图。我正在从服务器中检索有关某些客户端的数据,将其放入ArrayList中,然后在RecyClerview中显示数据。
在方法doinbackground((
中检索数据这是我的代码:
clientlist.java:
public class ClientList extends AppCompatActivity {
private ClientAdapter clientAdapter;
private RecyclerView recyclerView;
List<Client> clientList;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_client_list);
clientList= new ArrayList<>();
recyclerView=findViewById(R.id.client_list_recycler);
ConnectionOdoo tarea;
tarea = new ConnectionOdoo();
tarea.execute();
}
private class ConnectionOdoo extends AsyncTask<Void, String, Boolean> {
@Override
protected Boolean doInBackground(Void... params) {
// Try connection to server
ConnectionToServer oc = ConnectionToServer.connect(Url, Port, DataBase, Username, Passwort);
/* Object[] param = {new Object[0]};
Integer ids = oc.search_count("product.template", param);
System.out.println("Num. of customers: " + ids.toString() + "n");*/
Object[] param = {new Object[]{
new Object[]{"customer", "=", true},
new Object[]{"is_company", "=", false}}};
List<HashMap<String, Object>> data = oc.search_read("res.partner", param, "name", "id");
String msgResult = "";
for (int i = 0; i < data.size(); ++i) {
Log.i("NAMES ", data.get(i).get("name")+"");
clientList.add(new Client(data.get(i).get("name")+"") );
}
clientAdapter=new ClientAdapter(clientList, ClientList.this);
RecyclerView.LayoutManager layoutManager= new LinearLayoutManager(ClientList.this);
**recyclerView.setLayoutManager(layoutManager);**
recyclerView.setAdapter(clientAdapter);
return false;
}
}
}
** **之间的界线是导致错误的线。
这是日志:
Process: com.example.tarik.gestion, PID: 9642
java.lang.RuntimeException: An error occured while executing doInBackground()
at android.os.AsyncTask$3.done(AsyncTask.java:304)
at java.util.concurrent.FutureTask.finishCompletion(FutureTask.java:355)
at java.util.concurrent.FutureTask.setException(FutureTask.java:222)
at java.util.concurrent.FutureTask.run(FutureTask.java:242)
at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:231)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1112)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:587)
at java.lang.Thread.run(Thread.java:818)
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:7665)
at android.view.ViewRootImpl.requestLayout(ViewRootImpl.java:1119)
at android.view.View.requestLayout(View.java:18855)
at android.view.View.requestLayout(View.java:18855)
at android.view.View.requestLayout(View.java:18855)
at android.view.View.requestLayout(View.java:18855)
at android.view.View.requestLayout(View.java:18855)
at android.view.View.requestLayout(View.java:18855)
at android.support.constraint.ConstraintLayout.requestLayout(ConstraintLayout.java:3172)
at android.view.View.requestLayout(View.java:18855)
at android.support.v7.widget.RecyclerView.requestLayout(RecyclerView.java:4202)
at android.view.ViewGroup.removeAllViews(ViewGroup.java:4688)
at android.support.v7.widget.RecyclerView$5.removeAllViews(RecyclerView.java:900)
at android.support.v7.widget.ChildHelper.removeAllViewsUnfiltered(ChildHelper.java:193)
at android.support.v7.widget.RecyclerView.setLayoutManager(RecyclerView.java:1334)
at com.example.tarik.gestion.ClientList$ConnectionOdoo.doInBackground(ClientList.java:64)
at com.example.tarik.gestion.ClientList$ConnectionOdoo.doInBackground(ClientList.java:37)
at android.os.AsyncTask$2.call(AsyncTask.java:292)
at java.util.concurrent.FutureTask.run(FutureTask.java:237)
at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:231)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1112)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:587)
at java.lang.Thread.run(Thread.java:818)
您需要放置在UI线程中更新UI的代码:
runOnUiThread(new Runnable() {
@Override
public void run() {
//your code here to update UI
}
});