Problem when switching from a second activity to a third AndroidStudio

1

Good morning, I'm practicing with AndroidStudio and I want to make an app with several activities , the case is that from the main to the second happens perfectly but from this second to the rest the application closes with the following error:

  

java.lang.IllegalStateException: Could not find method   onStatistics (View) in a parent or ancestor Context for   android: onClick attribute defined on view class   android.support.v7.widget.AppCompatButton with id 'Statistics'

I have tried several things but I do not give with the key, currently I am only linking a button (Statistics) to the method, the rest will be the same :( I have tested with intent as well)

MenuPrincipal Class:

   package raulredondo.macesmanager;

   import android.content.Intent;
   import android.support.v7.app.AppCompatActivity;
   import android.os.Bundle;
    import android.view.View;
    import android.widget.Button;
    import android.widget.SearchView;

    public class MenuPrincipal extends AppCompatActivity{
    @Override
    protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_menu_principal);
    }

    public void onEstadisticas(View view){
     setContentView(R.layout.activity_estadisticas);
     }



    }

activity_menu_principal.xml:

 <?xml version="1.0" encoding="utf-8"?>
   <android.support.constraint.ConstraintLayout 
   xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context="raulredondo.macesmanager.MenuPrincipal">

   <LinearLayout
       android:layout_width="0dp"
       android:layout_height="200dp"
       android:orientation="vertical"
       android:weightSum="1"
       tools:layout_constraintTop_creator="1"
       tools:layout_constraintRight_creator="1"
       android:layout_marginStart="2dp"
       android:layout_marginEnd="2dp"
       app:layout_constraintRight_toRightOf="parent"
       android:layout_marginTop="8dp"
       tools:layout_constraintLeft_creator="1"
       app:layout_constraintLeft_toLeftOf="parent"
       app:layout_constraintTop_toTopOf="parent">

        <Button
        android:id="@+id/plantilla"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="Plantilla" />

        <Button
        android:id="@+id/Estadisticas"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:onClick="onEstadisticas"
        android:text="Estadisticas" />

        <Button
        android:id="@+id/Eventos"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:onClick="onEventos"
        android:text="Eventos" />

        <Button
        android:id="@+id/convocatoria"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="Convocatoria" />

        </LinearLayout>

         <LinearLayout
    android:layout_width="0dp"
    android:layout_height="205dp"
    android:orientation="horizontal"
    tools:layout_constraintRight_creator="1"
    tools:layout_constraintBottom_creator="1"
    android:layout_marginStart="11dp"
    app:layout_constraintBottom_toBottomOf="parent"
    android:layout_marginEnd="11dp"
    app:layout_constraintRight_toRightOf="parent"
    tools:layout_constraintLeft_creator="1"
    android:layout_marginBottom="16dp"
    app:layout_constraintLeft_toLeftOf="parent">

    <TextView
        android:id="@+id/proxPartidoMenu"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:gravity="bottom|center_horizontal"
        android:text="Próximo partido:"
        android:textSize="30sp" />
   </LinearLayout>
   </android.support.constraint.ConstraintLayout>
    
asked by Raul.Rt 30.11.2017 в 20:13
source

1 answer

1

In your main activity:

    public class MenuPrincipal extends AppCompatActivity{
        private Button btn;
        private Activity mActivity;

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

           this.mActivity = this;
           btn = (Button) findViewById(R.id.btnEstadisticas);

          btn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
               Intent i = new Intent(mActivity, ActivityEstadisticas.class);
               mActivity.startActivity(i);
            }
        });
}

Keep in mind that you can assign a function to a button by means of the XML and by means of the class. As you have in your code by means of the property android:onClick="nombre_funcion" you assign it directly to the XML on the other hand by means of serious code:

btn = (Button) findViewById(R.id.btnEstadisticas); // enlazas tu control con tu variable

          btn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
               Intent i = new Intent(mActivity, ActivityEstadisticas.class);
               mActivity.startActivity(i);
            }
        });
    
answered by 30.11.2017 / 21:41
source