Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android Call a method from another class

Tags:

I know that this Question is repeated but I can't find the answer in the Internet.

I want to call a method from another class.

I have Class1 and Class2.

In Class 2 I have this method:

public void UpdateEmployee(){     //some code } 

I want to call the above method from Class1.

Thanks for any answer.

----EDIT----

final Button btnUpdate = (Button)findViewById(R.id.btnUpd);    btnUpdate.setOnClickListener(new View.OnClickListener() {            public void onClick(View v) {              Employee updEmple = new Employee();              updEmple.UpdateEmployee();                        }     }); 

----LogCat---

05-28 16:30:44.030: E/AndroidRuntime(25198): FATAL EXCEPTION: main 05-28 16:30:44.030: E/AndroidRuntime(25198): java.lang.NullPointerException: println    needs a message 05-28 16:30:44.030: E/AndroidRuntime(25198):    at android.util.Log.println_native(Native Method) 05-28 16:30:44.030: E/AndroidRuntime(25198):    at android.util.Log.v(Log.java:116) 05-28 16:30:44.030: E/AndroidRuntime(25198):    at   and.net.Employee.UpdateEmployee(Employee.java:77) 05-28 16:30:44.030: E/AndroidRuntime(25198):    at and.net.AndActivity$2.onClick(AndActivity.java:51) 05-28 16:30:44.030: E/AndroidRuntime(25198):    at android.view.View.performClick(View.java:2485) 05-28 16:30:44.030: E/AndroidRuntime(25198):    at android.view.View$PerformClick.run(View.java:9080) 05-28 16:30:44.030: E/AndroidRuntime(25198):    at android.os.Handler.handleCallback(Handler.java:587) 05-28 16:30:44.030: E/AndroidRuntime(25198):    at android.os.Handler.dispatchMessage(Handler.java:92) 05-28 16:30:44.030: E/AndroidRuntime(25198):    at android.os.Looper.loop(Looper.java:123) 05-28 16:30:44.030: E/AndroidRuntime(25198):    at android.app.ActivityThread.main(ActivityThread.java:3683) 05-28 16:30:44.030: E/AndroidRuntime(25198):    at java.lang.reflect.Method.invokeNative(Native Method) 05-28 16:30:44.030: E/AndroidRuntime(25198):    at java.lang.reflect.Method.invoke(Method.java:507) 05-28 16:30:44.030: E/AndroidRuntime(25198):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839) 05-28 16:30:44.030: E/AndroidRuntime(25198):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597) 05-28 16:30:44.030: E/AndroidRuntime(25198):    at  dalvik.system.NativeStart.main(Native Method) 
like image 995
HaOx Avatar asked May 28 '12 15:05

HaOx


People also ask

How do you call a method from another class in Android?

You should use the following code : Class2 cls2 = new Class2(); cls2. UpdateEmployee(); In case you don't want to create a new instance to call the method, you can decalre the method as static and then you can just call Class2.

How do you call a method from another class?

In Java, a method can be invoked from another class based on its access modifier. For example, a method created with a public modifier can be called from inside as well as outside of a class/package. The protected method can be invoked from another class using inheritance.

How do you call one method from another method in Java?

Example: public class CallingMethodsInSameClass { // Method definition performing a Call to another Method public static void main(String[] args) { Method1(); // Method being called. Method2(); // Method being called. } // Method definition to call in another Method public static void Method1() { System. out.

How do you call a public method from another class in Java?

To class a method of another class, we need to have the object of that class. Here, we have a class Student that has a method getName() . We access this method from the second class SimpleTesting by using the object of the Student class.


2 Answers

You should use the following code :

Class2 cls2 = new Class2(); cls2.UpdateEmployee(); 

In case you don't want to create a new instance to call the method, you can decalre the method as static and then you can just call Class2.UpdateEmployee().

like image 115
eric.itzhak Avatar answered Oct 20 '22 17:10

eric.itzhak


In Class1:

Class2 inst = new Class2(); inst.UpdateEmployee(); 
like image 29
Parag Chauhan Avatar answered Oct 20 '22 18:10

Parag Chauhan