gpt4 book ai didi

android - ArrayOutOfBounds 将 addHeaderView() 与 EndlessAdapter 一起使用时

转载 作者:行者123 更新时间:2023-11-30 03:38:52 24 4
gpt4 key购买 nike

我正在使用 CommonswareEndlessAdapter。当我添加了一个 header View 时,我得到了我的 listView,我得到了这个错误和 LogCat:

04-22 17:06:15.200: E/AndroidRuntime(6401): FATAL EXCEPTION: main
04-22 17:06:15.200: E/AndroidRuntime(6401): java.lang.IndexOutOfBoundsException: Invalid index 0, size is 0
04-22 17:06:15.200: E/AndroidRuntime(6401): at java.util.ArrayList.throwIndexOutOfBoundsException(ArrayList.java:251)
04-22 17:06:15.200: E/AndroidRuntime(6401): at java.util.ArrayList.get(ArrayList.java:304)
04-22 17:06:15.200: E/AndroidRuntime(6401): at android.widget.HeaderViewListAdapter.isEnabled(HeaderViewListAdapter.java:164)
04-22 17:06:15.200: E/AndroidRuntime(6401): at android.widget.ListView.dispatchDraw(ListView.java:3207)
04-22 17:06:15.200: E/AndroidRuntime(6401): at android.view.View.draw(View.java:13715)
04-22 17:06:15.200: E/AndroidRuntime(6401): at android.widget.AbsListView.draw(AbsListView.java:3653)
04-22 17:06:15.200: E/AndroidRuntime(6401): at android.view.View.getDisplayList(View.java:12650)
04-22 17:06:15.200: E/AndroidRuntime(6401): at android.view.View.getDisplayList(View.java:12694)
04-22 17:06:15.200: E/AndroidRuntime(6401): at android.view.ViewGroup.dispatchGetDisplayList(ViewGroup.java:2910)
04-22 17:06:15.200: E/AndroidRuntime(6401): at android.view.View.getDisplayList(View.java:12588)
04-22 17:06:15.200: E/AndroidRuntime(6401): at android.view.View.getDisplayList(View.java:12694)
04-22 17:06:15.200: E/AndroidRuntime(6401): at android.view.ViewGroup.dispatchGetDisplayList(ViewGroup.java:2910)
04-22 17:06:15.200: E/AndroidRuntime(6401): at android.view.View.getDisplayList(View.java:12588)
04-22 17:06:15.200: E/AndroidRuntime(6401): at android.view.View.getDisplayList(View.java:12694)
04-22 17:06:15.200: E/AndroidRuntime(6401): at android.view.ViewGroup.dispatchGetDisplayList(ViewGroup.java:2910)
04-22 17:06:15.200: E/AndroidRuntime(6401): at android.view.View.getDisplayList(View.java:12588)
04-22 17:06:15.200: E/AndroidRuntime(6401): at android.view.View.getDisplayList(View.java:12694)
04-22 17:06:15.200: E/AndroidRuntime(6401): at android.view.ViewGroup.dispatchGetDisplayList(ViewGroup.java:2910)
04-22 17:06:15.200: E/AndroidRuntime(6401): at android.view.View.getDisplayList(View.java:12588)
04-22 17:06:15.200: E/AndroidRuntime(6401): at android.view.View.getDisplayList(View.java:12694)
04-22 17:06:15.200: E/AndroidRuntime(6401): at android.view.ViewGroup.dispatchGetDisplayList(ViewGroup.java:2910)
04-22 17:06:15.200: E/AndroidRuntime(6401): at android.view.View.getDisplayList(View.java:12588)
04-22 17:06:15.200: E/AndroidRuntime(6401): at android.view.View.getDisplayList(View.java:12694)
04-22 17:06:15.200: E/AndroidRuntime(6401): at android.view.ViewGroup.dispatchGetDisplayList(ViewGroup.java:2910)
04-22 17:06:15.200: E/AndroidRuntime(6401): at android.view.View.getDisplayList(View.java:12588)
04-22 17:06:15.200: E/AndroidRuntime(6401): at android.view.View.getDisplayList(View.java:12694)
04-22 17:06:15.200: E/AndroidRuntime(6401): at android.view.ViewGroup.dispatchGetDisplayList(ViewGroup.java:2910)
04-22 17:06:15.200: E/AndroidRuntime(6401): at android.view.View.getDisplayList(View.java:12588)
04-22 17:06:15.200: E/AndroidRuntime(6401): at android.view.View.getDisplayList(View.java:12694)
04-22 17:06:15.200: E/AndroidRuntime(6401): at android.view.ViewGroup.dispatchGetDisplayList(ViewGroup.java:2910)
04-22 17:06:15.200: E/AndroidRuntime(6401): at android.view.View.getDisplayList(View.java:12588)
04-22 17:06:15.200: E/AndroidRuntime(6401): at android.view.View.getDisplayList(View.java:12694)
04-22 17:06:15.200: E/AndroidRuntime(6401): at android.view.HardwareRenderer$GlRenderer.draw(HardwareRenderer.java:1198)
04-22 17:06:15.200: E/AndroidRuntime(6401): at android.view.ViewRootImpl.draw(ViewRootImpl.java:2173)
04-22 17:06:15.200: E/AndroidRuntime(6401): at android.view.ViewRootImpl.performDraw(ViewRootImpl.java:2045)
04-22 17:06:15.200: E/AndroidRuntime(6401): at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:1854)
04-22 17:06:15.200: E/AndroidRuntime(6401): at android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:989)
04-22 17:06:15.200: E/AndroidRuntime(6401): at android.view.ViewRootImpl$TraversalRunnable.run(ViewRootImpl.java:4351)
04-22 17:06:15.200: E/AndroidRuntime(6401): at android.view.Choreographer$CallbackRecord.run(Choreographer.java:749)
04-22 17:06:15.200: E/AndroidRuntime(6401): at android.view.Choreographer.doCallbacks(Choreographer.java:562)
04-22 17:06:15.200: E/AndroidRuntime(6401): at android.view.Choreographer.doFrame(Choreographer.java:532)
04-22 17:06:15.200: E/AndroidRuntime(6401): at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:735)
04-22 17:06:15.200: E/AndroidRuntime(6401): at android.os.Handler.handleCallback(Handler.java:725)
04-22 17:06:15.200: E/AndroidRuntime(6401): at android.os.Handler.dispatchMessage(Handler.java:92)
04-22 17:06:15.200: E/AndroidRuntime(6401): at android.os.Looper.loop(Looper.java:137)
04-22 17:06:15.200: E/AndroidRuntime(6401): at android.app.ActivityThread.main(ActivityThread.java:5226)
04-22 17:06:15.200: E/AndroidRuntime(6401): at java.lang.reflect.Method.invokeNative(Native Method)
04-22 17:06:15.200: E/AndroidRuntime(6401): at java.lang.reflect.Method.invoke(Method.java:511)
04-22 17:06:15.200: E/AndroidRuntime(6401): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:795)
04-22 17:06:15.200: E/AndroidRuntime(6401): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:562)
04-22 17:06:15.200: E/AndroidRuntime(6401): at dalvik.system.NativeStart.main(Native Method)

