Android列表视图数组索引超出界限异常 - 没有线索? [英] Android List View Array Index Out Of Bounds Exception - No clues?

查看:362
本文介绍了Android列表视图数组索引超出界限异常 - 没有线索?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个应用程序在打开时加载列表视图,但是我得到一个Array Index Out Of Bounds Exception,没有关于问题所在的线索。它尝试在某个地方访问index = -1,但我不知道在哪里。



应用程序从数据库加载家庭作业信息,将它们放入单独的家庭作业对象中ArrayList并将其加载到ListView中。我真的不知道异常来自哪里,我检查了我的所有代码。列表中只有1个作业,但是当我尝试打开它时,应用程序拒绝打开并强制关闭。这是LogCat输出



谢谢



LogCat输出



主要
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):在android.text.StaticLayout.calculateEllipsis(StaticLayout.java:738)
01-11 16:38: 43.644:E / AndroidRuntime(7267):android.text.StaticLayout.out(StaticLayout.java:702)
01-11 16:38:43.644:E / AndroidRuntime(7267):在android.text.StaticLayout .generate(StaticLayout.java:410)
01-11 16:38:43.644:E / AndroidRuntime(7267):android.text.StaticLayout。< init>(StaticLayout.java:140)
01-11 16:38:43.644:E / AndroidRuntime(7267):android.widget.TextView.makeSingleLayout(TextView.java:5888)
01-11 16:38:43.644:E / AndroidRuntime(7267 ):在android.widget.TextView.makeNewLayout(TextView.java:5745)
01-11 16:38:43.644:E / AndroidRuntime(7267):在android.widget.TextView.onMeasure(TextView.java: 6102)
01-11 16:38:43.644:E / AndroidRuntime(7267):在android.view.View.measure(View.java:15513)
01-11 16:38:43.644: E / AndroidRuntime(7267):在android.widget.RelativeLayout.m easureChildHorizo​​ntal(RelativeLayout.java:645)
01-11 16:38:43.644:E / AndroidRuntime(7267):在android.widget.RelativeLayout.onMeasure(RelativeLayout.java:425)
01-11 16:38:43.644:E / AndroidRuntime(7267):android.view.View.measure(View.java:15513)
01-11 16:38:43.644:E / AndroidRuntime(7267) .view.ViewGroup.measureChildWithMargins(ViewGroup.java:4827)
01-11 16:38:43.644:E / AndroidRuntime(7267):android.widget.LinearLayout.measureChildBeforeLayout(LinearLayout.java:1404)
01-11 16:38:43.644:E / AndroidRuntime(7267):android.widget.LinearLayout.measureHorizo​​ntal(LinearLayout.java:1052)
01-11 16:38:43.644:E / AndroidRuntime( 7267):在android.widget.LinearLayout.onMeasure(LinearLayout.java:590)
01-11 16:38:43.644:E / AndroidRuntime(7267):在android.view.View.measure(View.java :15513)
01-11 16:38:43.644:E / AndroidRuntime(7267):在android.widget.ListView.measureScrapChild(ListView.java:1183)
01-11 16:38 :43.644:E / AndroidRuntime(7267):在android.widget.ListView.measureHeightOfChildren(ListView.java:1248)
01-11 16:38:43.644:E / AndroidRuntime(7267):在android.widget。 ListView.onMeasure(ListView.java:1158)
01-11 16:38:43.644:E / AndroidRuntime(7267):在android.view.View.measure(View.java:15513)
01 -11 16:38:43.644:E / AndroidRuntime(7267):android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:4827)
01-11 16:38:43.644:E / AndroidRuntime(7267):在android.widget.LinearLayout.measureChildBeforeLayout(LinearLayout.java:1404)
01-11 16:38:43.644:E / AndroidRuntime(7267):在android.widget.LinearLayout.measureVertical(LinearLayout.java:695)
01-11 16:38:43.644:E / AndroidRuntime(7267):android.widget.LinearLayout.onMeasure(LinearLayout.java:588)
01-11 16:38:43.644:E / AndroidRuntime(7267):在android.view.View.measure(View.java:15513)
01-11 16:38:43.644:E / AndroidRuntime(7267):在android.view.ViewGroup.measureC hildWithMargins(ViewGroup.java:4827)
01-11 16:38:43.644:E / AndroidRuntime(7267):在android.widget.FrameLayout.onMeasure(FrameLayout.java:310)
01-11 16:38:43.644:E / AndroidRuntime(7267):android.view.View.measure(View.java:15513)
01-11 16:38:43.644:E / AndroidRuntime(7267) .widget.LinearLayout.measureVertical(LinearLayout.java:847)
01-11 16:38:43.644:E / AndroidRuntime(7267):在android.widget.LinearLayout.onMeasure(LinearLayout.java:588)
01-11 16:38:43.644:E / AndroidRuntime(7267):android.view.View.measure(View.java:15513)
01-11 16:38:43.644:E / AndroidRuntime 7267):在android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:4827)
01-11 16:38:43.644:E / AndroidRuntime(7267):在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):在android.view.View.measure(View.java:15513)
01-11 16:38:43.644:E / AndroidRuntime(7267):在android.view.ViewRootImpl.performMeasure(ViewRootImpl .java:1874)
01-11 16:38:43.644:E / AndroidRuntime(7267):在android.view.ViewRootImpl.measureHierarchy(ViewRootImpl.java:1089)
01-11 16:38 :43.644:E / AndroidRuntime(7267):在android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:1265)
01-11 16:38:43.644:E / AndroidRuntime(7267):在android.view。 ViewRootImpl.doTraversal(ViewRootImpl.java:989)
01-11 16:38:43.644:E / AndroidRuntime(7267):在android.view.ViewRootImpl $ TraversalRunnable.run(ViewRootImpl.java:4351)
01-11 16:38:43.644:E / AndroidRuntime(7267):android.view.Choreographer $ CallbackRecord.run(Choreographer.java:749)
01-11 16:38:43.644:E / AndroidRuntime (7267):在android.view.Choreographer.doCallbacks(Choreographer.java:562)
01-11 16:38:43.644:E / AndroidRuntime(7267):在android.view.C horeographer.doFrame(Choreographer.java:532)
01-11 16:38:43.644:E / AndroidRuntime(7267):android.view.Choreographer $ FrameDisplayEventReceiver.run(Choreographer.java:735)
01-11 16:38:43.644:E / AndroidRuntime(7267):android.os.Handler.handleCallback(Handler.java:725)
01-11 16:38:43.644:E / AndroidRuntime(7267 ):在android.os.Handler.dispatchMessage(Handler.java:92)
01-11 16:38:43.644:E / AndroidRuntime(7267):在android.os.Looper.loop(Looper.java: 137)
01-11 16:38:43.644:E / AndroidRuntime(7267):android.app.ActivityThread.main(ActivityThread.java:5039)
01-11 16:38:43.644: E / AndroidRuntime(7267):java.lang.reflect.Method.invokeNative(Native Method)
01-11 16:38:43.644:E / AndroidRuntime(7267):java.lang.reflect.Method。 invoke(Method.java:511)
01-11 16:38:43.644:E / AndroidRuntime(7267):在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); (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 {// list for 

public MyAdapter(Context c){

}

public int getCount(){
// TODO自动生成的方法存根
Log.d(hwk,hwks.size()+是大小);
return hwks.size();
}

public Object getItem(int position){
// TODO自动生成的方法stub
Log.d(hwk,position +正在访问 );
返回位置;
}

public long getItemId(int position){
// TODO自动生成的方法stub
Log.d(hwk,position +正在访问 );
返回位置;
}

public View getView(int position,View convertView,ViewGroup parent){
// TODO自动生成的方法stub
查看v = convertView; //膨胀列表
TextView标题,主题,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); //获取字段的
//
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 )。年;
dueDate.setText(date);
Log.d(hwk,一些文本集);

if(hwks.get(position).late){//设置是否迟到
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(正在进行);
}
Log.d(hwk,return v);
return v;
}

}

编辑:此外,ListView的作品当我第一次在应用程序中添加一个家庭作业,并且它会刷新ListView并添加作业。在这种情况下,我完全重新加载主要的Activity,所以在应用程序关闭一次后似乎是一个打开它的问题。



编辑2:我删除了数据从应用程序,添加了一个新的家庭作业,现在我无法重新创建的问题..可能会导致这个?!



编辑3:问题检测。这是与一些文本。我已经弄清楚,当我将'标题'设置为二项式扩展时,它重新创建了当作业也被称为二项扩展时,我以前的错误。可以这样做,试图将该String放在TextView中,文本太大?



我已经扩展了TextView的宽度,并且它停止了错误。当我扩展文本时,我再次遇到问题。文本视图溢出时一定是一个问题。有没有可以设置的TextView属性来阻止这个?

解决方案

这是由于设置 android:在 TextView 中没有<?code>(或开始)的ellipsize =middle code> android:singleLine =true on。



例如,以下崩溃对此错误:

 < 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。


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.

这篇关于Android列表视图数组索引超出界限异常 - 没有线索?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