Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/194.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/ssl/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Android handlemessage处理程序类不使用toast_Android - Fatal编程技术网

Android handlemessage处理程序类不使用toast

Android handlemessage处理程序类不使用toast,android,Android,我正在编写一个处理程序,并试图在handlemessage方法中打印线程名称 代码如下 public class handler extends Activity { EditText et; Handler h=new Handler() { public void handleMessage(Message m) { Toast.makeText(getApplicationContext(), Thread.currentThread

我正在编写一个处理程序,并试图在handlemessage方法中打印线程名称 代码如下

public class handler extends Activity
{
    EditText et;
    Handler h=new Handler()
    {
    public void handleMessage(Message m)
    {
        Toast.makeText(getApplicationContext(), Thread.currentThread().getName(), Toast.LENGTH_SHORT).show();
    }
    };

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        // TODO Auto-generated method stub
        super.onCreate(savedInstanceState);
        setContentView(R.layout.handler);
        et=(EditText)findViewById(R.id.handle);
        Thread t=new Thread()
        {
            public void run()
            {
                int i=0;
                while(i<10)
                {
                    try
                    {
                    Thread.sleep(1000);
                    }catch(Exception ep){}
                    i++;
                    h.dispatchMessage(h.obtainMessage());
                }
            }
        };
        t.start();


    }

}
公共类处理程序扩展活动
{
编辑文本;
处理程序h=新处理程序()
{
公共无效handleMessage(消息m)
{
Toast.makeText(getApplicationContext(),Thread.currentThread().getName(),Toast.LENGTH_SHORT).show();
}
};
@凌驾
创建时受保护的void(Bundle savedInstanceState){
//TODO自动生成的方法存根
super.onCreate(savedInstanceState);
setContentView(R.layout.handler);
et=(EditText)findViewById(R.id.handle);
线程t=新线程()
{
公开募捐
{
int i=0;

而(i用这个句子代替:

h.dispatchMessage(h.obtainMessage());
使用这个:

h.sendMessage(h.obtainMessage());
崩溃是因为您无法从主线程以外的任何线程对UI进行任何修改,通过调用dispatchMessage就像从当前调用的线程直接调用处理程序的handleMessage一样,在这种情况下,您是在工作线程中进行修改的,但是通过调用
h.sendMessage(h.obtainMessage());
您正在强制处理程序使用它所连接的线程来执行handleMessage,在您的情况下,主线程


注意!

有什么异常?使用runnable显示toast为什么只显示toast消息需要runnable它与我知道的某些循环器类相关。我正在尝试使用它查看dispatchMessage无法运行该方法的原因。我正在尝试打印并显示运行sendmessage和dispatchMessage.y时线程名称的差异你不能从工作线程调用Toast.makeText,这就是为什么我想说,如果你想看到线程名不使用Toast,请使用android.util.Log.d(“YourTag”,“Thread Name:”+Thread.currentThread().getName())……谢谢。但是出现的错误与某个循环器类有关。