Distinct ImageButton in the same MainActivity

0

I can not put two different actions to two ImageButton different in the same MainActivity , putting the function only to the first ImageButton , if it works, but when putting the function to the second, I get:

  

error: reached end of file while parsing

package prueba.otrointento;

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

import static prueba.otrointento.R.id.btn1;

public class MainActivity extends AppCompatActivity {

    ImageButton btn1;

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

        btn1 = (ImageButton) findViewById(R.id.btn1);

        btn1.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent boton1 = new Intent(MainActivity.this, Main2Activity.class);
                startActivity(boton1

                );
            }

            ImageButton btn2;

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

                btn2 = (ImageButton) findViewById(R.id.btn2);

                btn2.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        Intent boton2 = new Intent(MainActivity.this, Main2Activity.class);
                        startActivity(boton2

                        );
                    }
        });
    }
}
    
asked by UserNameYo 16.12.2016 в 15:13
source

1 answer

1

Your code is incorrect. Within the onCreate of your activity, you create a Listener for your btn1. After configuring what the click on your button does, RETURN TO CREATE THE METHOD onCreate within the event click of your button and that is incorrect. I leave the code as it should be:

package prueba.otrointento;

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

import static prueba.otrointento.R.id.btn1;

public class MainActivity extends AppCompatActivity {

    ImageButton btn1;
    ImageButton btn2;

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

        btn1 = (ImageButton) findViewById(R.id.btn1);
        btn2 = (ImageButton) findViewById(R.id.btn2);

        btn1.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent boton1 = new Intent(MainActivity.this, Main2Activity.class);
                startActivity(boton1);
            }
        });

        btn2.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent boton2 = new Intent(MainActivity.this, Main2Activity.class);
                startActivity(boton2);
            }
        });
    }
}
    
answered by 16.12.2016 / 15:18
source