Calling a non-static method from outside the class

P.Zaccaria

I often have to deal with this kind an error when programming in Java on Android. For example I have a class where I set a flag.

public class ViewActivity extends Activity {  
...

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
       getWindow().addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);
    ...
}

In another class I want to reset the FLAG_KEEP_SCREEN_ON

class DrawOnTop extends View {
...
if (condition) {
    getWindow().addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);
}

but this doesn't work, since I get "The method getWindow is undefined for the type DrawOnTop".

So I try to define a clearFlags method in ViewActivity class

void clearFlags() {
    getWindow().clearFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);
}

and to call it from the DrawOnTop class:

    if (condition) {
        ViewActivity.clearFlags();
    }

This doesn't work as well: I get "Cannot make a static reference to the non-static method clearFlags() from the type ViewActivity". Well, let's make it static then.

static void clearFlags() {
    getWindow().clearFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);
}

and then I get "Cannot make a static reference to the non-static method getWindow from the type Activity"

How could I execute such a statement?

Aksaçlı

If your DrawOnTop class is nested within the ViewActivity you can create a local Context variable and use it to call the getWindow(). If that's not the case then create a receiver in your activity class then from DawOnTop send an intent with your trigger to do whatever the job is. Do not instantiate your activity class, bad idea!

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Java

Cannot access a method call from non-static to non-static class/method

From Java

Calling static method on a class?

From Java

Java: Why are accessible non-static variables from a static methods outside from the class?

From Dev

calling static method from inside the class

From Dev

Ruby class with static method calling a private method?

From Dev

Calling a static method from a non-static context

From Dev

parent::method() - calling non static method

From Dev

Calling ES6 class constructor from class static method

From Dev

Trouble calling a non-static method from an abstract class

From Dev

Calling non-static method from another file/class

From Dev

Calling a method inside Vue component from an outside class

From Dev

Call non static method from different non static class

From Dev

Calling a method from a class

From Dev

Call a non-static method from static inner class

From Dev

running non-static method startActivity(Intent) outside of Mainactivity.class

From Dev

Access private static method from outside the class C++

From Dev

Calling a static jar class method from command line

From Dev

How to get static onScroll event values from CollectionView back to the calling ListFragment or use the values in non-static method in the class?

From Dev

Calling non-static method from another non-static method

From Dev

Undefined method for class / calling methods outside of a class

From Dev

Calling non static method from static method USING instance created in the static method

From Dev

Calling a non static method from a in a static context

From Dev

Calling a method from outside class with dependencies PHP

From Dev

Calling non static method in static method

From Dev

Calling a method from a different class that uses variables outside the method

From Dev

Calling non static method without class instance inside a non static method

From Dev

Method Calling (Static/Non-Static)

From Dev

Problem calling a method in a class from outside the method in Python

From Dev

Calling a non static method using blazor interop from js

Related Related

  1. 1

    Cannot access a method call from non-static to non-static class/method

  2. 2

    Calling static method on a class?

  3. 3

    Java: Why are accessible non-static variables from a static methods outside from the class?

  4. 4

    calling static method from inside the class

  5. 5

    Ruby class with static method calling a private method?

  6. 6

    Calling a static method from a non-static context

  7. 7

    parent::method() - calling non static method

  8. 8

    Calling ES6 class constructor from class static method

  9. 9

    Trouble calling a non-static method from an abstract class

  10. 10

    Calling non-static method from another file/class

  11. 11

    Calling a method inside Vue component from an outside class

  12. 12

    Call non static method from different non static class

  13. 13

    Calling a method from a class

  14. 14

    Call a non-static method from static inner class

  15. 15

    running non-static method startActivity(Intent) outside of Mainactivity.class

  16. 16

    Access private static method from outside the class C++

  17. 17

    Calling a static jar class method from command line

  18. 18

    How to get static onScroll event values from CollectionView back to the calling ListFragment or use the values in non-static method in the class?

  19. 19

    Calling non-static method from another non-static method

  20. 20

    Undefined method for class / calling methods outside of a class

  21. 21

    Calling non static method from static method USING instance created in the static method

  22. 22

    Calling a non static method from a in a static context

  23. 23

    Calling a method from outside class with dependencies PHP

  24. 24

    Calling non static method in static method

  25. 25

    Calling a method from a different class that uses variables outside the method

  26. 26

    Calling non static method without class instance inside a non static method

  27. 27

    Method Calling (Static/Non-Static)

  28. 28

    Problem calling a method in a class from outside the method in Python

  29. 29

    Calling a non static method using blazor interop from js

HotTag

Archive