0

所以我只是出于个人原因制作一个小应用程序。基本上,它所做的只是从网站解析 xml,然后返回颜色、标题和作者列表,然后将其全部显示在列表视图中。它有点工作,但有时我会得到这些 indexoutofboundsexceptions,例如,总是出现在 for 循环中。更好地解释 for 循环。结果是一个数组列表。列表视图中的每一行都由 3 个元素组成:标题、用户名和颜色。for 循环遍历数组,基本上将值分配给行,因此最初第一个元素将分配给标题,第二个元素将分配给用户名,依此类推。模式只是重复自己。如果有人看到问题,甚至知道更好的方法,请分享:)

   for (int i = 0; i < result.size(); i++) {
       sr = new SearchResults();
       sr.setTitle(result.get(i));
       i++;
       sr.setUser(result.get(i));
       i++;
       sr.setHex(result.get(i));
       results.add(sr);
   }

该错误通常看起来像这样:

07-11 14:48:08.272: E/AndroidRuntime(15370): FATAL EXCEPTION: main
07-11 14:48:08.272: E/AndroidRuntime(15370): java.lang.IndexOutOfBoundsException: Invalid index 62, size is 62

这是代码:

public class ColorsActivity extends ListActivity {
    /** Called when the activity is first created. */
    private int selection = 0;
    private String info;
    private ArrayList<String> myArr = new ArrayList<String>();
    private int resultOffset=0;
    private TextView settings;
    private SearchResults sr = new SearchResults();
    private ArrayList<SearchResults> results = new ArrayList<SearchResults>();

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

        settings = (TextView) findViewById(R.id.settings_text);

        final ListView lv = getListView();
        lv.setDividerHeight(0);

        colorTask refTask = new colorTask(); 
        refTask.execute();

