gpt4 book ai didi

android - notifyDataSetChanged() 导致 IndexOutOfBoundsException

转载 作者:行者123 更新时间:2023-12-03 23:59:14 35 4
gpt4 key购买 nike

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

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.
}

适配器类是,

@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)

最佳答案

我的问题是通过实现 ArrayAdaptergetCount() 方法解决的,这有助于 ArrayAdapter 知道要调用多少对象getView() 用于,一旦数据发生变化。

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

关于android - notifyDataSetChanged() 导致 IndexOutOfBoundsException,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29124850/

35 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com