Calling methods from other classes in Android

I try 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 method that calls a method from another onclick class.

I have text fields and edittexts declared in class 1.

Class 2 has functionality for these and uses if statements.

I am extending class 1 from class 2, so class 2 uses variables.

In what ways can I call methods from other classes in android.

Right now, I'm just calling a method from class 2 in class 1, using:

 class1.method(); 

but this does not seem to work. Appreciate any advice.

Edit:

Now I created a variable in class 1:

 static class2 object; 

from this i call onclick :

 object.class2method(); 

I get a nullPointerexception error message.

0
android oop
source share
3 answers

If I understand your question correctly, if you want to call methods in an extended class, you need to make sure that these methods are public or protected.

Also omit the class name, for example, just call method() not class.method()

To dwell on this in more detail, here is the code I just prepared:

 class class1 { private void privateMethod() { // do nothing } protected void protectedMethod() { // do nothing } public void publicMethod() { // do nothing } } class class2 extends class1 { private void testMethod() { privateMethod(); // error - not visible protectedMethod(); // works publicMethod(); // also works } } 
+3
source share

EDITED (because Kevin deleted the previous answer):

Android is a little specific in this situation.

In the event that one of your classes is Activity and the other is a helper class, the situation assumes that you need to create an instance of the helper class in the Activity class. Something like

 private Helper helper = new Helper(this); 

and you can call auxiliary class methods anywhere in your activity using:

 helper.nameOfMethod(requiredParameters); 

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

 methodFromActivityB(parameters); 

Hope it will be a little easier to figure it out now.

+2
source share

I know this is a little late, but after spending 2 full days to find a solution, this definitely works for me, which may be useful to others:

  • Define the required function in the first class (MainActivity, which extends the Activity) as static:

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

     Integer hh = MainActivity.getScreenH(); 

What is it! The problem was that the calling class was a TextView extension, so none of the methods available in the Activity could be used!

+1
source share

All Articles