I have an app which loads a listview when opened, however I am getting an Array Index Out Of Bounds Exception, with no clues as to where the problem lies. It is trying to access index=-1 somewhere, but I have no idea where.
The app loads Homework information from a database, puts them into separate Homework objects in an ArrayList and loads it into the ListView from there. I really don't know where the exception is coming from, I have checked all my code. There is only 1 homework in the list, however the app refuses to open and force closes as soon as I try to open it. Here is the LogCat output
Thanks
LogCat output
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)
Loading from 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 Adapter
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;
}
}
EDIT: Also, the ListView works when I first add a Homework within the app, and it goes and refreshes the ListView and adds the Homework to it. I am totally reloading the main Activity when this happens, so it appears to be a problem opening it after the app has been closed once.
EDIT 2: I deleted the data from the app, added a new Homework and now I cannot recreate the problem.. What could have caused this?!
EDIT 3: PROBLEM DETECTED. It is to do with some of the text. I have worked out that when I set the 'title' to "Binomial Expansion" it recreated the error that I had before when the homework was also called "Binomial Expansion". Could it be to do with trying to put that String in the TextView and the text being too large?
I have extended the width of the TextView and it stopped the error. When I extended the text I got the issue again. It must be a problem with the text view overflowing. Are there any TextView properties I can set to stop this?
This is caused by setting android:ellipsize="middle"
(or start
) on a TextView
that does not have android:singleLine="true"
on.
For example, the following crashes on this error:
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:ellipsize="middle"
android:maxLines="1"
android:text="blahblahblahblahblahblahblahblahblahbla---go really long" />
But the following does not:
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:ellipsize="middle"
android:singleLine="true"
android:text="blahblahblahblahblahblahblahblahblahbla---go really long" />
Reference: https://code.google.com/p/android/issues/detail?id=33868
Note: singleLine
is a deprecated attribute but still works on Jelly Bean.
your problem is in the getView method , this method will be called whenever u scroll through your array so make sure u check if the view is not null before doing any logic there.
Check your layouts and cursor column name array lengths in the CursorAdapter's constructor. When the two array's length mismatch, it causes ArrayOutOfBoundsException
.
This is definitely related to text view ellipsize logic, It has nothing to do with adapter code.
来源:https://stackoverflow.com/questions/14282757/android-list-view-array-index-out-of-bounds-exception-no-clues