notifyDataSetChanged() 导致 IndexOutOfBoundsException

notifyDataSetChanged() causes IndexOutOfBoundsException

这是我的代码的简化版本。

public class MainActivity extends ActionBarActivity {

private ArrayList<String> entry = new ArrayList<String>();
private String[] entryString = new String[11];
private ArrayAdapter<String> aa;
private ListView entryListView;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    entryListView = (ListView) findViewById(R.id.listView1);
    int listID = R.layout.entry_layout;
    aa = new EntryAdapter(this, listID, entry);
    entryListView.setAdapter(aa);
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    // Handle action bar item clicks here. The action bar will
    // automatically handle clicks on the Home/Up button, so long
    // as you specify a parent activity in AndroidManifest.xml.
    int id = item.getItemId();
    switch (id) {
    case R.id.action_add: {
        Intent intent = new Intent(MainActivity.this, AddActivity.class);
        startActivityForResult(intent, 1);
        break;
    }
    }
    return super.onOptionsItemSelected(item);
}

@Override
public void onActivityResult(int rc, int resc, Intent i) {
    super.onActivityResult(rc, resc, i);
    entryString = i.getStringArrayExtra("Entry");
    this.onNewItemAdded(entryString);
}

public void onNewItemAdded(String[] _entry){
    String key = new String();
    key = _entry[0];
    entry.add(key);
    aa.notifyDataSetChanged();   //Runs without crashing when this is removed.
}

适配器 class 是,

@SuppressLint("Instantiatable")
public class EntryAdapter extends ArrayAdapter<String>{

private ArrayList<String> entries;
int count;
int resource;
public EntryAdapter(Context _context, int _resource, List<String> _entries){
    //TODO Auto-generated constructor stub
    super(_context, _resource, R.id.key, _entries);
    this.resource = _resource;
    this.entries = new ArrayList<String>(_entries);
    this.count = 0;
}

@Override
public View getView(int position, View convertView, ViewGroup parent) {
    View row = convertView;

    if (convertView == null) {
        String inflater = Context.LAYOUT_INFLATER_SERVICE;
        LayoutInflater li;
        li = (LayoutInflater) getContext().getSystemService(inflater);
        row = li.inflate(resource, parent, false);
    }

    TextView keyView = (TextView) row.findViewById(R.id.key);
    TextView siteView = (TextView) row.findViewById(R.id.site);

    for(int c=0; c<11; c++){
        while(!entries.get(c).toString().equals("")){
            count++;
        }
    }

    keyView.setText(entries.get(position));
    siteView.setText(count + "sources");

    return row;
}
} 

通过注释 MainActivity 中的 aa.notifyDataSetChanged() 调用,应用程序运行时不会崩溃但抛出

IndexOutOfBoundsException: Invalid index 0, size is 0

未评论时

PS: XML 完全没问题。

编辑:

这是日志

