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); } }); 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); } }); 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
Source: https://ru.stackoverflow.com/questions/430204/
All Articles