[java] Android Call an method from another class

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

I want to call a method from another class.

I've Class1 and Class2.

In Class 2 I've that method:

public void UpdateEmployee(){
    //some code
}

I want to call that 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)

This question is related to java android class android-activity call

The answer is


And, if you don't want to instantiate Class2, declare UpdateEmployee as static and call it like this:

Class2.UpdateEmployee();

However, you'll normally want to do what @parag said.


Add this in MainActivity.

Intent intent = new Intent(getApplicationContext(), Heightimage.class);
startActivity(intent);

In Class1:

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

Examples related to java

Under what circumstances can I call findViewById with an Options Menu / Action Bar item? How much should a function trust another function How to implement a simple scenario the OO way Two constructors How do I get some variable from another class in Java? this in equals method How to split a string in two and store it in a field How to do perspective fixing? String index out of range: 4 My eclipse won't open, i download the bundle pack it keeps saying error log

Examples related to android

Under what circumstances can I call findViewById with an Options Menu / Action Bar item? How to implement a simple scenario the OO way My eclipse won't open, i download the bundle pack it keeps saying error log getting " (1) no such column: _id10 " error java doesn't run if structure inside of onclick listener Cannot retrieve string(s) from preferences (settings) strange error in my Animation Drawable how to put image in a bundle and pass it to another activity FragmentActivity to Fragment A failure occurred while executing com.android.build.gradle.internal.tasks

Examples related to class

String method cannot be found in a main class method Class constructor type in typescript? ReactJS - Call One Component Method From Another Component How do I declare a model class in my Angular 2 component using TypeScript? When to use Interface and Model in TypeScript / Angular Swift Error: Editor placeholder in source file Declaring static constants in ES6 classes? Creating a static class with no instances In R, dealing with Error: ggplot2 doesn't know how to deal with data of class numeric Static vs class functions/variables in Swift classes?

Examples related to android-activity

Kotlin Android start new Activity The activity must be exported or contain an intent-filter How to define dimens.xml for every different screen size in android? Activity, AppCompatActivity, FragmentActivity, and ActionBarActivity: When to Use Which? Not an enclosing class error Android Studio java.lang.IllegalStateException: Fragment not attached to Activity Soft keyboard open and close listener in an activity in Android android.app.Application cannot be cast to android.app.Activity Android Shared preferences for creating one time activity (example) Android ListView with onClick items

Examples related to call

ReactJS - Call One Component Method From Another Component Call a Class From another class What does it mean to "call" a function in Python? How to make method call another one in classes? How to call a mysql stored procedure, with arguments, from command line? Submit form on pressing Enter with AngularJS Running bash script from within python javascript: get a function's variable's value within another function Passing variables, creating instances, self, The mechanics and usage of classes: need explanation Android Call an method from another class