Why in this case it is impossible to use this ?

 btnNewActivity.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { Intent intent = new Intent(this, FirstActivity.class); startActivity(intent); } }); 
  • 6
    Because this (literally translated "this") is a pointer to the object from which it was called. In this case, this is called from the View.OnClickListener () object. And in Intent you need to pass context, so you need to write getApplicationContext (). - Kota1921 pm
  • Understood thanks!! - quaresma89

2 answers 2

In this case, this you have points to the OnClickListener object, and it is not a descendant of the Context class. But, if you, for example, asked a listener inside the Activity class, you can write this:

 public class FirstActivity extends Activity { btnNewActivity.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { Intent intent = new Intent(FirstActivity.this, FirstActivity.class); startActivity(intent); } }); } 

    Because you work in an anonymous classroom. To get the current object of the outer class, use the External Class ИмяВнешнегоКласса.this construction ИмяВнешнегоКласса.this