03-18 18:38:03.840: E/AndroidRuntime(6052): FATAL EXCEPTION: main 03-18 18:38:03.840: E/AndroidRuntime(6052): Process: com.FirstClass.alert, PID: 6052 03-18 18:38:03.840: E/AndroidRuntime(6052): java.lang.IndexOutOfBoundsException: Invalid index 0, size is 0 03-18 18:38:03.840: E/AndroidRuntime(6052): at java.util.ArrayList.throwIndexOutOfBoundsException(ArrayList.java:255) 03-18 18:38:03.840: E/AndroidRuntime(6052): at java.util.ArrayList.get(ArrayList.java:308) 03-18 18:38:03.840: E/AndroidRuntime(6052): at com.FirstClass.netalert.EntryAdapter.getView(EntryAdapter.java:66) 03-18 18:38:03.840: E/AndroidRuntime(6052): at android.widget.AbsListView.obtainView(AbsListView.java:2263) 03-18 18:38:03.840: E/AndroidRuntime(6052): at android.widget.ListView.makeAndAddView(ListView.java:1790) 03-18 18:38:03.840: E/AndroidRuntime(6052): at android.widget.ListView.fillDown(ListView.java:691) 03-18 18:38:03.840: E/AndroidRuntime(6052): at android.widget.ListView.fillFromTop(ListView.java:752) 03-18 18:38:03.840: E/AndroidRuntime(6052): at android.widget.ListView.layoutChildren(ListView.java:1630) 03-18 18:38:03.840: E/AndroidRuntime(6052): at android.widget.AbsListView.onLayout(AbsListView.java:2091) 03-18 18:38:03.840: E/AndroidRuntime(6052): at android.view.View.layout(View.java:14822) 03-18 18:38:03.840: E/AndroidRuntime(6052): at android.view.ViewGroup.layout(ViewGroup.java:4631) 03-18 18:38:03.840: E/AndroidRuntime(6052): at android.widget.RelativeLayout.onLayout(RelativeLayout.java:1055) 03-18 18:38:03.840: E/AndroidRuntime(6052): at android.view.View.layout(View.java:14822) 03-18 18:38:03.840: E/AndroidRuntime(6052): at android.view.ViewGroup.layout(ViewGroup.java:4631) 03-18 18:38:03.840: E/AndroidRuntime(6052): at android.widget.FrameLayout.layoutChildren(FrameLayout.java:453) 03-18 18:38:03.840: E/AndroidRuntime(6052): at android.widget.FrameLayout.onLayout(FrameLayout.java:388) 03-18 18:38:03.840: E/AndroidRuntime(6052): at android.view.View.layout(View.java:14822) 03-18 18:38:03.840: E/AndroidRuntime(6052): at android.view.ViewGroup.layout(ViewGroup.java:4631) 03-18 18:38:03.840: E/AndroidRuntime(6052): at android.support.v7.internal.widget.ActionBarOverlayLayout.onLayout(ActionBarOverlayLayout.java:502) 03-18 18:38:03.840: E/AndroidRuntime(6052): at android.view.View.layout(View.java:14822) 03-18 18:38:03.840: E/AndroidRuntime(6052): at android.view.ViewGroup.layout(ViewGroup.java:4631) 03-18 18:38:03.840: E/AndroidRuntime(6052): at android.widget.FrameLayout.layoutChildren(FrameLayout.java:453) 03-18 18:38:03.840: E/AndroidRuntime(6052): at android.widget.FrameLayout.onLayout(FrameLayout.java:388) 03-18 18:38:03.840: E/AndroidRuntime(6052): at android.view.View.layout(View.java:14822) 03-18 18:38:03.840: E/AndroidRuntime(6052): at android.view.ViewGroup.layout(ViewGroup.java:4631) 03-18 18:38:03.840: E/AndroidRuntime(6052): at android.widget.LinearLayout.setChildFrame(LinearLayout.java:1671) 03-18 18:38:03.840: E/AndroidRuntime(6052): at android.widget.LinearLayout.layoutVertical(LinearLayout.java:1525) 03-18 18:38:03.840: E/AndroidRuntime(6052): at android.widget.LinearLayout.onLayout(LinearLayout.java:1434) 03-18 18:38:03.840: E/AndroidRuntime(6052): at android.view.View.layout(View.java:14822) 03-18 18:38:03.840: E/AndroidRuntime(6052): at android.view.ViewGroup.layout(ViewGroup.java:4631) 03-18 18:38:03.840: E/AndroidRuntime(6052): at android.widget.FrameLayout.layoutChildren(FrameLayout.java:453) 03-18 18:38:03.840: E/AndroidRuntime(6052): at android.widget.FrameLayout.onLayout(FrameLayout.java:388) 03-18 18:38:03.840: E/AndroidRuntime(6052): at android.view.View.layout(View.java:14822) 03-18 18:38:03.840: E/AndroidRuntime(6052): at android.view.ViewGroup.layout(ViewGroup.java:4631) 03-18 18:38:03.840: E/AndroidRuntime(6052): at android.view.ViewRootImpl.performLayout(ViewRootImpl.java:1987) 03-18 18:38:03.840: E/AndroidRuntime(6052): at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:1744) 03-18 18:38:03.840: E/AndroidRuntime(6052): at android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:1000) 03-18 18:38:03.840: E/AndroidRuntime(6052): at android.view.ViewRootImpl$TraversalRunnable.run(ViewRootImpl.java:5670) 03-18 18:38:03.840: E/AndroidRuntime(6052): at android.view.Choreographer$CallbackRecord.run(Choreographer.java:761) 03-18 18:38:03.840: E/AndroidRuntime(6052): at android.view.Choreographer.doCallbacks(Choreographer.java:574) 03-18 18:38:03.840: E/AndroidRuntime(6052): at android.view.Choreographer.doFrame(Choreographer.java:544) 03-18 18:38:03.840: E/AndroidRuntime(6052): at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:747) 03-18 18:38:03.840: E/AndroidRuntime(6052): at android.os.Handler.handleCallback(Handler.java:733) 03-18 18:38:03.840: E/AndroidRuntime(6052): at android.os.Handler.dispatchMessage(Handler.java:95) 03-18 18:38:03.840: E/AndroidRuntime(6052): at android.os.Looper.loop(Looper.java:136) 03-18 18:38:03.840: E/AndroidRuntime(6052): at android.app.ActivityThread.main(ActivityThread.java:5021) 03-18 18:38:03.840: E/AndroidRuntime(6052): at java.lang.reflect.Method.invokeNative(Native Method) 03-18 18:38:03.840: E/AndroidRuntime(6052): at java.lang.reflect.Method.invoke(Method.java:515) 03-18 18:38:03.840: E/AndroidRuntime(6052): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:827) 03-18 18:38:03.840: E/AndroidRuntime(6052): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:643) 03-18 18:38:03.840: E/AndroidRuntime(6052): at dalvik.system.NativeStart.main(Native Method)

如果您通过调用 new ArrayList<String>(_entries); 创建新的 ArrayList,您 activity 中的 ArrayList 将不会被适配器中的 ArrayList 引用。因此,当您更改 activity 中的条目 Arraylist 时,它不会影响 Adapters ArrayList。将构造函数更改为:

public EntryAdapter(Context _context, int _resource, List<String> _entries){
    super(_context, _resource, R.id.key, _entries);
    this.resource = _resource;
    this.entries = _entries;
    this.count = 0;
}

或将 onNewItemAdded 更改为:

public void onNewItemAdded(String[] _entry){
    String key = new 
    key = _entry[0];
    aa.add(key);
    aa.notifyDataSetChanged();
}

我的问题是通过实施 ArrayAdaptergetCount() 方法解决的,这有助于 ArrayAdapter 知道要为多少对象调用 getView(),一旦数据已更改。

@Override
public int getCount()
{
    return data.size();
}