Calling methods from other classes in Android

后端 未结 3 1844
说谎
说谎 2020-12-22 05:44

I am trying to call a method from another class but I get an exception error.

I have two classes and class 1 is the main class

Class 1 has an onclick<

3条回答
  •  死守一世寂寞
    2020-12-22 06:25

    I know this is a little bit late, but having spent 2 full days to find a solution, this one definitely works for me, that could be useful to others:

    1. Define the required function within first class (MainActivity which extends Activity) as a Static:

      public static int getScreenH(){
          Integer H = Sheight;
          return H;
      }
      
    2. Then in the other class (class ResizeTextView extends TextView) call the function:

      Integer hh = MainActivity.getScreenH();
      

    That's it!! The problem was that the calling class was an extension of TextView so none of the methods available within Activity could be used!

提交回复
热议问题