Calling methods from other classes in Android

后端 未结 3 1849
说谎
说谎 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:41

    EDITED (because Kevin deleted previous answer):

    Android is a bit specific in this situation..

    In case one of your class is Activity and the other is "helper" class, situation implies than you need to create an instance of the "helper" class in Activity class. Something like

    private Helper helper = new Helper(this);
    

    and than you can call methods of helper class, anywhere in your Activity by using:

    helper.nameOfMethod(requiredParameters);
    

    It might happen that your ClassA is Activity (so it extends Android Activity class) and ClassB is Activity too but extends ActivityA. In this situation you should be able to use methods of ActivityA from ActivityB just by calling it by method name (without explicitly writing where is your method stored).

    methodFromActivityB(parameters);
    

    Hope it's a bit easier to understand it now.

提交回复
热议问题