I present my problem:
I have an activity
called public class PosicionamientoActivity extends AppCompatActivity
,
and a java class (not an activity)
public class DecisorCoordenadas {
public DecisorCoordenadas(Context contexto, Coordinador coordinador) {
I would like to know how I could execute a method ( public void resetInfo_Coordenadas(){
) of my java class from the activity.
All the best.
To call a method from another class in a
Activity
, the method must first be defined with the modifierpublic
, which is correct:If we review the parameters that you need, they are the one
Contexto
of theActivity
, although the one of the application can be used which is lighter (Check how it would be used since in some cases you require the one of theActivity
for example when creating a dialog within the class) , and an object of typeCoordinador
As the method
DecisorCoordenadas()
is not defined asstatic
the class has to be instantiated to call the method from theActivity
.In the case of the method described in your question, this method is actually the constructor used to initialize the class :
either
I imagine this method is inside the class
DecisorCoordenadas
, so it can be called in this way from theActivity
, first we instantiate the class since the method is not defined asstatic
and by having the instance we can call the method:A slightly similar question is asked in the Java Tag in the case of Android it would be similar.
You have to create an object of the DecisorCoordenadas class by doing a
And then you can call the method using the previously created object.
Try instantiating the class from your
Activity
like this:You can create an instance of your class or make your method static:
In your main class you would access with your class name (dot) and method name: