this.packagename not working in Android Studio?

205 Views Asked by At

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())));
            }
        });
1

There are 1 best solutions below

1
On BEST ANSWER

Because getPackageName() is not a method of the anonymous class created from View.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(), where OuterClass is the actual name of the outer class.