【问题标题】:Toast does not appears when thread completes its execution当线程完成执行时,Toast 不会出现
【发布时间】:2025-12-03 17:40:01
【问题描述】:

我的 Android 应用程序中有一个 AsyncTask<Task, Void, Boolean> 线程。当该线程完成执行时,我想通过Toast.makeText() 显示消息。为此,我在 if 以及 doInBackground 方法的 else 内部添加了 Toask.makeText()。线程已成功完成其执行,但未出现 toast 的消息。那么可能是什么问题呢?

代码:

@Override
    protected Boolean doInBackground(Task... arg0) {
        try {

            Task task = arg0[0];

            QueryBuilder qb = new QueryBuilder();

            HttpClient httpClient = new DefaultHttpClient();
            HttpPost request = new HttpPost(qb.buildContactsSaveURL());

            StringEntity params =new StringEntity(qb.createTask(task));
            request.addHeader("content-type", "application/json");
            request.setEntity(params);
            HttpResponse response = httpClient.execute(request);

            if(response.getStatusLine().getStatusCode()<205)
            {
                /*this is the message inside if*/
                Toast.makeText(context, "inside -IF", Toast.LENGTH_SHORT).show();
                return true;
            }
            else
            {
                /*this is the message inside else*/
                Toast.makeText(context, "inside -ELSE", Toast.LENGTH_SHORT).show();
                return false;
            }
        } catch (Exception e) {
            e.printStackTrace();
            return false;
        }
    }

【问题讨论】:

标签: android multithreading android-studio


【解决方案1】:

正如其他人所提到的,您不应该在后台线程上进行任何与 UI 相关的更改/活动。在 onPostExecute 方法所做的主线程上执行此操作。这是一个例子

private class DoSomethingTask extends AsyncTask<Void, Void, Void> {
    @Override
    protected Void doInBackground(Void... params) {
        //Do background process here. Make sure there are no UI related changes here
        return null;
    }

    protected void onPostExecute(Void x) 
    {
        //Do UI related changes here
    }
}

使用您的代码:

private class DoSomethingTask extends AsyncTask<Void, Void, Void> {
    int statusCode;
    @Override
    protected Void doInBackground(Task... arg0) {
        try {

        Task task = arg0[0];

        QueryBuilder qb = new QueryBuilder();

        HttpClient httpClient = new DefaultHttpClient();
        HttpPost request = new HttpPost(qb.buildContactsSaveURL());

        StringEntity params =new StringEntity(qb.createTask(task));
        request.addHeader("content-type", "application/json");
        request.setEntity(params);
        HttpResponse response = httpClient.execute(request);
        statusCode = response.getStatusLine().getStatusCode();

        } catch (Exception e) {
            e.printStackTrace();
            return false;
        }
    }

    protected void onPostExecute(Void x) 
    {
        //Do UI related changes here

        if(statusCode < 205)
        {
            /*this is the message inside if*/
            Toast.makeText(context, "inside -IF", Toast.LENGTH_SHORT).show();
            return true;
        }
        else
        {
            /*this is the message inside else*/
            Toast.makeText(context, "inside -ELSE", Toast.LENGTH_SHORT).show();
            return false;
        }
    }
}

希望这会有所帮助!

【讨论】:

    【解决方案2】:

    线程已成功完成其执行,但 toast 的 消息没有出现

    因为doInBackground 方法在非 ui-Thread 上运行。并且应用程序仅从 UI-Thread 显示 Alert、Toast 和更新 UI 元素。

    doInBackground 显示Toast 将Toast 相关代码包装在runOnUiThread 方法中

    doInBackground 方法返回response 并使用onPostExecute 方法显示Toast。

    【讨论】:

      【解决方案3】:

      它正在执行的任务在后台,它不会像在后台那样显示 toast。 后台任务不会影响您的 UI 或主线程。

      【讨论】:

        【解决方案4】:

        Toast 在 主线程 中工作,您尝试在 Background Thread (doInBackground) 中显示 Toast。将您的 toast 代码移动到 onPostExecutioncallaback,您将能够看到 Toast。

        【讨论】:

        • 如何调用 onPostExecution 方法?请显示执行此操作的代码。
        最近更新 更多