从 AsyncTask 返回

发布于 2024-12-09 01:53:57 字数 568 浏览 0 评论 0原文

我在根据线程中布尔变量的返回值更改 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

梦在夏天 2024-12-16 01:53:57

没有理由不能从 AsyncTask 的 doInBackground 方法返回布尔值。只需确保定义返回一个布尔值,并且您根据需要进行适当的设置:

private class recorderThread extends AsyncTask<String, Void, Boolean> {
   protected boolean doInBackground(String... strings) {
       boolean result;
       // Do your background process and make sure
       // that you set the boolean correctly
       return result;
   }

   protected void onPostExecute(boolean result) {
       // Your current UI stuff here.
   }
}

另外,放入大量日志记录。确保布尔值设置正确。另外,使用调试器单步执行它。假设您在 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:

private class recorderThread extends AsyncTask<String, Void, Boolean> {
   protected boolean doInBackground(String... strings) {
       boolean result;
       // Do your background process and make sure
       // that you set the boolean correctly
       return result;
   }

   protected void onPostExecute(boolean result) {
       // Your current UI stuff here.
   }
}

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.

甜味拾荒者 2024-12-16 01:53:57

仅允许 UI 线程修改 UI(并且您的异步任务在单独的线程上运行),因此所有 UI 修改都应通过 Activity 的 runOnUIThread() 进行:

    activeHighscoreEntry.setTurns(activeHighscoreEntry.getTurns() + 1);
    runOnUiThread(new Runnable() {
        public void run() {
            if (!handleRemoval(point1)) {
                placeNext();
            }
        }
    });

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:

    activeHighscoreEntry.setTurns(activeHighscoreEntry.getTurns() + 1);
    runOnUiThread(new Runnable() {
        public void run() {
            if (!handleRemoval(point1)) {
                placeNext();
            }
        }
    });
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文