No enclosing instance of type error while calling method from another class in Android
In terms of the actual error here, if parseYouTubeAndYahoo
class is a non-static inner class inside of your Activity, then you need an instance of the enclosing class in order to instantiate the inner class. So, you'll need:
MainActivity myActivity = new MainActivity();
MainActivity.parseYouTubeAndYahoo asyncTask = myActivity.new parseYouTubeAndYahoo();
However....
You really shouldn't be instantiating non-static inner classes of your Activities from outside of the Activity
because in order to instantiate a non-static innner class, you have to actually instantiate the enclosing class, which, in this case, is the Activity. Activities are meant to be started, not instantiated via new
. If you have an AsyncTask
that you'd like to use in different places, then create a new top-level class that extends from AsyncTask
.
(For an example of creating reusable AsyncTasks
, see: https://github.com/levinotik/ReusableAsyncTask)
Note that the syntax you've tried to use WOULD work if you needed to grab a static nested class. This is because in such a case, the outer class is really just acting as a namespace, but the nested class, because its static, does not actually need a reference to an instance of the outer class. Thus:
OuterClass.StaticNestedClass nestedObject =
new OuterClass.StaticNestedClass();
is the proper syntax for getting an instance of a static nested class.
Android - No enclosing instance of type 'some.abstract.class.name' class is in scope error when extended
ViewHolder
is an inner class of DragItemAdapter
(because it wasn't declared static
). That means that every object of class ViewHolder
must be associated with an object of class DragItemAdapter
(actually, it would have to be a subclass of DragItemAdapter
). You can think of ViewHolder
having a hidden instance variable like
DragItemAdapter __outerObject;
The ViewHolder
can directly access instance variables and methods belonging to the __outerObject
.
That means that when you say new ViewHolder(...)
, you have to have some DragItemAdapter
for the ViewHolder
to be associated with.
The same applies to any subclass of ViewHolder
, including ViewHolderChecklist
, since the subclass inherits the hidden __outerObject
variable.
In the first example, where ViewHolderChecklist
is inside a ChecklistAdapter
, the onCreateViewHolder
method will always be called on a ChecklistAdapter
instance. When that method says new ViewHolderChecklist
, the new object's __outerObject
will be set to the ChecklistAdapter
instance. Also, if an outside class has a ChecklistAdapter adapter;
, it can use that to create a new ViewHolderChecklist
by saying adapter.new ViewHolderChecklist(...)
.
When you move ViewHolderChecklist
outside the class, though, there's no way for a new instance to be created, since there's no way to use new
in a way that would tell it what its __outerObject
is supposed to be. The adapter.new ViewHolderChecklist(...)
syntax won't work, because that syntax is only allowed for nested classes, and ViewHolderChecklist
isn't a nested class. So ViewHolderChecklist
has to be a nested class inside a subclass of DragItemAdapter
.
Correction: It's actually possible to declare ViewHolderChecklist
like this. However, you have to give it an explicit constructor and it has to have a Qualified Superclass Constructor Invocation (see this; see also https://stackoverflow.com/questions/40501642/what-rule-prohibits-this-nested-class-reference/40501815.
What causes error No enclosing instance of type Foo is accessible and how do I fix it?
static class Thing
will make your program work.
As it is, you've got Thing
as an inner class, which (by definition) is associated with a particular instance of Hello
(even if it never uses or refers to it), which means it's an error to say new Thing();
without having a particular Hello
instance in scope.
If you declare it as a static class instead, then it's a "nested" class, which doesn't need a particular Hello
instance.
Inner class calling
You did instantiate your outer class, but you forgot to instantiate your inner class.
BubblesFrame f = new BubblesFrame(); //Instantiate outer class
ActionListener listener = f.new startNewGame(); //Instantiate inner class
I am more curious why would you want to expose your inner class when it is a listener
for your BubblesFrame. You might want to look into your design pattern again.
Usually it is either an anonymous class or a private class.
No enclosing instance of type Foo is accessible
Is the BluetoothSerialService an inner class? If so, make it static
.
See this post
No enclosing instance of type Server is accessible
No Enclosing instance error
This is because you are trying to instantiate a non-static inner class from a static method.
Java has two kinds of inner classes that can be nested at the class level - static and non-static. Non-static classes have a reference to an instance of their "outer" class, inside of which they were instantiated. This allows non-static inner classes access instance variables of their outer class. Static classes, such as your HelloComponent2
, do not access instance variables of their outer class. This lets you instantiate such classes from static functions.
Making the class static
should fix the problem:
static class HelloComponent2 extends JComponent implements MouseMotionListener
Related Topics
Outofmemory Exception When Loading Bitmap from External Storage
How to Display Gif in React-Native Android App
Android 2.1 View's Getdrawingcache() Method Always Returns Null
Does the Android Emulator Support Opengl Es 3.0
Out of Memory Error While Loading Bitmaps
Populating Spinner Directly in the Layout Xml
How to Set Recyclerview App:Layoutmanager="" from Xml
Handlers and Memory Leaks in Android
In Fragment on Back Button Pressed Activity Is Blank
Android Toolbar: Small Title Text in Landscape Mode
Center Align Title in Action Bar Using Styles in Android
Elevation on Android Lollipop Not Working
Android: Is Using Setcontentview Multiple Times Bad While Changing Layouts
To Draw an Underline Below the Textview in Android
How to Programmatically Set Maxlength in Android Textview
Am I Getting the Steps Right for Verifying a User's Android In-App Subscription