Java Arraylist indexoutofbounds异常
所以我只是出于个人原因制作了一个小应用程序。基本上,它所做的就是从一个网站解析xml,然后返回一个颜色、标题和作者的列表,然后在listview中显示所有内容。它有点工作,但有时我会得到这些IndexOutofBoundsSexceptions,例如,总是出现在for循环中。更好地解释for循环。结果是一个数组列表。listview中的每一行由3个元素组成:标题、用户名和颜色。for循环在数组中迭代,基本上将值分配给行,因此最初第一个元素将分配给title,第二个元素分配给username,依此类推。这种模式只会重复。如果有人看到问题,甚至知道更好的解决方法,请分享:) 代码如下:Java Arraylist indexoutofbounds异常,java,android,arrays,listview,Java,Android,Arrays,Listview,所以我只是出于个人原因制作了一个小应用程序。基本上,它所做的就是从一个网站解析xml,然后返回一个颜色、标题和作者的列表,然后在listview中显示所有内容。它有点工作,但有时我会得到这些IndexOutofBoundsSexceptions,例如,总是出现在for循环中。更好地解释for循环。结果是一个数组列表。listview中的每一行由3个元素组成:标题、用户名和颜色。for循环在数组中迭代,基本上将值分配给行,因此最初第一个元素将分配给title,第二个元素分配给username,依此
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;
}
}
公共类ColorsActivity扩展了ListActivity{
/**在首次创建活动时调用*/
私有整数选择=0;
私有字符串信息;
private ArrayList myArr=new ArrayList();
私有int resultofset=0;
私有文本视图设置;
私有搜索结果sr=新搜索结果();
private ArrayList results=new ArrayList();
@凌驾
创建时的公共void(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
设置=(TextView)findViewById(R.id.settings\u text);
最终ListView lv=getListView();
lv.setDividerHeight(0);
colorTask refTask=新建colorTask();
refTask.execute();
settings.setOnClickListener(新视图.OnClickListener(){
公共void onClick(视图v){
colorTask refTask=新建colorTask();
refTask.execute();
}
});
}
公共类MyCustomBaseAdapter扩展了BaseAdapter{
私有ArrayList搜索ArrayList;
私人停车场;
公共MyCustomBaseAdapter(上下文、ArrayList结果){
searchArrayList=结果;
mInflater=LayoutInflater.from(上下文);
}
public int getCount(){
返回searchArrayList.size();
}
公共对象getItem(int位置){
返回searchArrayList.get(位置);
}
公共长getItemId(int位置){
返回位置;
}
公共视图getView(int位置、视图转换视图、视图组父视图){
视窗座;
if(convertView==null){
convertView=mInflater.充气(R.layout.list_项,空);
holder=新的ViewHolder();
holder.txtHex=(TextView)convertView.findViewById(R.id.text1);
holder.txtTitle=(TextView)convertView.findViewById(R.id.TextView02);
holder.txtexser=(TextView)convertView.findViewById(R.id.TextView01);
holder.txtColorValue=(TextView)convertView.findViewById(R.id.TextView03);
convertView.setTag(支架);
}否则{
holder=(ViewHolder)convertView.getTag();
}
holder.txtitle.setText(searchArrayList.get(position.getTitle());
holder.txtUser.setText(searchArrayList.get(position.getUser());
字符串c=searchArrayList.get(position.getHex();
holder.txtHex.setBackgroundColor(Integer.parseInt(c,16)+0xFF000000);
holder.txtColorValue.setText(searchArrayList.get(position.getHex());
返回视图;
}
类视图持有者{
TextView-txtTitle;
文本视图;
TextView-txtHex;
TextView txtColorValue;
}
}
类colorTask扩展了AsyncTask{
private final ProgressDialog=新建ProgressDialog(ColorsActivity.this);
//可以在这里使用UI线程
受保护的void onPreExecute(){
this.dialog.setMessage(“正在联系服务器…”);
this.dialog.show();
}
受保护的ArrayList doInBackground(最终字符串…args){
URL=null;
ParsedExampleDataSet ParsedExampleDataSet=null;
试试{
如果(选择==0){
url=新url(“http://www.colourlovers.com/api/colors/top?resultOffset=“+resultofset);
}
}捕获(格式错误){
//TODO自动生成的捕捉块
e、 printStackTrace();
}
SAXParserFactory spf=SAXParserFactory.newInstance();
saxsp=null;
试一试{
sp=spf.newSAXParser();
}捕获(ParserConfiguration异常e){
//TODO自动生成的捕捉块
e、 printStackTrace();
}捕获(SAXE异常){
//TODO自动生成的捕捉块
e、 printStackTrace();
}
/*获取我们创建的SAXParser的XMLReader*/
XMLReader xr=null;
试一试{
xr=sp.getXMLReader();
}捕获(SAXE异常){
//TODO自动生成的捕捉块
e、 printStackTrace();
}
/*创建新的ContentHandler并将其应用于XML读取器*/
getColors myExampleHandler=新的getColors(选择);
xr.setContentHandler(myExampleHandler);
/*从我们的URL解析xml数据*/
试一试{
parse(新输入源(url.openStream());
parsedExampleDataSet=
myExampleHandler.getParsedData();
myArr=parsedExampleDataSet.toArrayList();
}捕获(IOE异常){
//TODO自动生成的捕获块
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);
}
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);
}
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);
}