问题
I'm not really familiar with the concept of threads , but I got this error showing up each time I'm trying to display my RecyclerView : Only the original thread that created a view hierarchy can touch its views. I'm retrieving data regarding some clients from the server , putting in it in an ArrayList and then displaying the data within a recyclerView .
The data is retrieved within the method doInBackGround()
Here is my Code :
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;
}
}
}
The line in between ** ** is the line that causes an error .
Here is the log :
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)
回答1:
You need to put your code that updates the UI in the UI thread:
runOnUiThread(new Runnable() {
@Override
public void run() {
//your code here to update UI
}
});
来源:https://stackoverflow.com/questions/56917814/how-to-solve-the-error-only-the-original-thread-that-created-a-view-hierarchy