我尝试从名为“MainActivity”的其他类中调用“AsyncTask”类,但是“AsyncTask”类位于名为“SiteAdapter”的类中。我尝试传递一个引用但是不起作用。如何解决这个问题?
Main Activity:
public class MainActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Log.i("StackSites", "OnCreate()");
setContentView(R.layout.activity_main);
//Call the class AsyncTask
new GetAddressTask(this).execute(); // <----ERROR - GetAddressTask cannot be resolved to a type
}
}
SitesAdapter类中的AsyncTask:
public class SitesAdapter extends ArrayAdapter<StackSite> {
...//PROCESS
public class GetAddressTask extends AsyncTask<String, Void, String> {
Context mContext;
public GetAddressTask(Context context) {
super();
mContext = context;
}
//Pass a reference to MainActivity
private MainActivity mainActivity; // <--- WARNING - The value of the field SitesAdapter.GetAddressTask.mainActivity is not used
public GetAddressTask(MainActivity mainActivity)
{
this.mainActivity = mainActivity;
}
@Override
protected String doInBackground(String... arg0) {
...
}
@Override
protected void onPostExecute(String result) {
...
}
}
}
AsyncTask
的doInBackground
方法。 - Batuhan Coşkun