0
public class MainActivity extends AppCompatActivity {

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

    Button button1 = new Button(this);
    button1 = (Button) findViewById(R.id.click_me);


    button1.setOnClickListener(
            new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    Intent nextActivity = new Intent(this, NextActivity.class);
                    startActivity(nextActivity);
                }
            } 
    );

}

Hi Guys, I'm having an error when declaring / creating a new instance of the Intent,

Intent nextActivity = new Intent(this, NextActivity.class);

It says cannot resolve constructor Intent.... What is wrong?

Sorry for the long post, hope you guys will let me know soon. Thanks!!

WiL BaJaMas
  • 55
  • 1
  • 9
  • you should learn java's basics: like what does `this` means – Selvin Jul 11 '17 at 09:57
  • I did learn java, but the android developers official page said to insert the current object which "this" refers to, this is what I did... – WiL BaJaMas Jul 11 '17 at 09:59
  • No you didn't learn ... obviously in anonymous class `this` means instance of this class not outer class – Selvin Jul 11 '17 at 10:02

1 Answers1

4

try this

button1.setOnClickListener(
            new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    Intent nextActivity = new Intent(MainActivity.this, NextActivity.class);
                    startActivity(nextActivity);
                }
            } 
    );

and make sure you have to declare NextActivity.class in AndroidManifest.xml

Mehul Kabaria
  • 6,404
  • 4
  • 25
  • 50