简体   繁体   中英

How to call a method from another class on buttonclick of Android activity?

I have 2 classes in my project. 1) activity_main and 2) newclass

activity_main class has a button - button1. On click of this, I want to call a method from newclass; which is show() method.

So, I wrote: (in activity_main where I have defined button)

public void onClick(View v){

newclass.show();
}

This is giving an error in activity_main; saying newclass.show() is not static. If I make it static, it gives an error in newclass saying it shouldn't be static.

I might have to first create an object of newclass but m New to Java and New to Android too.

Can anyone please help me? Kindly let me know if the question is not clear.

saying newclass.show() is not static. If I make it static, it gives an error in newclass saying it shouldn't be static.

Means show() is not static method in newclass class(static method directly accessed using class name), you need to create class object to access method from.

newclass object=new newclass();
object.show();

Also read about Java Naming conventions

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM