gpt4 book ai didi

java - Arraylist 索引越界异常

转载 作者:行者123 更新时间:2023-12-02 07:42:13 26 4
gpt4 key购买 nike

所以我只是出于个人原因制作一个小应用程序。基本上它所做的就是从网站解析 xml,然后返回颜色、标题和作者的列表,然后将其全部显示在 ListView 中。它有点工作,但有时我会得到这些indexoutofboundsExceptions,例如总是出现在for循环中。更好地解释 for 循环。结果是一个数组列表。 ListView 中的每一行由 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;
}
}

最佳答案

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。

关于java - Arraylist 索引越界异常,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11439666/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com