从我的线程执行时,Acitivity 会忽略显示 Toast

发布于 2024-10-26 11:29:53 字数 1031 浏览 0 评论 0原文

为什么我只看到“第一个”toast,而看不到从我的线程创建的其他(应该)toast?

public class BannerExample extends Activity {

    @Override
    public void onCreate(final Bundle savedInstanceState) {

        super.onCreate(savedInstanceState);

        Toast toast = Toast.makeText(this, "first toast", Toast.LENGTH_SHORT);
        toast.show();

        new MyThread(this).start();

    }

    class MyThread extends Thread {

        private Context context;

        public MyThread(Context context) {
            this.context = context;
        }

        public void run() {

            Looper.prepare(); // An exception told me to add this - i have no clue why

            for (int i = 0; i < 3; i++) {

                try {
                    sleep(1000);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }

                Toast toast = Toast.makeText(context, i + "whoho", Toast.LENGTH_SHORT);
                toast.show();
            }

        }

    }

}

Why do I only see the "first" toast and not the others (should be) created from my thread?

public class BannerExample extends Activity {

    @Override
    public void onCreate(final Bundle savedInstanceState) {

        super.onCreate(savedInstanceState);

        Toast toast = Toast.makeText(this, "first toast", Toast.LENGTH_SHORT);
        toast.show();

        new MyThread(this).start();

    }

    class MyThread extends Thread {

        private Context context;

        public MyThread(Context context) {
            this.context = context;
        }

        public void run() {

            Looper.prepare(); // An exception told me to add this - i have no clue why

            for (int i = 0; i < 3; i++) {

                try {
                    sleep(1000);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }

                Toast toast = Toast.makeText(context, i + "whoho", Toast.LENGTH_SHORT);
                toast.show();
            }

        }

    }

}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(1

鲜肉鲜肉永远不皱 2024-11-02 11:29:53
  1. 您不应该调用Looper.prepare。您收到异常是因为您做错了什么:
  2. Toast 是 UI 工具包的一部分,因此必须从 UI 线程访问 - 这就是您收到异常的原因,
  3. 我建议您交换 Thread对于 AsyncTask 并利用 < code>onProgressUpdate 方法来创建 Toast,因为它会自动在 UI 线程上运行。否则,您将需要使用处理程序。
  4. 将上下文从 getBaseContext 传递到构造函数,而不是从当前活动的上下文
  1. You should not be calling Looper.prepare. You were getting the exception because you were doing something wrong:
  2. Toasts are part of the UI toolkit, and therefore must be accessed from the UI thread - this is why you were getting an exception
  3. I suggest you swap Thread for AsyncTask and make use of the onProgressUpdate method to create your toasts as this automatically runs on the UI thread. Otherwise you'll need to use a handler.
  4. Pass the context from getBaseContext rather than the context from the current activity to your constructor
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文