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 (egxnew A() where x is an instance of MainActivity)."
Any ideas on how to fix this?
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();
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.