button opening a new problem Activity in a class

Asked

Viewed 19 times

1

package com.ftc;

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

public class MainActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        Button btnTela2 = (Button) findViewById(R.id.btnTela2);
        btnTela2.setOnClickListener(this);
    }
    public void onClick(View v){
        Intent it = new Intent(this, Menu.class);
        startActivity(it);
    }
}

btnTela2.setOnClickListener(this); this error is showing inserir a descrição da imagem aqui

2 answers

2

The class you set as Listener should implement the View.OnClickListener. In this case, it is the very Activity(this):

public class MainActivity extends AppCompatActivity implements View.OnClickListener

0

package com.ftc;

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

public class MainActivity extends AppCompatActivity {

    private Button btnTela2;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        btnTela2 = (Button) findViewById(R.id.btnTela2);

        btnTela2.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent it = new Intent(this, Menu.class);
                startActivity(it);
            }
        });
    }

}

Browser other questions tagged

You are not signed in. Login or sign up in order to post.