无法从 Handler 类型对非静态方法 sendEmptyMessage(int) 进行静态引用

发布于 2024-11-18 14:20:12 字数 737 浏览 0 评论 0原文

我遇到错误“无法从 Handler 类型对非静态方法 sendEmptyMessage(int) 进行静态引用”

如何修复?我认为这是一个问题,我所做的这门课不是一个活动?

        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();
    }
};

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();
    }
};

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

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

发布评论

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

评论(2

乄_柒ぐ汐 2024-11-25 14:20:12

“无法从 Handler 类型对非静态方法 sendEmptyMessage(int) 进行静态引用”

这是因为 Handler引用一个类,但 sendEmptyMessage 不是静态方法(应该在对象上调用,而不是在类上调用)。

如何解决?

为了能够调用sendEmptyMessage方法,您将

  1. 需要实例化一个Handler,即执行类似的操作

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

  2. static修饰符添加到sendEmptyMessage 方法:

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

"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) { ...
           ^^^^^^
    
神魇的王 2024-11-25 14:20:12
Handler handler = new Handler();
Handler.myStaticMethod();
handler.myNonStaticMethod();

为了调用非静态方法(也称为实例方法),您必须引用特定对象(实例)。您无法引用该类。

静态方法可以通过仅引用类来调用(也可以通过对该类的对象的引用来调用它们,但这被认为是不好的做法)。

关于用法:当您创建实例(对象)时,该对象具有一些内部数据(状态)。非静态方法利用所引用对象的状态,静态方法不需要该数据)。

Handler handler = new Handler();
Handler.myStaticMethod();
handler.myNonStaticMethod();

In order to invoke a non-static method (aka instance methods) you must refer to a particular object (instance). You cannot refer to the class.

Static methods can be invoked by referencing only the class (they can also be called from a reference to an object of that class, but that is considered bad practice).

About usage: when you create an instance (object), that object has some inner data (state). Non static methods make use of the state of the object that is referenced, static methods do not need that data).

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文