Calling Method in Android

后端 未结 3 1039
遥遥无期
遥遥无期 2021-01-25 17:33

I am trying to call a method I have written. It compiles except for one line...

public class http extends Activity {

httpMethod();            //will not compile         


        
3条回答
  •  面向向阳花
    2021-01-25 18:08

    EDIT: Just to leave no-one in any doubt, this answer only addresses why you're getting a compile-time error. It does not address what you should be doing in which thread and at what time in Android.

    Personally I would recommend that you put Android down for the moment, learn Java in a simpler environment (e.g. console apps) and then, when you're comfortable with the language, revisit Android and learn all the requirements of Android development - which are obviously much more than just the language.


    You're trying to call a method as a statement directly within your class. You can't do that - it has to be part of a constructor, initializer block, other method, or static initializer. For example:

    // TODO: Rename this class to comply with Java naming conventions
    public class http extends Activity {
        // Constructor is able to call the method... or you could call
        // it from any other method, e.g. onCreate, onResume
        public http() {
            httpMethod();
        }
    
        public void httpMethod() {
            ....
        }
    }
    

    Note that I've only given this example to show you a valid Java class. It doesn't mean you should actually be calling the method from your constructor.

提交回复
热议问题