I wanted to add rate us button to Android app.So I added below code.but "getPackageName" colored in RED.I deleted "this".Then it's OK.Why is that? Is it effect to my code...?
Button ratebutton = (Button) findViewById(R.id.ratebutton);
ratebutton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
startActivity(new Intent(Intent.ACTION_VIEW, Uri.parse
("http://play.google.com/store/apps/details?id=" + this.getPackageName())));
}
});
Because
getPackageName()
is not a method of the anonymous class created fromView.OnClickListener
, but a method of the outer class.In an anonymous class,
this
refers to the anonymous class.To explicitly refer to the outer class, you need to write
OuterClass.this.getPackageName()
, whereOuterClass
is the actual name of the outer class.