Android: ViewRootCalledFromWrongThreadException upon creating an alertdialog

huangapple go评论90阅读模式
英文:

Android: ViewRootCalledFromWrongThreadException upon creating an alertdialog

问题

我试图在向服务器发送请求时创建并显示AlertDialog,当我收到响应时取消它。这是我的代码:

  1. View alertdialog_2 = LayoutInflater.from(candidate_registration_1.this).inflate(R.layout.custom_dialog_layout2,null);
  2. AlertDialog.Builder dialog_2 = new AlertDialog.Builder(candidate_registration_1.this);
  3. dialog_2.setView(alertdialog_2);
  4. MaterialTextView txt = alertdialog_2.findViewById(R.id.dialogtext);
  5. AlertDialog wait_dialog = dialog_2.create();
  6. wait_dialog.getWindow().setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));
  7. call = api.save_data(array.get(0),array.get(1));
  8. if(call != null){
  9. wait_dialog.show();
  10. final int[] dotcount = {0};
  11. new Timer().scheduleAtFixedRate(new TimerTask(){ //在对话框内的文本中创建一个...动画
  12. @Override
  13. public void run(){
  14. dotcount[0] += 1;
  15. if(dotcount[0] != 3){
  16. txt.append(".");
  17. }
  18. else{
  19. dotcount[0] = 0;
  20. String tmp = txt.getText().toString();
  21. tmp = tmp.replace(".", "");
  22. txt.setText(tmp);
  23. }
  24. }
  25. },0,1000);
  26. call.enqueue(new Callback<String>() {
  27. @Override
  28. public void onResponse(@NonNull Call<String> call, @NonNull Response<String> response) {
  29. wait_dialog.cancel();
  30. }
  31. @Override
  32. public void onFailure(@NonNull Call<String> call, @NonNull Throwable t) {}
  33. });
  34. }

在执行代码时,我收到这个错误:

android.view.ViewRootImpl$CalledFromWrongThreadException: Only the original thread that created a view hierarchy can touch its views.

txt.append("。")这一行。为什么会这样?我该如何解决?

英文:

I am trying to create and show an AlertDialog when I send a request to my server, and cancel it when I receive a response. This is my code:

  1. View alertdialog_2 = LayoutInflater.from(candidate_registration_1.this).inflate(R.layout.custom_dialog_layout2,null);
  2. AlertDialog.Builder dialog_2 = new AlertDialog.Builder(candidate_registration_1.this);
  3. dialog_2.setView(alertdialog_2);
  4. MaterialTextView txt = alertdialog_2.findViewById(R.id.dialogtext);
  5. AlertDialog wait_dialog = dialog_2.create();
  6. wait_dialog.getWindow().setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));
  7. call = api.save_data(array.get(0),array.get(1));
  8. if(call != null){
  9. wait_dialog.show();
  10. final int[] dotcount = {0};
  11. new Timer().scheduleAtFixedRate(new TimerTask(){ //creating a ... animation in text inside dialog
  12. @Override
  13. public void run(){
  14. dotcount[0] += 1;
  15. if(dotcount[0] != 3){
  16. txt.append(&quot;.&quot;);
  17. }
  18. else{
  19. dotcount[0] = 0;
  20. String tmp = txt.getText().toString();
  21. tmp = tmp.replace(&quot;.&quot;,&quot;&quot;);
  22. txt.setText(tmp);
  23. }
  24. }
  25. },0,1000);
  26. call.enqueue(new Callback&lt;String&gt;() {
  27. @Override
  28. public void onResponse(@NonNull Call&lt;String&gt; call, @NonNull Response&lt;String&gt; response) {
  29. wait_dialog.cancel();
  30. }
  31. @Override
  32. public void onFailure(@NonNull Call&lt;String&gt; call, @NonNull Throwable t) {}
  33. });
  34. }

When I execute the code, I get this error:
> android.view.ViewRootImpl$CalledFromWrongThreadException: Only the original thread that created a view hierarchy can touch its views.

in the line txt.append(&quot;.&quot;);. Why is that? How do I fix this?

答案1

得分: 1

The TimerTask is being executed on the Timer thread, not the thread that created the TimerTask, Timer, or MaterialTextView.

From https://developer.android.com/reference/java/util/Timer :

> Corresponding to each Timer object is a single background thread that is used to execute all of the timer's tasks, sequentially.

英文:

The TimerTask is being executed on the Timer thread, not the thread that created the TimerTask, Timer, or MaterialTextView.

From https://developer.android.com/reference/java/util/Timer :

> Corresponding to each Timer object is a single background thread that is used to execute all of the timer's tasks, sequentially.

huangapple
  • 本文由 发表于 2023年5月21日 09:44:13
  • 转载请务必保留本文链接:https://go.coder-hub.com/76297977.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定