java - 如何解决错误:只有创建视图层次结构的原始线程才能接触其视图
问题描述
我不太熟悉线程的概念,但是每次尝试显示我的 RecyclerView 时都会出现此错误:只有创建视图层次结构的原始线程才能触摸其视图。我正在从服务器检索有关某些客户端的数据,将其放入 ArrayList 中,然后在 recyclerView 中显示数据。
在方法 doInBackGround() 中检索数据
这是我的代码:
客户端列表.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
}
});
推荐阅读
- r - R -- install_github 到底是做什么的?
- reactjs - 附加带有动态创建的输入字段的图像,无需重复
- postgresql - 内部连接的别名
- python - 排除名称中带有空格的整个目录
- javascript - 当同一行中的另一个单元格在 Javascript 中具有值时,验证一个单元格是否成为必需的
- excel - 拆分数据并使用 vba 进行比较
- python - 用奇怪的 html 抓取网站
- c# - 有效的 RegEx 不适用于 ASP.Net MVC 正则表达式属性
- mips - 谁能告诉我在这个 MIPS 代码中犯了什么错误?
- python - 导入错误:libopencv_hdf.so.3.3