Problem with the use of switch in android studio

0

The problem in question is that I want to configure a switch to move from one activity to another.
The thing is that the program compiles normal but at the time of using the switch it tells me that the application stopped.

Current code:

package com.exercisetosuityou.exercisetosuityou;

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

public class Test_entrenamiento extends AppCompatActivity {
    Switch switchE;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_test_entrenamiento);
        switchE=(Switch) findViewById(R.id.switchEntrenamientoencasa);
    }

    public void Anterior(View view){
        Intent anterior=new Intent(this,MainActivity.class);
        startActivity(anterior);
    }



    public void Entrenamientoencasa(View view) {
        if(switchE.isChecked()){
        Intent entrenamientoencasa=new Intent(this,Entrenamientoencasa.class);
        startActivity(entrenamientoencasa);
        }
    }
}
    
asked by Daniel Sanchez Sepulveda 25.10.2018 в 09:19
source

1 answer

-1

let's see .. since you have not copied all the necessary code.

check your Switch xml (example)

<Switch
    android:id="@+id/switch1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="Switch" />

matters

import android.widget.Switch;

declares the variable

Switch switch1= (Switch)findViewById(R.id.switch1);

run the event

switch1.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
    public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
        if (isChecked) {
            // muestra un activity
        } else {
            // muestra otro activity
        }
    }
});
    
answered by 25.10.2018 в 16:55