获取矢量元素崩溃

时间:2014-04-17 08:43:43

标签: android

向量由函数返回:

public Vector<Config> getConfigVector(){

        XmlResourceParser xml = context.getResources().getXml(R.xml.configs);
        Vector<Config> data = new Vector<Config>();
        int eventType;
        try {
            eventType = xml.getEventType();
            String[] attr = {"",""};
            while (eventType != XmlPullParser.END_DOCUMENT) {
                if(eventType == XmlPullParser.START_TAG) {
                      if(xml.getName().equals("config")){
                            Config item = new Config(xml.getAttributeValue(0),xml.getAttributeValue(1));
                            data.add(item);
                      }
                } 
                eventType = xml.next();
            } 
        } catch (Exception e) {
            e.printStackTrace();
        }
        finally{                
            return data;
        }
    }

然后我想循环其元素:

String url = "", num = "", lang = "";
ConfigDb confdb = new ConfigDb(getApplicationContext());
Vector<Config> configs = confdb.getConfigVector();
int nb = configs.size();
for (int i=0; i<nb; i++) {
    Config cfg = (Config)configs.get(i); // this causes bug which stopped the app
    if (cfg.getConfigId() == "cfg.url") {
        url = cfg.getConfigValue();
    }
    else if (cfg.getConfigId() == "cfg.number") {
        num = cfg.getConfigValue();
    }
    else if (cfg.getConfigId() == "cfg.lang") {
        lang = cfg.getConfigValue();
    }
}

在这种情况下如何正确获取向量的每个元素?

2 个答案:

答案 0 :(得分:1)

这很奇怪,看起来像一个完全有效的代码。请你试试这个:

for (Config cfg : configs) {
  if (cfg.getConfigId() == "cfg.url") {
    url = cfg.getConfigValue();
  }
  else if (cfg.getConfigId() == "cfg.number") {
    num = cfg.getConfigValue();
  }
  else if (cfg.getConfigId() == "cfg.lang") {
    lang = cfg.getConfigValue();
  }
}

答案 1 :(得分:0)

if (cfg.getConfigId().equals(new String("cfg.url")))取代测试解决了它!很奇怪 !洛尔