gpt4 book ai didi

java.lang.IndexOutOfBoundsException : Invalid index 2, 大小为 2 而在 Android 中删除 ListView 末尾的页脚 View ?

转载 作者:行者123 更新时间:2023-11-30 01:22:52 25 4
gpt4 key购买 nike

我正在使用 ListView 实现分页。我试图在加载我的一些数据时添加页脚 View ,并在到达 ListView 末尾的 setOnScrollListener 的最后一页时删除 ListView 末尾的页脚 View 。但是在最后一页出现崩溃时出现 IndexOutOfBoundsException 问题:无效索引 2 , 大小为 2

我在初始化列表后添加页脚 View

ListView listView;

int pageCount = 0;
View footer;@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);

listView = (ListView) findViewById(R.id.load);

// Add footer view
footer = ((LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE)).inflate(R.layout.prohressbar, null, false);
listView.addFooterView(footer);

List<All_Post> allDesc = dbhelper.getAllDescriptions(OFFSET);
for (All_Post all_Post : allDesc){
descArray.add(all_Post);
}

if (adapter == null) {
adapter = new AllPostAdapter(this, R.layout.allpostlist, descArray);
listView.setAdapter(adapter);
}
if (adapter != null) {
adapter.notifyDataSetChanged();

adapter = new AllPostAdapter(this, R.layout.allpostlist, descArray);
listView.setAdapter(adapter);
listView.invalidateViews();

final SharedPreferences sharedPreferences = PreferenceManager.getDefaultSharedPreferences(getApplicationContext());
int item_position = sharedPreferences.getInt("item_position", 0);
listView.setSelection(item_position);
}

Pegination();

这是我的分页方法

public void Pegination() {

final int activityCount = sharedPreferences.getInt("ActivityListCount", 0);
Log.e("", "activityCount in Pegination method =" + activityCount);

listView.addFooterView(footer);

final Button btnLoadMore = new Button(this);
btnLoadMore.setText("Load More ....");

listView.setOnScrollListener(new AbsListView.OnScrollListener() {
private int total;

@Override
public void onScrollStateChanged(AbsListView absListView, int scrollState) {
}

@Override
public void onScroll(AbsListView absListView, int firstItem, int visibleItemCount, final int totalItems) {

total = firstItem + visibleItemCount;
Log.e("", "total =" + total);

if (totalItems > 0 && total > 0 && total == totalItems) {
if (preLast != total) {
if (pageCount < LocalPages) {
Handler handler = new Handler();
handler.postDelayed(new Runnable() {
public void run() {
pageCount++;

List<All_Post> allDesc = dbhelper.getAllDescriptions(OFFSET_SCROLL);
for (All_Post all_Post : allDesc) {

descArray.add(all_Post);
}
if (adapter != null) {
adapter.notifyDataSetChanged();
listView.setAdapter(adapter);
listView.setSelection(totalItems);
}
}
}, 2500);
}
preLast = total;
Log.e("", "11111111111 preLast=" + preLast);
}

preLast = total;
Log.e("", "222222222222222 preLast=" + preLast);
if (pageCount == LocalPages)
{
if (preLast == total)
{
if (activityCount == 200)
{
Log.e(" 11111 In btnLoadMore ", " ActivityListCount =" + activityCount);
Log.e("", "Go to else conditions --- 1 !!!!!! pageCount=" + pageCount + " LocalPages=" + LocalPages);
pageCount = 0;

listView.removeFooterView(footer);
listView.addFooterView(btnLoadMore);
}
}
if (activityCount < 200) {
Log.e(" 222222 In btnLoadMore ", " ActivityListCount =" + activityCount);
Log.e("", "Go to else conditions --- 2 !!!!!! pageCount=" + pageCount + " LocalPages=" + LocalPages);
pageCount = 0;
listView.removeFooterView(btnLoadMore);


}
}


}
}
}

});

}

这是我的 Log Cat 错误信息。

