Android: ViewRootCalledFromWrongThreadException upon creating an alertdialog

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

Android: ViewRootCalledFromWrongThreadException upon creating an alertdialog

问题

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

View alertdialog_2 = LayoutInflater.from(candidate_registration_1.this).inflate(R.layout.custom_dialog_layout2,null);

AlertDialog.Builder dialog_2 = new AlertDialog.Builder(candidate_registration_1.this);

dialog_2.setView(alertdialog_2);

MaterialTextView txt = alertdialog_2.findViewById(R.id.dialogtext);

AlertDialog wait_dialog = dialog_2.create();

wait_dialog.getWindow().setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));

call = api.save_data(array.get(0),array.get(1));

if(call != null){

    wait_dialog.show();

    final int[] dotcount = {0};

    new Timer().scheduleAtFixedRate(new TimerTask(){ //在对话框内的文本中创建一个...动画
        @Override
        public void run(){
            dotcount[0] += 1;
            if(dotcount[0] != 3){
                txt.append(".");
            }
            else{
                dotcount[0] = 0;
                String tmp = txt.getText().toString();
                tmp = tmp.replace(".", "");
                txt.setText(tmp);
            }
        }
    },0,1000);

    call.enqueue(new Callback<String>() {
        @Override
        public void onResponse(@NonNull Call<String> call, @NonNull Response<String> response) {
            wait_dialog.cancel();
        }
        @Override
        public void onFailure(@NonNull Call<String> call, @NonNull Throwable t) {}
    });
}

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

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:

View alertdialog_2 = LayoutInflater.from(candidate_registration_1.this).inflate(R.layout.custom_dialog_layout2,null);
AlertDialog.Builder dialog_2 = new AlertDialog.Builder(candidate_registration_1.this);
dialog_2.setView(alertdialog_2);
MaterialTextView txt = alertdialog_2.findViewById(R.id.dialogtext);
AlertDialog wait_dialog = dialog_2.create();
wait_dialog.getWindow().setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));
call = api.save_data(array.get(0),array.get(1));
if(call != null){
wait_dialog.show();
final int[] dotcount = {0};
new Timer().scheduleAtFixedRate(new TimerTask(){ //creating a ... animation in text inside dialog
@Override
public void run(){
dotcount[0] += 1;
if(dotcount[0] != 3){
txt.append(&quot;.&quot;);
}
else{
dotcount[0] = 0;
String tmp = txt.getText().toString();
tmp = tmp.replace(&quot;.&quot;,&quot;&quot;);
txt.setText(tmp);
}
}
},0,1000);
call.enqueue(new Callback&lt;String&gt;() {
@Override
public void onResponse(@NonNull Call&lt;String&gt; call, @NonNull Response&lt;String&gt; response) {
wait_dialog.cancel();
}
@Override
public void onFailure(@NonNull Call&lt;String&gt; call, @NonNull Throwable t) {}
});
}

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:

确定