不能让从类型处理器的静态引用非静态方法sendEmptyMessage(INT) [英] Cannot make a static reference to the non-static method sendEmptyMessage(int) from the type Handler

查看:539
本文介绍了不能让从类型处理器的静态引用非静态方法sendEmptyMessage(INT)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个错误无法作出从类型处理器的静态引用非静态方法sendEmptyMessage(INT)

如何解决?因为我觉得这是一个问题,这个类,我这样做是不是一个活动?

 新的Thread(){
            公共无效的run(){
            尝试 {

                    名单<赛欧>帆= searchSails();

                    selectSailIntent.putParcelableArrayListExtra(
                            Constant.SAILS,新的ArrayList<赛欧>(帆));

                    。的getContext()startActivity(selectSailIntent);

                    Handler.sendEmptyMessage(0);

                }赶上(例外五){
                     alertDialog.setMessage(e.getMessage());
                     Handler.sendEmptyMessage(1);

                }
            }
        }。开始();
    }
};
 

解决方案
  

不能让从类型处理器的静态引用非静态方法sendEmptyMessage(INT)

这是由于这一事实,即处理程序是指一类,但 sendEmptyMessage 不是一个静态方法(应称为一个对象,而不是在一个类)上

  

如何解决?

要能够调用的 sendEmptyMessage 方法,你要么

  1. 需要的实例化的一个处理程序,即做这样的事情

     处理程序H =新的处理程序();
    h.sendEmptyMessage(0);
     

  2. 添加静态修改为 sendEmptyMessage 方法:

     公共静态无效sendEmptyMessage(int i)以{...
           ^^^^^^
     

I have an error "Cannot make a static reference to the non-static method sendEmptyMessage(int) from the type Handler"

How to fix it? As I think this is a problem that this class where I do this is not an activity?

        new Thread() {
            public void run() {
            try {

                    List<Sail> sails = searchSails();

                    selectSailIntent.putParcelableArrayListExtra(
                            Constant.SAILS, new ArrayList<Sail>(sails));

                    getContext().startActivity(selectSailIntent);

                    Handler.sendEmptyMessage(0);

                } catch (Exception e) {
                     alertDialog.setMessage(e.getMessage());
                     Handler.sendEmptyMessage(1);

                }
            }
        }.start();
    }
};

解决方案

"Cannot make a static reference to the non-static method sendEmptyMessage(int) from the type Handler"

This is due to the fact that Handler refers to a class, but sendEmptyMessage is not a static method (should be called on an object, and not on a class).

How to fix it?

To be able to call the sendEmptyMessage method you will either

  1. Need to instantiate a Handler, i.e., do something like

    Handler h = new Handler();
    h.sendEmptyMessage(0);
    

    or

  2. Add the static modifier to the sendEmptyMessage method:

    public static void sendEmptyMessage(int i) { ...
           ^^^^^^
    

这篇关于不能让从类型处理器的静态引用非静态方法sendEmptyMessage(INT)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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