"No enclosing instance of type" error while calling method from another class in Android

Colleagues, I have the such question: 1. In my first class I have the

public class parseYouTubeAndYahoo extends AsyncTask<String, Void, List<VideoDataDescription>>

to parse data from internet. But I need to call execute() method of this class from another class. While trying to right such code:

new MainActivity.parseYouTubeAndYahoo().execute("someURL"); 

I have the next error message from Eclipse

No enclosing instance of type MainActivity is accessible. Must qualify the allocation with an enclosing instance of type MainActivity (e.g. x.new A() where x is an instance of MainActivity).

and really this problem is shrouded in fog for me. So, how to call this method from another class?


Solution 1:

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.

Solution 2:

I guess your parseYouTubeAndYahoo class is an inner class in class MainActivity, in Java, you should instaniate an object of the inner class by new MainActivity().new parseYouTubeAndYahoo(), so call that method like this new MainActivity().new parseYouTubeAndYahoo().execute("someURL");

I also guess MainActivity extends the Activity class, so I think the answer should be this.new parseYouTubeAndYahoo().execute("someURL"); when you just call this method inside your MainActivity class.

Solution 3:

It would be easier just to place AsuncTask into another file as a separete class.

But if you really want to have is an inner class, then either it has to be static or you need to obtain a reference to its parent class first, which could be done like this:

in onCreate of MainActivity:

static MainActivity activityInstance = getContext();

New method in MainActivity:

public static MainActivity getActivityInstance(){
  return activityInstance;
}

Then in another activity you can get the instance and access its public methods

MainActivity instanceOfMainActivity = MainActivity.getInstance();

Then

new instanceOfMainActivity.parseYouTubeAndYahoo().execute("someURL");

Solution 4:

Reference the context of activity to other class and use it.

Like that: public oneofconstructer(Context ctx, .....)