ListView在使用IndexOutOfBoundsException

时间:2015-12-28 08:38:09

标签: java android user-interface listview

当我滚动这个ListView时,我得到一个indexOutOfRangeException。我不知道出了什么问题,除了它与addScrapView有关,如我的LogCat所示。我一般都知道导致IndexOutOfRangeException的原因,但我不明白ListView导致它的原因。我不明白为什么它会导致错误,并且解释错误的来源会有所帮助。我不得不重写getViewTypeCount来正确设置属性。

public class InfoActivity extends AppCompatActivity {


public int Position = -1;


private static word[] removeElement(word[] words1, int counter) {
    if(counter>0) {

        word[] copy = new word[words1.length-1];
        System.arraycopy(words1, 0, copy, 0, counter);
        System.arraycopy(words1, counter+1, copy, counter, words1.length-counter-1);
        return copy;
    }

    return null;
}


// Toast.makeText(getApplicationContext(),"Ran in: " + millis +" milliseconds",Toast.LENGTH_LONG).show();}

private static word[] findSentencewords(String sentence) {
    boolean[] chars = new boolean[26];
    word[] words1={new word('A')};
    for (int i=0;i<26;i++)
        chars[i]=sentence.contains(String.valueOf((char)('A'+i)));
    for (int i=1; i<26;i++)
        if (chars[i])
        {
            if(words1 != null) {
                words1 = Arrays.copyOf(words1, words1.length + 1);
                words1[words1.length - 1] =words[i];
            }
            else {
                words1[0]=words[0];

            }
        }
    if (!chars[0]) words1= Arrays.copyOfRange(words1,1,words1.length);
    return words1;
}


public void kill(View view) {
    try {
        Intent i = new Intent();
        setResult(RESULT_OK, i);
        i.putExtra("Count", word.count);
        finish();
    } catch (Throwable throwable) {
        throwable.printStackTrace();
    }
}


    public void InfowordAdd(View view) {

    word word = new word(word.idents.charAt(word.count++));
    MainActivity.words[word.ID]=word;
    //Activity act = this;
    //this.recreate()mParts=mParts2;
    mParts.add(word.count-1,word);
    arrayAdapter.notifyDataSetChanged();
}

public void doNote(View view) {
    int i = 0;
    if (notes==null) {
        notes=new word[1];
        notes[0]=new word((char)(255));
        notes[0].setName('*');
    }
    else
    {
        i=notes.length;
        notes=Arrays.copyOf(notes,i+1);
        notes[i]=new word((char)255);
        notes[i].setName('*');
    }
    mParts.add(notes[i]);
    arrayAdapter.notifyDataSetChanged();
}

public void doDelete(View view) {
    if (Position >-1) {
        mParts.remove(Position);
        Position=-1;
        if(Position<=25){
            word.count-=1;
            MainActivity.words=removeElement(MainActivity.words,Position);}
        arrayAdapter.notifyDataSetChanged();
    }
    else Toast.makeText(this,"Nothing selected",Toast.LENGTH_LONG).show();
}


class wordViewer extends ArrayAdapter<word> {
    private ArrayList<word> objects;

    public wordViewer(Context context, int resource, ArrayList objects) {
        super(context, resource, objects);
        this.objects = objects;
//        this.context = context;
    }
    @Override

    public int getViewTypeCount() {

        return mParts.size()-1;
    }

    @Override
    public int getItemViewType(int position) {

        return position;
    }
    @Override
    public View getView(int position, View convertView, final ViewGroup parent) {

        // assign the view we are converting to a local variable
        View v = convertView;
        final ViewHolder holder;

        // first check to see if the view is null. if so, we have to inflate it.
        // to inflate it basically means to render, or show, the view.
        if (v == null) {
            holder=new ViewHolder();

            LayoutInflater inflater = (LayoutInflater) getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            convertView = inflater.inflate(R.layout.word_view, null);
            holder.ref=position;

            holder.editText1= (EditText) convertView.findViewById(R.id.definition);
            CheckBox tb = (CheckBox) convertView.findViewById(R.id.trueBox);
            CheckBox fb = (CheckBox) convertView.findViewById(R.id.falseBox);
            holder.tb=tb;
            holder.fb=fb;
            holder.label=(TextView)convertView.findViewById(R.id.wordName);
            convertView.setTag(holder);
        } else holder= (ViewHolder) convertView.getTag();
        v=convertView;

        TextView name = (TextView) v.findViewById(R.id.wordName);


        word i = objects.get(position);


        holder.editText1.setText(e.valueAt(position));
        holder.editText1.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {

            }

            @Override
            public void afterTextChanged(Editable s) {
                e.setValueAt(holder.ref, s);

            }
        });
        {

            holder.fb.setChecked(d.get(holder.ref));
            holder.tb.setChecked(c.get(holder.ref));

/*        boolean def = c.get(position);
    if (def&&i.valueSet) {
        holder.fb.setChecked(false);
        holder.tb.setChecked(true);

    }
    else if(!def)
    {
        holder.fb.setChecked(true);
        holder.tb.setChecked(false);
    }*/
            CharSequence t = String.valueOf(i.getName());
            name.setText(t);
            if (i.dictionaryValue != null)     holder.editText1.setText(i.dictionaryValue);
        }


