0

Okay so i had the problem of network usage in UI Thread and now using AsyncTask. I am using Fragments and have a problem.

When putting this line:

    ConnectivityManager connMgr = (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE);

I have to add getActivity() because its in a fragmetn, then the error goes away but then i get an error on this line:

 new DownloadWebpageTask().execute(stringUrl);

The error is: "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)."

Any ideas on how to fix this?

Tom Doe
  • 331
  • 6
  • 23
  • Are you sure this is the code that is getting the error ? – aran May 22 '13 at 21:49
  • Yeah, its not a error log, its put the red line under it and gave me that error message. – Tom Doe May 22 '13 at 21:52
  • Possible duplicate of [Java - No enclosing instance of type Foo is accessible](http://stackoverflow.com/questions/9560600/java-no-enclosing-instance-of-type-foo-is-accessible) – fabian Mar 04 '16 at 00:23

1 Answers1

1

My guess is that your DownloadWebpageTask is a non-static nested class of MainActivity.

You can make it static by changing it like so:

public static class DownloadWebpageTask extends AsyncTask<..> {
...
}

Alternatively, you could create a new DownloadWebpageTask by using the instance of MainActivity, like so:

mainactInstance.new DownloadWebpageTask();
Thomas
  • 1,508
  • 2
  • 22
  • 35