开发者

Calling methods from other classes in Android

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 method which calls the method from another class onclick.

I have textfields and edittexts declared in class 1.

Class 2 has functionality for these and uses if statements.

I extend class 1 from class 2 so class 2 uses the variables.

What are the ways I can call methods from other classes in android.

At the moment I just call the method from class 2 in class 1 by using:

class1.method(); 

but this doesn't seem to be working. Appreciate any advice thanks.

Edit:

I have now created in class 1 a variable:

static class2 object;

from this I call in onclick:

obj开发者_如何学Cect.class2method();

I get a nullPointerexception error thanks.


If I've understood your question correctly, if you want to call the methods in a extended class, you need to make sure those methods are either public or protected.

Also omit the classname, eg just call method() not class.method()

Just to elaborate on this some more, here is some code I just cooked up:

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
    }
}


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.


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!

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