04-25 15:48:18.780  28787-28787/com.example.tazeen.classnkk E/﹕ total =137
04-25 15:48:18.780 28787-28787/com.example.tazeen.classnkk E/﹕ 222222222222222 preLast=137
04-25 15:48:18.781 28787-28787/com.example.tazeen.classnkk E/11111 In btnLoadMore﹕ ActivityListCount =200
04-25 15:48:18.781 28787-28787/com.example.tazeen.classnkk E/﹕ Go to else conditions --- 1 !!!!!! pageCount=3 LocalPages=3
04-25 15:48:18.785 28787-28787/com.example.tazeen.classnkk D/AndroidRuntime﹕ Shutting down VM
04-25 15:48:18.788 28787-28787/com.example.tazeen.classnkk E/AndroidRuntime﹕ FATAL EXCEPTION: main
Process: com.example.tazeen.classnkk, PID: 28787
java.lang.IndexOutOfBoundsException: Invalid index 2, size is 2
at java.util.ArrayList.throwIndexOutOfBoundsException(ArrayList.java:255)
at java.util.ArrayList.get(ArrayList.java:308)
at android.widget.HeaderViewListAdapter.isEnabled(HeaderViewListAdapter.java:164)
at android.widget.ListView.dispatchDraw(ListView.java:3334)
at android.view.View.draw(View.java:15364)
at android.widget.AbsListView.draw(AbsListView.java:4434)
at android.view.View.updateDisplayListIfDirty(View.java:14242)
at android.view.View.getDisplayList(View.java:14272)
at android.view.View.draw(View.java:15057)
at android.view.ViewGroup.drawChild(ViewGroup.java:3528)
at android.view.ViewGroup.dispatchDraw(ViewGroup.java:3322)
at android.view.View.draw(View.java:15364)
at android.view.View.updateDisplayListIfDirty(View.java:14242)
at android.view.View.getDisplayList(View.java:14272)
at android.view.View.draw(View.java:15057)
at android.view.ViewGroup.drawChild(ViewGroup.java:3528)
at android.view.ViewGroup.dispatchDraw(ViewGroup.java:3322)
at android.view.View.updateDisplayListIfDirty(View.java:14234)
at android.view.View.getDisplayList(View.java:14272)
at android.view.View.draw(View.java:15057)
at android.view.ViewGroup.drawChild(ViewGroup.java:3528)
at android.view.ViewGroup.dispatchDraw(ViewGroup.java:3322)
at android.view.View.updateDisplayListIfDirty(View.java:14234)
at android.view.View.getDisplayList(View.java:14272)
at android.view.View.draw(View.java:15057)
at android.view.ViewGroup.drawChild(ViewGroup.java:3528)
at android.view.ViewGroup.dispatchDraw(ViewGroup.java:3322)
at android.view.View.updateDisplayListIfDirty(View.java:14234)
at android.view.View.getDisplayList(View.java:14272)
at android.view.View.draw(View.java:15057)
at android.view.ViewGroup.drawChild(ViewGroup.java:3528)
at android.view.ViewGroup.dispatchDraw(ViewGroup.java:3322)
at android.view.View.updateDisplayListIfDirty(View.java:14234)
at android.view.View.getDisplayList(View.java:14272)
at android.view.View.draw(View.java:15057)
at android.view.ViewGroup.drawChild(ViewGroup.java:3528)
at android.view.ViewGroup.dispatchDraw(ViewGroup.java:3322)
at com.android.internal.policy.impl.PhoneWindow$DecorView.dispatchDraw(PhoneWindow.java:2845)
at android.view.View.draw(View.java:15364)
at android.widget.FrameLayout.draw(FrameLayout.java:647)
at com.android.internal.policy.impl.PhoneWindow$DecorView.draw(PhoneWindow.java:2824)
at android.view.View.updateDisplayListIfDirty(View.java:14242)
at android.view.View.getDisplayList(View.java:14272)
at android.view.ThreadedRenderer.updateViewTreeDisplayList(ThreadedRenderer.java:267)
at android.view.ThreadedRenderer.updateRootDisplayList(ThreadedRenderer.java:273)
at android.view.ThreadedRenderer.draw(ThreadedRenderer.java:312)
at android.view.ViewRootImpl.draw(ViewRootImpl.java:2852)
at android.view.ViewRootImpl.performDraw(ViewRootImpl.java:2689)
at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:2313)
at android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:1251)
at android.view.ViewRootImpl$TraversalRunnable.run(ViewRootImpl.java:6438)
at android.view.Choreographer$CallbackRecord.run(Choreographer.java:795)
at android.view.Choreographer.doCallbacks(Choreographer.java:598)
at android.view.Choreographer.doFrame(Choreographer.java:567)
at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:781)
at android.os.Handler.handleCallback(Handler.java:810)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:189)
at android.app.ActivityThread.main(ActivityThread.java:5529)
at java.lang.reflect.Method.invoke(Native Method)
at java.lang.reflect.Method.invoke(Method.java:372)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:950)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:745)

谁能帮我解决这个问题。为什么会出现 InvallidIndexOutOfBoundException 。

最佳答案

索引值从零开始。所以数组/列表的第一个元素是 0 e 第二个是 1。你说你的数据只有 2 个元素,所以索引 2 是你没有的 3 元素。查看您的代码并正确处理限制。

在这一行,intead == use <:

if (totalItems > 0 && total > 0 && total < totalItems) {

但也要验证您的整个代码以发现其他可能的错误。

关于java.lang.IndexOutOfBoundsException : Invalid index 2, 大小为 2 而在 Android 中删除 ListView 末尾的页脚 View ?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36837957/

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