Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

356
Vistas
Crashing on startActivity when clicking button

Having the same issue. The button is initialized at the right time but for some reason, it crashes on click. The xml file says the onClick handler is missing the related activity.

xml

<Button
    android:id="@+id/start_reg_btn"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentBottom="true"
    android:layout_centerHorizontal="true"
    android:layout_marginBottom="216dp"
    android:onClick="onClick"
    android:text="@string/need_a_new_account" />

Java

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

    mRegBtn = (Button) findViewById(R.id.start_reg_btn);

    mRegBtn.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            Intent reg_intent = new Intent(StartActivity.this, RegisterActivity.class);
            startActivity(reg_intent);
        }
    });
}

Very new to android programming. Any help is appreciated

over 4 years ago · Santiago Trujillo
3 Respuestas
Responde la pregunta

0

There are two ways of solving your issue:

  1. When you declare onClick handler in your XML, you need to implement the method in your activity.

In your case, you have declared an onClick handler for your button on XML with this line:

 android:onClick="onClick"

So, you now have to create a method name onClick() in your activity and do your code there like this:

public void onClick(View v) {
  Intent reg_intent = new Intent( StartActivity.this, RegisterActivity.class);
  startActivity(reg_intent);
}
  1. Remove this line from your XML:
 android:onClick="onClick"

and do what you did already:

mRegBtn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent reg_intent = new Intent( StartActivity.this, RegisterActivity.class);
                startActivity(reg_intent);
            }
        });

So, the main concept is this you can't use android:onClick and setOnClickListener together. You have to use one at a time.

over 4 years ago · Santiago Trujillo Denunciar

0

android:onClick and `setOnClickListener` can not used simultaneous.

To use android:onClick correctly, you could refer to the official link

over 4 years ago · Santiago Trujillo Denunciar

0

Try getting current context using getContext() and implement as.

public void onClick(View v) {
    Intent reg_intent = new Intent(v.getContext(), RegisterActivity.class);
    v.getContext().startActivity(reg_intent);
}

And either define onClick from xml file or define it programmatically using View.setOnClickListener. Both can not co-exist.

over 4 years ago · Santiago Trujillo Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda