英文:
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(".");
}
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) {}
});
}
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(".");
. 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.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论