user1952003
user1952003

Reputation: 5

How can i make a New intent for a button to open a new Class?

Here is My Code,I need a to put something inside{} to link the button to new class or activity like second page.java :

public void addListenerOnButton() {

        ImageButton imageButton = (ImageButton) findViewById(R.id.imageButton1);

        imageButton.setOnClickListener(new OnClickListener() {


/*What can I put here to open new class 
,I mean another activity like secondp.java.*/



            }

I Tried to put below code, but I got the following error:

The constructor Intent(new View.OnClickListener(){}, Class<List>) is undefined

Code

@Override
            public void onClick(View arg0) {
                Intent k = new Intent(this,Secondp.class);
                startActivity(k);

Upvotes: 0

Views: 169

Answers (3)

Dheeraj Bhaskar
Dheeraj Bhaskar

Reputation: 19039

Simple

You're using the wrong this :)
this would pass in the OnClickListener class to the Intent when a context is required.

Use:

startActivity(new Intent(NameofyourcurrentActivity.this,Second.class));

Upvotes: 1

Ewe Seong
Ewe Seong

Reputation: 67

try removing the "this" clause, cause when you use the "this" in your code, it is referring to the onclicklistener class. if you wants to add in the context, make an reference of your context before the methods and use that instead.

Upvotes: 0

Robin Chander
Robin Chander

Reputation: 7415

Change

Intent k = new Intent(this,Secondp.class);

to

Intent k = new Intent(NameofyourcurrentActivity.this,Secondp.class);

Using just this keyword, you are passing the object of OnClickListener

Upvotes: 1

Related Questions