从 AsyncTask 返回
我在根据线程中布尔变量的返回值更改 UI 中的图像时遇到问题。
我的类定义为:
class recorderThread extends AsyncTask<String, Void, Boolean> {
我传入一个字符串,我想要一个布尔值输出。 如果我无法传递布尔值,那么我可以这样做...但是我尝试过,但无法从线程中更改图像,如下所示
@Override
protected void onPostExecute(Boolean result) {
if (result)
ball = new Ball(getContext(),R.drawable.correctball);
else
ball = new Ball(getContext(),R.drawable.wrongball);
}
是否没有办法可以通过调用 UI 中的线程来获取布尔值?
new recorderThread().execute("A");
谢谢大家,希望这是有道理的。
I am having problems changing an image in my UI depending on the return value of a boolean variable in my thread.
my class is defined as:
class recorderThread extends AsyncTask<String, Void, Boolean> {
I pass in a string, i want a boolean out.
if i cant pass the boolean out then i could do this... but i tried and cant change the image from the thread as follows
@Override
protected void onPostExecute(Boolean result) {
if (result)
ball = new Ball(getContext(),R.drawable.correctball);
else
ball = new Ball(getContext(),R.drawable.wrongball);
}
Is there not a way I can get the boolean value from calling the thread in the UI?
new recorderThread().execute("A");
Thanks Guys, hope it makes sense.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
没有理由不能从 AsyncTask 的 doInBackground 方法返回布尔值。只需确保定义返回一个布尔值,并且您根据需要进行适当的设置:
另外,放入大量日志记录。确保布尔值设置正确。另外,使用调试器单步执行它。假设您在 Ball() 方法中正确更新了 UI,这应该是一个简单的修复。注意:也请检查以确保其正常工作。
There's no reason why you shouldn't be able to return a boolean from your doInBackground method of the AsyncTask. Just make sure that the definition is returning a boolean and that you're setting it appropriately for your needs:
Also, put in lots of logging. Make sure the boolean is being set correctly. Also, step through it with the debugger. Should be an easy fix assuming your correctly updating the UI in your Ball() method. Note: check that too to make sure it works correctly.
仅允许 UI 线程修改 UI(并且您的异步任务在单独的线程上运行),因此所有 UI 修改都应通过 Activity 的 runOnUIThread() 进行:
Only UI Thread is allowed to modify UI (and your async task runs on separate thread), so all UI modification shall happpen via runOnUIThread() of your activity: