TheLettuceMaster
TheLettuceMaster

Reputation: 15734

Illegal Enclosing Instance inside a fragment

I have this code:

MainFragmentActivity a = new MainFragmentActivity();
MainFragmentActivity.MasterFrag mm = a.new MasterFrag(); // a.new
MainFragmentActivity.MasterFrag.MasterTask e = mm.new MasterTask();
e.execute();

On the a.new line referenced above, it has a problem with a. It says:

Illegal enclosing instance specification for type MainFragmentActivity.MasterFrag

This block of code is inside a static method inside a Fragment (not static) inside a FragmentActivity. I am not sure how to declare this a var? Making this a static call is not an option either... How can I get a reference to the MainFragmentActivity instance?

Upvotes: 2

Views: 5280

Answers (1)

taoChengDe
taoChengDe

Reputation: 76

I believe you would get this compilation error if the inner class MasterFrag is declared to be static. The correct way to instantiate it would then be:

MainFragmentActivity.MasterFrag mm = new MainFragmentActivity.MasterFrag();

Upvotes: 6

Related Questions