Calling methods from other classes in Android

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

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

提交回复
热议问题