从 OnPostExecute(Async) 更新 UI
Updating UI from OnPostExecute(Async)
我有一个代表迷宫的数组。在 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);
}
更新:
我检查以确保解决方案变量包含解决方案并且它确实包含有效 data.I 也尝试删除睡眠但无济于事。
更新:
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();
}
我有一个代表迷宫的数组。在 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);
}
更新: 我检查以确保解决方案变量包含解决方案并且它确实包含有效 data.I 也尝试删除睡眠但无济于事。
更新: 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 线程
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();
}