2014-04-17 76 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