ArrayOutOfBounds与EndlessAdapter一起使用addHeaderView()时

时间:2013-04-23 00:13:24

标签: android android-asynctask android-arrayadapter commonsware-cwac

我在EndlessAdapter使用Commonsware。当我添加了header视图后,我收到了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)

如果我删除了标题视图,我就会超越这一点。

补充:如果这与标题视图无关,则还有其他代码:

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个组。前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);
        }
    }

1 个答案:

答案 0 :(得分:1)

EndlessAdapter无法支持页眉或页脚视图。使用页眉或页脚视图时创建的HeaderListViewAdapter会对基础适配器做出一些假设,EndlessAdapter并非总是如此。

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