        settings.setOnClickListener(new View.OnClickListener() {
            public void onClick(View v) {
                colorTask refTask = new colorTask(); 
                refTask.execute();
            }
        });  
    }

    public class MyCustomBaseAdapter extends BaseAdapter {
        private ArrayList<SearchResults> searchArrayList;

        private LayoutInflater mInflater;

        public MyCustomBaseAdapter(Context context, ArrayList<SearchResults> results) {
            searchArrayList = results;
            mInflater = LayoutInflater.from(context);
        }

        public int getCount() {
            return searchArrayList.size();
        }

        public Object getItem(int position) {
            return searchArrayList.get(position);
        }

        public long getItemId(int position) {
            return position;
        }

        public View getView(int position, View convertView, ViewGroup parent) {
            ViewHolder holder;
            if (convertView == null) {
                convertView = mInflater.inflate(R.layout.list_item, null);
                holder = new ViewHolder();
                holder.txtHex = (TextView) convertView.findViewById(R.id.text1);
                holder.txtTitle = (TextView) convertView.findViewById(R.id.TextView02);
                holder.txtUser = (TextView) convertView.findViewById(R.id.TextView01);
                holder.txtColorValue = (TextView) convertView.findViewById(R.id.TextView03);

                convertView.setTag(holder);
            } else {
                holder = (ViewHolder) convertView.getTag();
            }

            holder.txtTitle.setText(searchArrayList.get(position).getTitle());
            holder.txtUser.setText(searchArrayList.get(position).getUser());
            String c = searchArrayList.get(position).getHex();
            holder.txtHex.setBackgroundColor(Integer.parseInt(c, 16)+0xFF000000);
            holder.txtColorValue.setText(searchArrayList.get(position).getHex());

            return convertView;
        }

        class ViewHolder {
            TextView txtTitle;
            TextView txtUser;
            TextView txtHex;
            TextView txtColorValue;
        }
    }

    class colorTask extends AsyncTask<String, Void, ArrayList<String>> {
        private final ProgressDialog dialog = new ProgressDialog(ColorsActivity.this);

        // can use UI thread here
        protected void onPreExecute() {
           this.dialog.setMessage("Contacting server...");
           this.dialog.show();
        }

        protected ArrayList<String> doInBackground(final String... args) {
            URL url = null;
            ParsedExampleDataSet parsedExampleDataSet = null;
            try {                       
                if (selection == 0) {
                     url = new URL ("http://www.colourlovers.com/api/colors/top?resultOffset=" + resultOffset);
                } 

            } catch (MalformedURLException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
                SAXParserFactory spf = SAXParserFactory.newInstance();
                SAXParser sp = null;
                try {
                    sp = spf.newSAXParser();
                } catch (ParserConfigurationException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                } catch (SAXException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }

                /* Get the XMLReader of the SAXParser we created. */
                XMLReader xr = null;
                try {
                    xr = sp.getXMLReader();
                } catch (SAXException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
                /* Create a new ContentHandler and apply it to the XML-Reader*/
                getColors myExampleHandler = new getColors(selection);
                xr.setContentHandler(myExampleHandler);

                /* Parse the xml-data from our URL. */
                try {
                    xr.parse(new InputSource(url.openStream()));
                    parsedExampleDataSet =
                        myExampleHandler.getParsedData();
                    myArr = parsedExampleDataSet.toArrayList();             
                } catch (IOException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                } catch (SAXException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }   
                    return myArr;               
        }

        // can use UI thread here
        protected void onPostExecute(final ArrayList<String> result) {
           if (this.dialog.isShowing()) {
              this.dialog.dismiss();
           }
           if (result != null) {    
               for (int i = 0; i < result.size(); i++) {
                   sr = new SearchResults();
                   sr.setTitle(result.get(i));
                   i++;
                   sr.setUser(result.get(i));
                   i++;
                   sr.setHex(result.get(i));
                   results.add(sr);
               }
               ListView lv = getListView();
               lv.setAdapter(new MyCustomBaseAdapter(ColorsActivity.this, results));
               resultOffset += 20;

           } else {
               Toast.makeText(ColorsActivity.this, "Error contacting server.", Toast.LENGTH_SHORT).show();
           }
        }

     }
    public class SearchResults {
         private String title = "";
         private String hex = "";
         private String user = "";

         public void setTitle(String title) {
          this.title = title;
         }

         public String getTitle() {
          return title;
         }

         public void setHex(String hex) {
          this.hex = hex;
         }

         public String getHex() {
          return hex;
         }
         public void setUser(String user) {
           this.user = user;
         }

         public String getUser() {
           return user;
         }
    }
4

4 回答 4

2
for (int i = 0; i < result.size(); i++) {
    sr = new SearchResults();
    sr.setTitle(result.get(i));
    i++;
    sr.setUser(result.get(i));
    i++;
    sr.setHex(result.get(i));
    results.add(sr);
}

你在这里盲目地增加i。这很可能是问题所在。找到问题的根源,您正在解析的数据很可能有时不完整或解析不正确,因此它并不总是产生您想要的参数数量(即您解析的东西期望标题、用户和颜色,但是只得到一个标题和一个用户)。

由于 的大小List为 62,这表明您在解析时至少错过了 1 个参数(标题、用户或颜色)。如果一切都被正确解析,你List的大小应该是 63。

于 2012-07-11T19:08:25.900 回答
0

当您增加 i (i++) 时,您将超出数组的范围,因为您正在从数组的 0 大小进行迭代。当你达到 size-2 时,for 循环内的第二个 i++ 将是一个不存在的索引。

于 2012-07-11T19:09:03.597 回答
0

你的问题出在你的 for 循环中。您正在增加循环内的变量 i 。因此,当大小为 62 时 i 为 61 时,您也添加一个 i,尝试访问索引 62,当大小为 62 时。

于 2012-07-11T19:09:13.403 回答
0

问题是您试图访问数组列表中不存在的索引。问题肯定在for循环内部:

for (int i = 0; i < result.size(); i++) {
   sr = new SearchResults();
   sr.setTitle(result.get(i));
   i++;
   sr.setUser(result.get(i));
   i++;
   sr.setHex(result.get(i));
   results.add(sr);
}

如果你只在循环声明中增加 i 就可以了;问题将出现在另外两个 i++ 语句之一,因为在每个语句之后,您都没有检查是否超过了数组的长度。这必然意味着在一种或多种情况下,有一个三元组(标题、用户、十六进制)缺少一个或多个参数。如果应该始终存在所有三个参数,则数组未正确填充。

如果您希望代码在参数不匹配的情况下是故障安全的,那么每次增加它时检查 i 数组的长度,即在循环内再增加两次 - 然后,如果它已达到数组的长度,则处理以您认为合适的任何方式出现错误(跳出循环,用虚拟数据填充 sr 的剩余值等)。

像这样的东西:

for (int i = 0; i < result.size(); i++) {
   sr = new SearchResults();
   sr.setTitle(result.get(i));

   i++;
   if (i == result.size()) {
      sr.setUser("dummy");
   }
   else {
      sr.setUser(result.get(i));
   }

   i++;
   if (i == result.size()) {
      sr.setHex("#000000");
   }
   else {
      sr.setHex(result.get(i));
   }

   results.add(sr);
}
于 2012-07-11T19:21:45.340 回答