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?

4

1 回答 1

1

我的猜测是你的DownloadWebpageTask是一个非静态嵌套类MainActivity

您可以通过像这样更改它来使其成为静态:

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

或者,您可以DownloadWebpageTask使用 的实例创建一个新的MainActivity,如下所示:

mainactInstance.new DownloadWebpageTask();
于 2013-05-22T21:51:16.830 回答