简体   繁体   中英

this.packagename not working in Android Studio?

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 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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM