How to call a method in MainActivity from another class?

How to call the startChronometer method in another class when the method is declared inside the main action?

Inside MainActivity :

 public void startChronometer() { mChronometer.start(); showElapsedTime(); } 

Inside another class, I tried to do this:

 MainActivity mActivity; mActivity.startChronometer(); 

But an error occurred that said:

 java.lang.NullPointerException. 

Can I find out what else I need to add to the code?

+13
java android
source share
11 answers

You can easily call a method from any fragment in your activity by doing a cast like this:

Java

 ((MainActivity)getActivity()).startChronometer(); 

Kotlin

 (activity as MainActivity).startChronometer() 

Just remember to make sure that this fragment action is actually MainActivity before you do it.

Hope this helps!

+19
source share

I would suggest that you should not create an object of a class of type Activity.

 MainActivity mActivity = new MainActivity(); // BIG NO TO THIS. 

All actions in Android must go through the Activity lifecycle so that they have a valid context attached to them.

Considering Activity as a regular Java class, you get a null context. Since most methods in an Activity are called in its Context, you will get a null pointer exception, so your application crashes.

Instead, move all such methods that need to be called from other classes to the Utility class, which takes a valid context in its constructor, and then use that context in the methods to do the work.

+9
source share

MainActivity.java

 public class MainActivity extends AppCompatActivity { private static MainActivity instance; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); instance = this; } public static MainActivity getInstance() { return instance; } public void myMethod() { // do something... } ) 

AnotherClass.java

 public Class AnotherClass() { // call this method MainActivity.getInstance().myMethod(); } 
+7
source share

Initialize first

 MainActivity mActivity= new MainActivity(); 

Then you can continue

 mActivity.startChronometer(); 
+5
source share

But an error occurred that says the java.lang.NullPointerException error.

This is because you never initialized your MainActivity. you must initialize your object before you call its methods.

 MainActivity mActivity = new MainActivity();//make sure that you pass the appropriate arguments if you have an args constructor mActivity.startChronometer(); 
+4
source share

Use this code in the MainActivity to invoke the method on it.

 ((MainActivity) getActivity()).startChronometer(); 
+3
source share

You must pass the instance of MainActivity to another class, then you can call everything public (in MainActivity) everywhere.

MainActivity.java

 public class MainActivity extends AppCompatActivity { // Instance of AnotherClass for future use private AnotherClass anotherClass; @Override protected void onCreate(Bundle savedInstanceState) { // Create new instance of AnotherClass and // pass instance of MainActivity by "this" anotherClass = new AnotherClass(this); } // Method you want to call from another class public void startChronometer(){ ... } } 

AnotherClass.java

 public class AnotherClass { // Main class instance private MainActivity mainActivity; // Constructor public AnotherClass(MainActivity activity) { // Save instance of main class for future use mainActivity = activity; // Call method in MainActivity mainActivity.startChronometer(); } } 
0
source share

What I did and it works is to create an instance in MainActivity and getter for this instance:

  public class MainActivity extends AbstractMainActivity { private static MainActivity mInstanceActivity; public static MainActivity getmInstanceActivity() { return mInstanceActivity; } 

And the onCreate method just points to this activity:

 @Override protected void onCreate(Bundle savedInstanceState) { mInstanceActivity = this; } 

And in onDestroy you should set this instance to null:

 @Override protected void onDestroy() { super.onDestroy(); mInstanceActivity = null; } 

Later you can call every method in any class you want:

 MainActivity.getmInstanceActivity().yourMethod(); 
0
source share

Initialize the main action instance in another class where you want to use its method.

Just try it, I did it :)

 MainActivity mActivity = new MainActivity(this); mActivity.startChronometer(); 
0
source share

You can make this method static.

 public static void startChronometer(){ mChronometer.start(); showElapsedTime(); } 

this function can be called in another class, as shown below:

 MainActivity.startChronometer(); 

OR

You can make the object of the main class in the second class, for example,

 MainActivity mActivity = new MainActivity(); mActivity.startChronometer(); 
-2
source share

You can simply make this method static, as shown below:

 public static void startChronometer(){ mChronometer.start(); showElapsedTime(); } 

this function can be called in another class, as shown below:

 MainActivity.startChronometer(); 
-2
source share

All Articles