如果我删除 headerview,我就过了这一点。

补充:这是与标题 View 无关的其他代码:

public class EndlessProfileAdapter extends EndlessAdapter implements
IItemsReadyListener {

private boolean hasMoreData = true;
private RotateAnimation rotate = null;
private View pendingView = null;
private ArrayList<ProfileReview> profileList;

public EndlessProfileAdapter(Context context,
ArrayList<ProfileReview> profileList, Typeface tf) {
super(new ProfileAdapter(context, profileList, tf));

this.profileList = profileList;

rotate = new RotateAnimation(0f, 360f, Animation.RELATIVE_TO_SELF,
0.5f, Animation.RELATIVE_TO_SELF, 0.5f);
rotate.setDuration(600);
rotate.setRepeatMode(Animation.RESTART);
rotate.setRepeatCount(Animation.INFINITE);

}

@Override
protected boolean cacheInBackground() throws Exception {
Log.d("Cache Array Size", String.valueOf(profileList.size()));
new ReviewTask(this, profileList.size()).execute();
return hasMoreData;
}

@Override
protected void appendCachedData() {

}

void startProgressAnimation() {
if (pendingView != null) {
pendingView.startAnimation(rotate);
}
}

public void onItemsReady(ArrayList<ProfileReview> data) {

profileList.addAll(data);
endlessAdapter.onDataReady();
hasMoreData = profileList.isEmpty();
}

内部任务 onPostExecute():

profileList = new ArrayList<ProfileReview>();

for (int i = startPoint; i < startPoint + 10; i++) {
ProfileReview p = new ProfileReview(itemIdList.get(i),
reviewIdList.get(i), reviews.get(i), datelist.get(i),
itemslist.get(i), ratings.get(i), cats.get(i));
profileList.add(p);
}

return profileList;

EndlessAdapter 以列表中的十个为一组。前 10 个工作...但在 11 个时,它崩溃了。 LogCat 崩溃实际上发生在第二轮 10(因此用户滚动到列表中的前 10 个之后的第 11 个项目),并在 profileList.add(p);

更新了请求的信息

onActivityCreated() 中:

       // for adapter constructor 
tf = Typeface.createFromAsset(getActivity().getAssets(),
"fonts/listitemfont.ttf");


LayoutInflater inflater = getActivity().getLayoutInflater();

ViewGroup header = (ViewGroup) inflater.inflate(
R.layout.profile_header, listView, false);

listView = getListView();
listView.addHeaderView(header, null, false);

// stuff like settings up buttons inside header

// just before end of `onActivityCreated()`

if (endlessAdapter == null) {
if (getActivity() != null) {

profileList = new ArrayList<ProfileReview>();
endlessAdapter = new EndlessProfileAdapter(getActivity(),
profileList, tf);
endlessAdapter.setRunInBackground(false);
listView.setAdapter(endlessAdapter);
}
}

最佳答案

EndlessAdapter 将无法支持页眉或页脚 View 。当您使用页眉或页脚 View 时创建的 HeaderListViewAdapter 对底层适配器做出了一些假设,而 EndlessAdapter 并非总是如此。

我已经更新了 EndlessAdapter 文档以指出限制。

关于android - ArrayOutOfBounds 将 addHeaderView() 与 EndlessAdapter 一起使用时,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16158837/

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