2014-04-17 78 views
0

一个向量函数的返回:获得一个矢量元素崩溃

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(); 
    } 
} 

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

+0

你会得到什么错误?为什么使用Vector而不是ArrayList? –

+0

Logcat是空白的!好吧,我会尝试与ArrayList – pheromix

+0

与arraylist相同的问题! – pheromix

回答

0

用这个替换测试if (cfg.getConfigId().equals(new String("cfg.url")))解决了它!很奇怪 !大声笑

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(); 
    } 
} 
+0

同样的问题:应用程序停止 – pheromix