1

Tengo estos 2 métodos, sin embargo el último no funciona, mi código compila pero cuando doy click en el botón simplemente no pasa nada. ¿Saben cómo lo puedo solucionar?

public class MapsActivity extends FragmentActivity implements OnMapReadyCallback {

    Button siguiente;
    private GoogleMap mMap;
    private Marker marcador;
    double lat = 0.0;
    double lng = 0.0;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_maps);
        // Obtain the SupportMapFragment and get notified when the map is ready to be used.
        SupportMapFragment mapFragment = (SupportMapFragment) getSupportFragmentManager()
                .findFragmentById(R.id.map);
        mapFragment.getMapAsync(this);
    }


    protected void onCreate1(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_maps);

        siguiente = (Button) findViewById(R.id.button_next_services);

        siguiente.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                Intent siguiente = new Intent(MapsActivity.this, SelectServicesActivity.class);
                startActivity(siguiente);
            }
        });
    }

Gracias

1
  • Odalis, debes tener ùnicamente un método onCreate().
    – Jorgesys
    el 10 may. 2017 a las 8:00

2 respuestas 2

1

El método onCreate1() no es un método llamado durante la construcción de la activity, por lo tanto no es válido para lo que deseas. Debes usar únicamente el método onCreate().

    public class MapsActivity extends FragmentActivity implements OnMapReadyCallback {

        Button siguiente;
        private GoogleMap mMap;
        private Marker marcador;
        double lat = 0.0;
        double lng = 0.0;

        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_maps);


            siguiente = (Button) findViewById(R.id.button_next_services);

            siguiente.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {

                    Intent siguiente = new Intent(MapsActivity.this, SelectServicesActivity.class);
                    startActivity(siguiente);
                }
            });


            // Obtain the SupportMapFragment and get notified when the map is ready to be used.
            SupportMapFragment mapFragment = (SupportMapFragment) getSupportFragmentManager()
                    .findFragmentById(R.id.map);
            mapFragment.getMapAsync(this);
        }

    //metodo no valido.
      /*  protected void onCreate1(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_maps);

            siguiente = (Button) findViewById(R.id.button_next_services);

            siguiente.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {

                    Intent siguiente = new Intent(MapsActivity.this, SelectServicesActivity.class);
                    startActivity(siguiente);
                }
            });
        }*/
}
1

El problema que tienes es que onCreate1 no es un método estándard del ciclo de vida de las activities de Android. Por tanto, no se llama de forma implicita en la creación en del FragmentActivity.

De este modo, el evento OnClickListener nunca es establecido en el botón ya que ese código no se está ejecutando y, como es de esperar, al pulsar el botón no se ejecuta nada.

Para establecer el evento en el botón debes ponerlo dentro del onCreate "original", que si se llama en la creación del FragmentActivity, o bien dentro de un método que se llame desde dicho onCreate.

Prueba con este código:

public class MapsActivity extends FragmentActivity implements OnMapReadyCallback {

    Button siguiente;
    private GoogleMap mMap;
    private Marker marcador;
    double lat = 0.0;
    double lng = 0.0;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_maps);
        // Obtain the SupportMapFragment and get notified when the map is ready to be used.
        SupportMapFragment mapFragment = (SupportMapFragment) getSupportFragmentManager()
                .findFragmentById(R.id.map);
        mapFragment.getMapAsync(this);    

        siguiente = (Button) findViewById(R.id.button_next_services);    
        siguiente.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                Intent siguiente = new Intent(MapsActivity.this, SelectServicesActivity.class);
                startActivity(siguiente);
    }

}

Tu Respuesta

By clicking “Publica tu respuesta”, you agree to our terms of service and acknowledge you have read our privacy policy.

¿No es la respuesta que buscas? Examina otras preguntas con la etiqueta o formula tu propia pregunta.