所以我只是出于个人原因制作一个小应用程序。基本上,它所做的只是从网站解析 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;
}
}