我有一个应用程序在打开时会加载一个列表视图,但是我得到了一个数组索引越界异常,没有关于问题所在的线索。它试图在某个地方访问 index=-1,但我不知道在哪里。
该应用程序从数据库加载作业信息,将它们放入 ArrayList 中的单独作业对象中,然后从那里将其加载到 ListView 中。我真的不知道异常来自哪里,我检查了我所有的代码。列表中只有 1 项作业,但应用程序拒绝打开并在我尝试打开时强制关闭。这是 LogCat 输出
谢谢
LogCat 输出
01-11 16:38:43.644: E/AndroidRuntime(7267): FATAL EXCEPTION: main
01-11 16:38:43.644: E/AndroidRuntime(7267): java.lang.ArrayIndexOutOfBoundsException: length=29; index=-1
01-11 16:38:43.644: E/AndroidRuntime(7267): at android.text.StaticLayout.calculateEllipsis(StaticLayout.java:738)
01-11 16:38:43.644: E/AndroidRuntime(7267): at android.text.StaticLayout.out(StaticLayout.java:702)
01-11 16:38:43.644: E/AndroidRuntime(7267): at android.text.StaticLayout.generate(StaticLayout.java:410)
01-11 16:38:43.644: E/AndroidRuntime(7267): at android.text.StaticLayout.<init>(StaticLayout.java:140)
01-11 16:38:43.644: E/AndroidRuntime(7267): at android.widget.TextView.makeSingleLayout(TextView.java:5888)
01-11 16:38:43.644: E/AndroidRuntime(7267): at android.widget.TextView.makeNewLayout(TextView.java:5745)
01-11 16:38:43.644: E/AndroidRuntime(7267): at android.widget.TextView.onMeasure(TextView.java:6102)
01-11 16:38:43.644: E/AndroidRuntime(7267): at android.view.View.measure(View.java:15513)
01-11 16:38:43.644: E/AndroidRuntime(7267): at android.widget.RelativeLayout.measureChildHorizontal(RelativeLayout.java:645)
01-11 16:38:43.644: E/AndroidRuntime(7267): at android.widget.RelativeLayout.onMeasure(RelativeLayout.java:425)
01-11 16:38:43.644: E/AndroidRuntime(7267): at android.view.View.measure(View.java:15513)
01-11 16:38:43.644: E/AndroidRuntime(7267): at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:4827)
01-11 16:38:43.644: E/AndroidRuntime(7267): at android.widget.LinearLayout.measureChildBeforeLayout(LinearLayout.java:1404)
01-11 16:38:43.644: E/AndroidRuntime(7267): at android.widget.LinearLayout.measureHorizontal(LinearLayout.java:1052)
01-11 16:38:43.644: E/AndroidRuntime(7267): at android.widget.LinearLayout.onMeasure(LinearLayout.java:590)
01-11 16:38:43.644: E/AndroidRuntime(7267): at android.view.View.measure(View.java:15513)
01-11 16:38:43.644: E/AndroidRuntime(7267): at android.widget.ListView.measureScrapChild(ListView.java:1183)
01-11 16:38:43.644: E/AndroidRuntime(7267): at android.widget.ListView.measureHeightOfChildren(ListView.java:1248)
01-11 16:38:43.644: E/AndroidRuntime(7267): at android.widget.ListView.onMeasure(ListView.java:1158)
01-11 16:38:43.644: E/AndroidRuntime(7267): at android.view.View.measure(View.java:15513)
01-11 16:38:43.644: E/AndroidRuntime(7267): at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:4827)
01-11 16:38:43.644: E/AndroidRuntime(7267): at android.widget.LinearLayout.measureChildBeforeLayout(LinearLayout.java:1404)
01-11 16:38:43.644: E/AndroidRuntime(7267): at android.widget.LinearLayout.measureVertical(LinearLayout.java:695)
01-11 16:38:43.644: E/AndroidRuntime(7267): at android.widget.LinearLayout.onMeasure(LinearLayout.java:588)
01-11 16:38:43.644: E/AndroidRuntime(7267): at android.view.View.measure(View.java:15513)
01-11 16:38:43.644: E/AndroidRuntime(7267): at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:4827)
01-11 16:38:43.644: E/AndroidRuntime(7267): at android.widget.FrameLayout.onMeasure(FrameLayout.java:310)
01-11 16:38:43.644: E/AndroidRuntime(7267): at android.view.View.measure(View.java:15513)
01-11 16:38:43.644: E/AndroidRuntime(7267): at android.widget.LinearLayout.measureVertical(LinearLayout.java:847)
01-11 16:38:43.644: E/AndroidRuntime(7267): at android.widget.LinearLayout.onMeasure(LinearLayout.java:588)
01-11 16:38:43.644: E/AndroidRuntime(7267): at android.view.View.measure(View.java:15513)
01-11 16:38:43.644: E/AndroidRuntime(7267): at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:4827)
01-11 16:38:43.644: E/AndroidRuntime(7267): at android.widget.FrameLayout.onMeasure(FrameLayout.java:310)
01-11 16:38:43.644: E/AndroidRuntime(7267): at com.android.internal.policy.impl.PhoneWindow$DecorView.onMeasure(PhoneWindow.java:2176)
01-11 16:38:43.644: E/AndroidRuntime(7267): at android.view.View.measure(View.java:15513)
01-11 16:38:43.644: E/AndroidRuntime(7267): at android.view.ViewRootImpl.performMeasure(ViewRootImpl.java:1874)
01-11 16:38:43.644: E/AndroidRuntime(7267): at android.view.ViewRootImpl.measureHierarchy(ViewRootImpl.java:1089)
01-11 16:38:43.644: E/AndroidRuntime(7267): at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:1265)
01-11 16:38:43.644: E/AndroidRuntime(7267): at android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:989)
01-11 16:38:43.644: E/AndroidRuntime(7267): at android.view.ViewRootImpl$TraversalRunnable.run(ViewRootImpl.java:4351)
01-11 16:38:43.644: E/AndroidRuntime(7267): at android.view.Choreographer$CallbackRecord.run(Choreographer.java:749)
01-11 16:38:43.644: E/AndroidRuntime(7267): at android.view.Choreographer.doCallbacks(Choreographer.java:562)
01-11 16:38:43.644: E/AndroidRuntime(7267): at android.view.Choreographer.doFrame(Choreographer.java:532)
01-11 16:38:43.644: E/AndroidRuntime(7267): at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:735)
01-11 16:38:43.644: E/AndroidRuntime(7267): at android.os.Handler.handleCallback(Handler.java:725)
01-11 16:38:43.644: E/AndroidRuntime(7267): at android.os.Handler.dispatchMessage(Handler.java:92)
01-11 16:38:43.644: E/AndroidRuntime(7267): at android.os.Looper.loop(Looper.java:137)
01-11 16:38:43.644: E/AndroidRuntime(7267): at android.app.ActivityThread.main(ActivityThread.java:5039)
01-11 16:38:43.644: E/AndroidRuntime(7267): at java.lang.reflect.Method.invokeNative(Native Method)
01-11 16:38:43.644: E/AndroidRuntime(7267): at java.lang.reflect.Method.invoke(Method.java:511)
01-11 16:38:43.644: E/AndroidRuntime(7267): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
01-11 16:38:43.644: E/AndroidRuntime(7267): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)
01-11 16:38:43.644: E/AndroidRuntime(7267): at dalvik.system.NativeStart.main(Native Method)
从数据库加载
public ArrayList<HomeworkItem> getHomeworks() {
String[] columns = new String[] { KEY_ROWID, KEY_TITLE, KEY_SUBJECT,
KEY_DUE_DAY, KEY_DUE_MONTH, KEY_DUE_YEAR, KEY_NOTES,
KEY_REMINDER_ONE, KEY_REMINDER_TWO };
Cursor c = ourDatabase.query(DATABASE_TABLE, columns, null, null, null,
null, null);
ArrayList<HomeworkItem> hwks = new ArrayList<HomeworkItem>();
int id = c.getColumnIndex(KEY_ROWID);
int iTitle = c.getColumnIndex(KEY_TITLE);
int iSub = c.getColumnIndex(KEY_SUBJECT);
int iDay = c.getColumnIndex(KEY_DUE_DAY);
int iMonth = c.getColumnIndex(KEY_DUE_MONTH);
int iYear = c.getColumnIndex(KEY_DUE_YEAR);
int iNotes = c.getColumnIndex(KEY_NOTES);
int iOne = c.getColumnIndex(KEY_REMINDER_ONE);
int iTwo = c.getColumnIndex(KEY_REMINDER_TWO);
for (c.moveToFirst(); !c.isAfterLast(); c.moveToNext()) {
HomeworkItem h = new HomeworkItem();
h.id = c.getInt(id);
h.title = c.getString(iTitle);
h.subject = c.getString(iSub);
h.day = Integer.parseInt(c.getString(iDay));
h.month = Integer.parseInt(c.getString(iMonth));
h.year = Integer.parseInt(c.getString(iYear));
h.notes = c.getString(iNotes);
h.late = h.isLate();
h.oneDayReminder = Boolean.parseBoolean(c.getString(iOne));
h.twoDayReminder = Boolean.parseBoolean(c.getString(iTwo));
hwks.add(h);
}
return hwks;
}
ListView 适配器
public class MyAdapter extends BaseAdapter { // adapter for list
public MyAdapter(Context c) {
}
public int getCount() {
// TODO Auto-generated method stub
Log.d("hwk", hwks.size()+" is the size");
return hwks.size();
}
public Object getItem(int position) {
// TODO Auto-generated method stub
Log.d("hwk", position+" being accessed");
return position;
}
public long getItemId(int position) {
// TODO Auto-generated method stub
Log.d("hwk", position+" being accessed");
return position;
}
public View getView(int position, View convertView, ViewGroup parent) {
// TODO Auto-generated method stub
View v = convertView; // inflate the list
TextView title, subject, dueDate, listLate;
if (v == null) {
LayoutInflater vi = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
v = vi.inflate(R.layout.list, null);
Log.d("hwk", "v inflated");
}
title = (TextView) v.findViewById(R.id.listTitle); // get the id's
// of fields
subject = (TextView) v.findViewById(R.id.listSubject);
dueDate = (TextView) v.findViewById(R.id.listDueDate);
listLate = (TextView) v.findViewById(R.id.listLate);
Log.d("hwk", "got views");
title.setText(hwks.get(position).title);
subject.setText(hwks.get(position).subject);
String date = hwks.get(position).day + "/"
+ (hwks.get(position).month + 1) + "/"
+ hwks.get(position).year;
dueDate.setText(date);
Log.d("hwk", "some text set");
if (hwks.get(position).late) { // set if they are late or not
if (hwks.get(position).isToday()) {
listLate.setTextColor(Color.rgb(255, 165, 0));
listLate.setText("Due Today!");
} else {
listLate.setTextColor(Color.RED);
listLate.setText("Late");
}
} else {
listLate.setTextColor(Color.GREEN);
listLate.setText("Ongoing");
}
Log.d("hwk", "returning v");
return v;
}
}
编辑:此外,当我第一次在应用程序中添加家庭作业时,ListView 工作,它会刷新 ListView 并将家庭作业添加到其中。发生这种情况时,我完全重新加载了主 Activity,因此在应用程序关闭一次后打开它似乎是一个问题。
编辑 2:我从应用程序中删除了数据,添加了一个新的家庭作业,现在我无法重新创建问题.. 可能是什么原因造成的?!
编辑 3:检测到问题。这与一些文本有关。我已经发现,当我将“标题”设置为“二项式展开”时,它重新创建了我之前在作业也称为“二项式展开”时遇到的错误。是否与尝试将该字符串放在 TextView 中并且文本太大有关?
我已经扩展了 TextView 的宽度,它停止了错误。当我扩展文本时,我又遇到了问题。一定是文本视图溢出的问题。我可以设置任何 TextView 属性来阻止它吗?