gpt4 book ai didi

java - 如何解决错误: Only the original thread that created a view hierarchy can touch its views

转载 作者:行者123 更新时间:2023-12-02 09:42:55 25 4
gpt4 key购买 nike

我不太熟悉线程的概念,但每次尝试显示 RecyclerView 时都会出现此错误: 只有创建 View 层次结构的原始线程才能触摸其 View 。我正在从服务器检索有关某些客户端的数据,将其放入 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
}
});

关于java - 如何解决错误: Only the original thread that created a view hierarchy can touch its views,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56917814/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com