        return v;

    }




}

public ArrayList<word> mParts = new ArrayList<word>();
public static word[] words;
public word[] notes;
public wordViewer mAdapter;
public ListView listView;
public static SparseArray<Editable> e = new SparseArray<>(26);
public static SparseBooleanArray c = new SparseBooleanArray(26);
public static SparseBooleanArray d = new SparseBooleanArray(26);
public static View rootview;
public static     String args;
public static    String concs;
public   ArrayAdapter<word> arrayAdapter;

@Override
protected void onRestoreInstanceState(Bundle savedInstanceState) {
    super.onRestoreInstanceState(savedInstanceState);
    if(savedInstanceState!=null) {
        notes = (word[]) savedInstanceState.getParcelableArray("notes");
        int counter=0;
        argParse= ParserResponse.ParseSentence(makeParsable(args), 0);
        concParse=ParserResponse.ParseSentence(makeParsable(concs),0);

        for (word a:words
                ) {
            if(a!=null) {
                if (a.valueSet) c.put(counter, a.immutabledef);
                else c.put(counter, a.def);
                if (a.valueSet) d.put(counter, !a.immutabledef);
                else d.put(counter, !a.def);
                counter++;
                mParts.add(a);

            }

        }
        if (notes!=null)for (word e:notes
                ) {
            if(e!=null) {
                if (e.valueSet) c.put(counter, e.immutabledef);
                else c.put(counter, e.def);
                if (e.valueSet) d.put(counter, !e.immutabledef);
                else d.put(counter, !e.def);
                counter++;
                mParts.add(e);
            }


        }



        arrayAdapter = new wordViewer(this, R.layout.word_view,mParts);
        listView= (ListView) findViewById(R.id.list);
        listView.setAdapter(arrayAdapter);
        listView.getCheckedItemPositions();
        listView.setOnItemLongClickListener(new AdapterView.OnItemLongClickListener() {
            @Override
            public boolean onItemLongClick(AdapterView<?> arg0, View view, int position, long arg3) {
                listView.setSelection(position);
                Position=position;
                Toast.makeText(getBaseContext(),String.valueOf(position)+" selected",Toast.LENGTH_LONG).show();
                Log.d("Selected: ",String.valueOf(position));
                return false;
            }
        });
    }

}

@Override
protected void onSaveInstanceState(Bundle outState) {
    super.onSaveInstanceState(outState);
    outState.putParcelableArray("notes", notes);
}

@Override
protected void onCreate(Bundle stuff) {
    super.onCreate(stuff);


    setContentView(R.layout.activity_info);
    Intent i = getIntent();
    rootview = getWindow().getDecorView();

    Bundle b = i.getExtras();
    words = MainActivity.words;
    args = b.getString("Argument");
    concs = b.getString("Conclusion");
    TextView tv = (TextView) findViewById(R.id.argText);
    tv.setText(args);
    tv = (TextView) findViewById(R.id.concText);
    tv.setText(concs);
    tv = (TextView) findViewById(R.id.valid);
    tv.setText(b.getString("Validity"));
    tv = (TextView) findViewById(R.id.sound);
    tv.setText(b.getString("Soundness"));
    //int index = 0;
    argParse = ParserResponse.ParseSentence(makeParsable(args), 0);
    concParse = ParserResponse.ParseSentence(makeParsable(concs), 0);
    if (stuff == null) {
        int counter = 0;
        for (word a : words
                ) {
            if (a != null) {
                if (a.valueSet) c.put(counter, a.immutabledef);
                else c.put(counter, a.def);
                if (a.valueSet) d.put(counter, !a.immutabledef);
                else d.put(counter, !a.def);
                counter++;
                mParts.add(a);


            }
            if (notes != null) for (word e : notes
                    ) {
                mParts.add(e);

            }


        }
        arrayAdapter = new wordViewer(this, R.layout.word_view, mParts);
        listView = (ListView) findViewById(R.id.list);
        listView.setAdapter(arrayAdapter);
        listView.getCheckedItemPositions();
        listView.setOnItemLongClickListener(new AdapterView.OnItemLongClickListener() {
            @Override
            public boolean onItemLongClick(AdapterView<?> arg0, View view, int position, long arg3) {
                listView.setSelection(position);
                Position = position;
                Toast.makeText(getBaseContext(), String.valueOf(position) + " selected", Toast.LENGTH_LONG).show();
                Log.d("Selected: ", String.valueOf(position));
                return false;
            }
        });


    }
}
private static class ViewHolder {
    EditText editText1;
    TextView label;
    int ref;
}

