0

我有以下onPostExecute代码AsyncTask。它按原样运行良好。但是,当我删除注释的代码时,我从未收到过onPostExecute事件(没有 Toast)。

@Override
protected void onPostExecute(String result) {
      internetData = result;        
      tempview.append(result + "\n");
      Toast.makeText(getApplicationContext(), internetData.toString(), 1).show();
      //setServiceId();
}

setServiceId事件和后续事件是(故意删除 URL),GetXMLData是我的AsyncTask

public void setServiceId() {
    if (internetData != null) {
        try {
            XmlPullParserFactory factory = XmlPullParserFactory.newInstance();
            factory.setNamespaceAware(true);
            XmlPullParser xpp = factory.newPullParser();

            xpp.setInput(new StringReader(internetData));
            int eventType = xpp.getEventType();
            while (eventType != XmlPullParser.END_DOCUMENT) {
                if (eventType == XmlPullParser.TEXT) {
                    serviceId = xpp.getText();
                    serviceHistory = "www.google.com/" + serviceId + "/history";
                    serviceUsage = "www.google.com/" + serviceId + "/usage";
                    serviceDetails = "www.google.com/" + serviceId + "/service";
                    Toast.makeText(getApplicationContext(), serviceId, 1).show();
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
    updateHistory();
    updateDetails();
    updateUsage();
}

public void updateHistory() { //Never called
    new GetXMLData().execute(serviceHistory);
    Toast.makeText(getApplicationContext(), "Update History", 1).show();
}

public void updateUsage() { //Never called
    new GetXMLData().execute(serviceUsage);
    Toast.makeText(getApplicationContext(), "Update Usage", 1).show();
}

public void updateDetails() { //Never called
    new GetXMLData().execute(serviceDetails);
    Toast.makeText(getApplicationContext(), "Update Details", 1).show();
}

    public void updateData() { //First call
    new GetXMLData().execute(serviceListing);
    Toast.makeText(getApplicationContext(), "Update Data", 1).show();
}

上面的最后一个事件updateData被调用,当事件没有被注释掉Toast时,似乎永远不会消失。setServiceId除此之外,不会检索 XML 数据。

当它被注释掉时,我收到了updateData()事件的 XML 数据,但没有后续事件,而且我没有得到Toastfrom onPostExecute。关于为什么会发生这种情况的任何建议?

4

1 回答 1

1

您实际上在 while 循环条件中有一个错误,它将无限循环并使您的应用程序挂起:

    while (eventType != XmlPullParser.END_DOCUMENT) {
        if (eventType == XmlPullParser.TEXT) {
            serviceId = xpp.getText();
            serviceHistory = "www.google.com/" + serviceId + "/history";
            serviceUsage = "www.google.com/" + serviceId + "/usage";
            serviceDetails = "www.google.com/" + serviceId + "/service";
            Toast.makeText(getApplicationContext(), serviceId, 1).show();
        }
    }

在循环中添加下一行:

    eventType = xpp.next();
于 2013-04-15T12:42:23.693 回答