gpt4 book ai didi

android - 从 OnPostExecute(异步)更新 UI

转载 作者:行者123 更新时间:2023-11-30 01:32:06 25 4
gpt4 key购买 nike

我有一个代表迷宫的数组。在 UI 上,迷宫表示为按钮的行和列。在异步任务的 doInBackground 方法中,我搜索了一条路径并使用通向目标的路径初始化了一个解决方案数组。我想要做的是更新这些按钮的按钮文本以显示通向目标的路径。我在 OnPostExecute 中执行此操作。但是,它不起作用。它甚至不执行启用解决方案按钮的最后一行。我在哪里做什么?

private void updateUI() {
Button cell;
TableRow row;

do {
row = (TableRow) (State.maze.getChildAt(solution.row));
cell = (Button) (row.getChildAt(solution.col));
cell.setText(State.pathCell);
try {
Thread.sleep(100);
} catch (InterruptedException e) {
e.printStackTrace();
}
solution = solution.next;
} while (solution.next != null);
}

@Override
protected void onPostExecute(String result) {
super.onPostExecute(result);
updateUI();
//Enable solution button
State.solveResetButton.setEnabled(true);

}

更新:我检查以确保解决方案变量包含解​​决方案并且它确实包含有效数据。我也尝试删除 sleep 但无济于事。

更新:Logcat 输出(红色部分)

 02-23 13:09:15.471 4640-4640/? E/Zygote: MountEmulatedStorage()
02-23 13:09:15.471 4640-4640/? E/Zygote: v2
02-23 13:09:15.471 4640-4640/? E/Zygote: accessInfo : 0
02-23 13:09:15.471 4640-4640/? E/SELinux: [DEBUG] get_category: variable seinfo: default sensitivity: NULL, cateogry: NULL

最佳答案

solution.next 的值不会在您的 while 循环中更新。这会导致无限循环,因此您的 updateUI() 方法永远不会停止执行。

此外,onPostExecute 作为 AsyncTask 的一部分始终在主线程上调用。您不想在主线程上使用 Thread.sleep。这是获得 ANR 的好方法。

关于如何实现这一点,有许多可行的方法。考虑像这样的主线程处理程序:

private void updateUI() {
Button cell;
TableRow row;

if (solution != null){
row = (TableRow) (State.maze.getChildAt(solution.row));
cell = (Button) (row.getChildAt(solution.col));
cell.setText(State.pathCell);
solution = solution.next;
new Handler(Looper.getMainLooper()).postDelayed(new Runnable() {
@Override
public void run() {
updateUI();
}
}, 100);
}
}

您还可以通过使用 publishProgress 回调 UI 线程的新 AsyncTask 来完成此操作

private void updateUI(){
new AsyncTask<Solution, Solution, Void>(){
@Override
protected Void doInBackground(Solution... solutions) {
Solution solution = solutions[0];
do {
publishProgress(solution);
try {
Thread.sleep(100);
} catch (InterruptedException e) {
e.printStackTrace();
}
solution = solution.next;
}
while(solution.next != null);
return null;
}

@Override
protected void onProgressUpdate(Solution... values) {
super.onProgressUpdate(values);
Solution solution = values[0];
row = (TableRow) (State.maze.getChildAt(solution.row));
cell = (Button) (row.getChildAt(solution.col));
cell.setText(State.pathCell);
}

@Override
protected void onPostExecute(Void aVoid) {
super.onPostExecute(aVoid);
//Enable solution button
State.solveResetButton.setEnabled(true);
}
}.execute(solution);
}

@Override
protected void onPostExecute(String result) {
super.onPostExecute(result);
updateUI();
}

关于android - 从 OnPostExecute(异步)更新 UI,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35584983/

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