简体   繁体   English

VOID:同一按钮上调用了静态和非静态

[英]VOID: static and non-static called on same button

I have button, onclick="weboscio". 我有一个按钮,onclick =“ weboscio”。

I want to do two things on click: 我想在点击时做两件事:

public void web(View view) {
    Intent intent = new Intent(this, about.class);
    startActivity(intent); 
    //opens a new layout

And also: 并且:

 public static void warning(Context context, int id, int titleResId, int textResId, PendingIntent intent) {

    NotificationManager notificationManager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
    String title = context.getString(titleResId);   
    ....etc....
    // to open a pop-up window

Just put there two voids into one activity and call it on button click. 只需将两个空隙放在一个活动中,然后单击按钮即可调用它。 I searched quite a lot, but nothing useful.. I also tried: 我搜索了很多内容,但没有任何用处。.我也尝试过:

public static void weboscio(String args[]) {
        home something = new something();
        something.web();
        new something().warning();

It obviously works only for non-static voids.. and also I were getting an error in .web(HERE) and .warning(HERE). 显然,它仅适用于非静态空洞。.而且我在.web(HERE)和.warning(HERE)中遇到错误。

weboscio = onclick function weboscio = onclick函数

home = main java activity 主页 =主要的Java活动

web = activity supposed to open new layout web =应该打开新布局的活动

warning = activity supposed to display some warning on new layout 警告 =应该在新布局上显示一些警告的活动

Ok, after looking at the problem again, I understand what's going on. 好的,再次查看问题后,我了解发生了什么。 You're simply calling the static method incorrectly. 您只是错误地调用了静态方法。

Do this instead: 改为这样做:

public static void weboscio(String args[]) {
    home something = new something();
    something.web();
    home.warning(...);

A static method is called through the class, not the instance of the class. 通过类而不是类的实例调用静态方法。

Here's an example: 这是一个例子:

String x = new String("Hello");
x.concat("World"); // non-static

String y = String.join("Hello", "World"); // static

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM