我有一个应用程序在打开时加载列表视图,但是我得到一个数组索引超出界限异常,没有关于问题所在的线索。它试图在某处访问index = -1,但我不知道在哪里。
应用程序从数据库加载家庭作业信息,将它们放入ArrayList中的单独Homework对象中,然后从那里将其加载到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)
从DB加载
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在我第一次在应用程序中添加一个Homework时工作,然后它会刷新ListView并将Homework添加到它。当发生这种情况时,我完全重新加载主Activity,因此在应用程序关闭一次后打开它似乎是一个问题。
编辑2:我从应用程序中删除了数据,添加了一个新的Homework,现在我无法重新创建问题..这可能是什么造成的?!
编辑3:检测到问题。这与一些文字有关。我已经知道,当我将“标题”设置为“二项式扩展”时,它重新创建了我之前所谓的“二项式扩展”的错误。是否可以尝试将该String放在TextView中并且文本太大?
我扩展了TextView的宽度并停止了错误。当我扩展文本时,我再次遇到了问题。文本视图溢出一定是个问题。我可以设置任何TextView属性来阻止它吗?
答案 0 :(得分:10)
这是因为android:ellipsize="middle"
(或start
)设置了TextView
上没有android:singleLine="true"
的原因。
例如,以下错误导致此错误:
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:ellipsize="middle"
android:maxLines="1"
android:text="blahblahblahblahblahblahblahblahblahbla---go really long" />
但以下情况并非如此:
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:ellipsize="middle"
android:singleLine="true"
android:text="blahblahblahblahblahblahblahblahblahbla---go really long" />
参考:https://code.google.com/p/android/issues/detail?id=33868
注意:singleLine
是一个已弃用的属性,但仍适用于Jelly Bean。
答案 1 :(得分:0)
你的问题出在getView方法中,每当你滚动数组时都会调用这个方法,所以在做任何逻辑之前一定要检查视图是否为null。
答案 2 :(得分:0)
在CursorAdapter的constructor中检查布局和光标列名称数组的长度。当两个数组的长度不匹配时,会导致ArrayOutOfBoundsException
。
答案 3 :(得分:0)
这绝对与文本视图ellipsize逻辑有关,它与适配器代码无关。