Java solves the problem of No enclosing instance of type XXX is accessible

Source: Internet
Author: User

Sometimes we have to move some of the implementation classes of the activity into Java classes, such as writing the following two classes:

In the Lifecircle class:

public class Lifecircle {public class Mybroadcast extends Broadcastreceiver {@Overridepublic void onreceive (Context conte XT, Intent Intent) {}}}

If you go to new in this activity:

public class Settings extends Activity {@Overrideprotected void onCreate (Bundle savedinstancestate) {super.oncreate ( Savedinstancestate); Setcontentview (r.layout.test_layout); Lifecircle lifecircle = new Lifecircle (this); Mybroadcast cast = new Mybroadcast ();}}

The compiler will report an error:

-No enclosing instance of type lifecircle is accessible. Must qualify the allocation with an enclosing instance of type lifecircle (e.g. X.new A () where X was an instance of  Li fecircle).

One solution is to define Mybroadcast as the static class

To make a way to get:

Package Com.example.shoplistdownload;import Android.content.broadcastreceiver;import Android.content.Context; Import Android.content.intent;import Android.content.intentfilter;public class Lifecircle {Context mcontext;private Static lifecircle sinstance;private mybroadcast mbroadcast;    Private Settings minstance; Lifecircle (Context context) {Mcontext = Context;minstance = (Settings) Context;} public static Lifecircle Getintance (context context) {if (sinstance = = null) {sinstance = new lifecircle (context);} return sinstance;} public class Mybroadcast extends Broadcastreceiver {@Overridepublic void OnReceive (context context, Intent Intent) {}}pub LIC void Registerbroadcaster () {mbroadcast = new mybroadcast (); intentfilter filter = new Intentfilter (); filter.addaction ("Com.example.action"); Minstance.registerreceiver (mbroadcast, filter);}}

public class Settings extends Activity {@Overrideprotected void onCreate (Bundle savedinstancestate) {super.oncreate ( Savedinstancestate); Setcontentview (r.layout.test_layout); Lifecircle lifecircle = new Lifecircle (this); Lifecircle.registerbroadcaster ();}}


Java solves the problem of No enclosing instance of type XXX is accessible

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.