- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我有一个代表迷宫的数组。在 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/
当我使用 new GiveMeJson2.execute(ref_id).get() 调用异步类时, 我的应用程序进入 doInBackground,创建一个 JSON 字符串并返回它。 此时,我认为
我正在尝试在后台下载一些文件,为此我使用了下载管理器并制作了此方法: /*** media download ***/ public long mediaDownload(ArrayList
我在尝试使用 onPostExecute 来触发这段代码时遇到问题,我对 java 尤其是多线程很陌生,所以如果我正在做的事情完全错误,请随时纠正我...需要学习. 所以问题是这样的: public
以下代码是我出于 Intent 而添加的代码,结果是从我的login.php 返回的内容。但它无法比较成功与否。在任何情况下它都不会进入 if 循环。但它在警报对话框中显示“登录成功”。 @Overr
字符串 descript 的内容显示在 logcat 中,没有任何问题,但不会显示在 OnPostExecute 对话框中的 TextView 上。这是一个空指针异常。我不知道为什么,因为 episo
当我运行我的应用程序时,onPostExecute似乎没有被调用。它没有像应有的那样填充到 UI 中。另外,关于DoInBackground ,任何超过 for 循环的日志消息: for (int i
我有一个单独的类(不是 Activity)来管理图像及其属性。 在这个类(Image.class)中,我还创建了一个 AsynkTask 来从网络下载位图。一切正常,除了 AsyncTask 继续运行
在 php 脚本的支持下,我有一段代码在 Async Task 中运行。我只想检查我的数据库中是否存在电子邮件 ID。我想显示 toast :“电子邮件已存在”(如果在数据库中)。否则另一个 toas
所以我创建了一个 web 服务调用类,它调用我在 AsyncTask 上扩展的 web 服务: public class WebServiceLoginCall extends AsyncTask{
我有一个扩展 AsyncTask 的类,它旨在用作我的应用程序的通用任务管理器类。 问题:奇怪的行为是进度对话框出现,但从未被关闭。 我确信每个任务实例都会调用 onPostExecute(),因为任
更新,请见下文。 如何将我的类 LocationData 和我的 ArrayList listOfObjects 返回到 onPostExecute()?我想在我的 UI 中使用它,现在它在 Asyn
我正在尝试获取 onPostExecute()完成 doInBackground() 后调用的方法方法,但它没有被调用。我该如何解决这个问题? 我已经调试过了,可以看到ArrayList data里面
我有一些控制权,因此我做了一些线程。我想用 asycntask 将一个屏幕的加载栏显示到另一个屏幕。我的问题是加载栏正在快速显示和消失,然后在没有加载的情况下等待同一个屏幕很多bar.And 一段时间
我在 Android 应用程序上做一个登录系统,当我输入错误的帐户时,onPostExecute 方法返回正确的失败消息,但是当我输入一个现有帐户并且登录正确后它仍然是相同的失败消息,我不明白为什么!
public static class TestTask extends AsyncTask { private String stacktrace; public TestTask
我有一个运行良好的 AsyncTask 类,但在这个类中,我在 onPreExecute 中有一个 ProgressDialog,它工作正常,但我需要在完成进程对话框关闭时,我将代码放在 onPost
我不知道如何找出这个空指针异常发生的位置。 按照 Google Play 的建议,崩溃只在用户的物理设备上发生过一次(到目前为止)——我无法在调试环境中重现它。 Google Play 给我的 Sta
我尝试使用此函数返回对象数组: public static JSONEvent[] invokeFunction(String funName, String requestContent) {
我有一个 Activity (RecipiesActivity),通过单击我的应用程序主 Activity 的“下一步”按钮打开。 在 RecipiesActivity onCreate 中,我想在
我有一个看起来像这样的类: internal class MyClass : AsyncTask>>> { protected override void OnPostExec
我是一名优秀的程序员,十分优秀!