Android List View Array Index Out Of Bounds Exception - no hints?

I have an application that loads listview upon opening, however I get the Array Index Out Of Bounds Exception parameter, having no idea where the problem is. He is trying to access index = -1 somewhere, but I have no idea where.

The application loads the homework information from the database, puts it in separate Homework objects in the ArrayList, and loads it into the ListView. I really don't know where the exception comes from, I checked all my code. There is only 1 homework in the list, however the application refuses to open and force close as soon as I try to open it. Here is the output of LogCat

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) 

Download from database

 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, ListView works when I first add homework to the application, and it goes and updates ListView and adds homework to it. I completely reload the main activity when this happens, so the problem is opening it after the application is closed.

EDIT 2: I deleted the data from the application, added a new homework, and now I can not recreate the problem. What could cause this ?!

EDIT 3: PROBLEM DETECTED. This is due to some texts. I decided that when I set the “title” to “Binomial extension”, he recreated the error that I had before, when my homework was also called “Binomial extension”. Could this be due to an attempt to put this line in a TextView and text too large?

I expanded the width of the TextView and stopped the error. When I expanded the text, I again got a question. This should be a problem when overflowing a text view. Are there any TextView properties that I can set to stop this?

+4
source share
4 answers

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 failures in 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:

 <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:ellipsize="middle" android:singleLine="true" android:text="blahblahblahblahblahblahblahblahblahbla---go really long" /> 

Link: https://code.google.com/p/android/issues/detail?id=33868

Note. singleLine is an obsolete attribute, but still runs on Bean jelly.

+9
source

your problem is in the getView method, this method will be called whenever u scrolls your array, so make sure you check to see if the view is invalid before doing any logic.

0
source

Check the layout sizes and column lengths of the column column in the CursorAdapter constructor. If the length of the array does not ArrayOutOfBoundsException it raises an ArrayOutOfBoundsException .

0
source

This is definitely related to the ellipsis logic with the text representation, it has nothing to do with the adapter code.

0
source

All Articles