`
我的LogCat在这里: java.lang.ArrayIndexOutOfBoundsException:length = 2;索引= 2                                                                                         在android.widget.AbsListView $ RecycleBin.addScrapView(AbsListView.java:6643)                                                                                         在android.widget.ListView.layoutChildren(ListView.java:1643)                                                                                         在android.widget.AbsListView.onLayout(AbsListView.java:2148)                                                                                         在android.view.View.layout(View.java:16630)                                                                                         在android.view.ViewGroup.layout(ViewGroup.java:5437)                                                                                         在android.widget.RelativeLayout.onLayout(RelativeLayout.java:1079)                                                                                         在android.view.View.layout(View.java:16630)                                                                                         在android.view.ViewGroup.layout(ViewGroup.java:5437)                                                                                         在android.widget.FrameLayout.layoutChildren(FrameLayout.java:336)                                                                                         在android.widget.FrameLayout.onLayout(FrameLayout.java:273)                                                                                         在android.view.View.layout(View.java:16630)                                                                                         在android.view.ViewGroup.layout(ViewGroup.java:5437)                                                                                         在android.support.v7.widget.ActionBarOverlayLayout.onLayout(ActionBarOverlayLayout.java:435)                                                                                         在android.view.View.layout(View.java:16630)                                                                                         在android.view.ViewGroup.layout(ViewGroup.java:5437)                                                                                         在android.widget.FrameLayout.layoutChildren(FrameLayout.java:336)                                                                                         在android.widget.FrameLayout.onLayout(FrameLayout.java:273)                                                                                         在android.view.View.layout(View.java:16630)                                                                                         在android.view.ViewGroup.layout(ViewGroup.java:5437)                                                                                         在android.widget.LinearLayout.setChildFrame(LinearLayout.java:1743)                                                                                         在android.widget.LinearLayout.layoutVertical(LinearLayout.java:1586)                                                                                         在android.widget.LinearLayout.onLayout(LinearLayout.java:1495)                                                                                         在android.view.View.layout(View.java:16630)                                                                                         在android.view.ViewGroup.layout(ViewGroup.java:5437)                                                                                         在android.widget.FrameLayout.layoutChildren(FrameLayout.java:336)                                                                                         在android.widget.FrameLayout.onLayout(FrameLayout.java:273)                                                                                         在com.android.internal.policy.PhoneWindow $ DecorView.onLayout(PhoneWindow.java:2678)                                                                                         在android.view.View.layout(View.java:16630)                                                                                         在android.view.ViewGroup.layout(ViewGroup.java:5437)                                                                                         在android.view.ViewRootImpl.performLayout(ViewRootImpl.java:2171)                                                                                         在android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:1931)                                                                                         在android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:1107)                                                                                         在android.view.ViewRootImpl $ TraversalRunnable.run(ViewRootImpl.java:6013)                                                                                         在android.view.Choreographer $ CallbackRecord.run(Choreographer.java:858)                                                                                         在android.view.Choreographer.doCallbacks(Choreographer.java:670)                                                                                         在android.view.Choreographer.doFrame(Choreographer.java:606)                                                                                         在android.view.Choreographer $ FrameDisplayEventReceiver.run(Choreographer.java:844)                                                                                         在android.os.Handler.handleCallback(Handler.java:739)                                                                                         在android.os.Handler.dispatchMessage(Handler.java:95)                                                                                         在android.os.Looper.loop(Looper.java:148)

0 个答案:

没有答案