吐司不能在新的可运行使用? [英] Toast can't be used in new runnable?

查看:232
本文介绍了吐司不能在新的可运行使用?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

作为标题,我是用土司可运行,但有错误
我的code:

 公共Runnable的研究背景=新的Runnable(){    公共无效的run(){
        // TODO自动生成方法存根
        尝试
        {
            而(!Thread.interrupted())
            {
        弦乐味精=这是一个测试;
        Toast.makeText(这一点,味精,Toast.LENGTH_SHORT).show();
        视频下载(1000);
            }
        }
        赶上(InterruptedException的C)
        {
            c.printStackTrace();
        }
    }};


解决方案

您不能直接在其他线程中使用烤面包,但有一个解决方案
您创建msgHandler

  mHandler =新的处理程序(){
              @覆盖公共无效的handleMessage(消息MSG){
                 字符串mString =(字符串)msg.obj;
                 Toast.makeText(这一点,mString,Toast.LENGTH_SHORT).show();
              }
          };

之后,你从你的线程传递信息

 新主题(新的Runnable(){                           @覆盖
                           公共无效的run(){
                                   而(!Thread.interrupted())
                                   {                                       消息味精=新的Message();
                                       msg.obj =文本;
                                       mHandler.sendMessage(MSG);
                                        尝试{
                                            视频下载(100);
                                        }
                                        赶上(InterruptedException的E){
                                           e.printStackTrace();
                                        }
                                   }
                           }
                   })。开始();

as the title,i was used toast in runnable but there is an error my code:

public Runnable backgroud=new Runnable(){

    public void run() {
        // TODO Auto-generated method stub
        try
        {
            while(!Thread.interrupted())
            {
        String msg="this is a test";
        Toast.makeText(this, msg, Toast.LENGTH_SHORT).show();
        Thread.sleep(1000);
            }
        }
        catch(InterruptedException c)
        {
            c.printStackTrace();
        }
    }

};

解决方案

you can not use toast directly in other thread but there is a solution you create your msgHandler

 mHandler = new Handler() { 
              @Override public void handleMessage(Message msg) { 
                 String mString=(String)msg.obj;
                 Toast.makeText(this, mString, Toast.LENGTH_SHORT).show();
              }
          };

after that you pass message from your thread

 new Thread(new Runnable() {

                           @Override
                           public void run() {
                                   while(!Thread.interrupted())
                                   {

                                       Message msg=new Message();
                                       msg.obj="your text";
                                       mHandler.sendMessage(msg);
                                        try {
                                            Thread.sleep(100);
                                        } 
                                        catch (InterruptedException e) {
                                           e.printStackTrace();
                                        }
                                   }
                           }
                   }).start();

这篇关于吐司不能在新的可运行使用?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