将AsyncTask onPreExecute添加到片段

时间:2012-11-13 20:52:35

标签: android android-asynctask actionbarsherlock fragment

我想将一个onPreExecute AsyncTask添加到SherlockFragment但它会导致错误。我的代码如下。在preExecute AsyncTask上添加对话框的代码有什么问题?

public class customlist extends SherlockFragment  {
static final String URL = "esample";
static final String KEY_SONG = "song"; // parent node
private static final String KEY_TAB_NUM = "key.tab.num";
 private ProgressDialog pDialog;


ListView list;
LazyAdapterbeth adapter;
XMLParser parser = new XMLParser();
public void onActivityCreated(Bundle savedInstanceState) {
    super.onActivityCreated(savedInstanceState);

}


public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    new getFeed().execute();

}

public View onCreateView(LayoutInflater inflater, ViewGroup container,
        Bundle savedInstanceState) 

{
    View thisfragment = inflater.inflate(R.layout.dovomi, container, false);

    return thisfragment;
}




private class getFeed extends AsyncTask<Void, Void, Document> {
  @Override
    protected void onPreExecute() {
        super.onPreExecute();
        pDialog = new ProgressDialog(customlist.this);
        pDialog.setTitle("Connect to Server");
        pDialog.setMessage("This process can take a few seconds to a few minutes, depending on your Internet Connection Speed.");
        pDialog.setIndeterminate(false);
        pDialog.setCancelable(false);
        pDialog.show();
    }

    protected Document doInBackground(Void... params) {

        XMLParser parser = new XMLParser();
        String xml = parser.getXmlFromUrl(URL); // getting XML from URL
        Document doc = parser.getDomElement(xml); // getting DOM element

        return doc;
    }

它会导致行

出错
pDialog = new ProgressDialog(customlist.this);

如何在此片段中添加dialoge或progressbar?

1 个答案:

答案 0 :(得分:5)

ProgressDialog将Context作为参数。由于您在片段中使用它,因此可以使用getActivity()