我爲什麼會得到ArrayIndexOutOfBoundsException?儘管初始化數組時使用了大小,但我無法找到爲什麼會得到此異常。當我嘗試增加數組的大小時,會發生此異常。這是我的代碼。任何人都可以建議我做錯了什麼?儘管初始化數組的大小爲
public class MsgAdapter extends BaseAdapter {
public String msgs[]=new String[150];
public Activity context;
public LayoutInflater inflater;
int count=16;
public MsgAdapter(Activity context,String[] msgs) {
super();
this.context = context;
this.msgs = msgs;
this.inflater = (LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
}
public static class ViewHolder
{
TextView msgView;
ImageView b;
}
@Override
public int getCount() {
// TODO Auto-generated method stub
Log.v("count", ""+msgs.length);
return msgs.length+16;
}
@Override
public Object getItem(int position) {
// TODO Auto-generated method stub
return null;
}
@Override
public long getItemId(int position) {
// TODO Auto-generated method stub
return 0;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
// TODO Auto-generated method stub
final ViewHolder holder;
View v;
LayoutInflater inflater = context.getLayoutInflater();
if(convertView==null){
convertView = inflater.inflate(R.layout.msg_list, null);
holder = new ViewHolder();
holder.msgView=(TextView)convertView.findViewById(R.id.msgtext);
holder.b=(ImageView)convertView.findViewById(R.id.sms);
convertView.setTag(holder);
}
else
{
holder=(ViewHolder)convertView.getTag();
}
holder.b.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
Intent sendIntent = new Intent(Intent.ACTION_VIEW);
sendIntent.putExtra("sms_body", holder.msgView.getText().toString());
sendIntent.setType("vnd.android-dir/mms-sms");
v.getContext().startActivity(sendIntent);
}
});
holder.msgView.setText(msgs[position]);//the exception occurs here
return convertView;
}
}
您的程序,要求你,你應該瞭解ArrayIndexOutOfBoundsException異常,即爲什麼你得到這個錯誤。 – RobinHood 2012-03-12 05:19:39