【问题标题】:IndexOutOfBoundsException on ArrayListArrayList 上的 IndexOutOfBoundsException
【发布时间】:2016-11-07 04:36:39
【问题描述】:

由于某种原因,此方法在我的 ArrayList 中的第一个位置返回 viewType = 2。那是因为 0 除以 5 不会产生余数吗?如果我设置 && position != 0 而不是 1 我得到 java.lang.IndexOutOfBoundsException: Invalid index 0, size is 0

@Override
public int getItemViewType(int position) {
        int viewType = 1;
        // Ads appear where the position in the list divided by 5 does not produce a remainder, i.e. 0, 5, 10, 15, 20, 25, etc.
        if ((position % 5 == 0) && position != 1) viewType = 2;
        return viewType;
}

理想情况下,我的广告展示位置 (viewType = 2) 每 25 个位置出现一次,第一个位置除外。我该怎么做?

编辑

public class MarketFeedRecyclerAdapter extends LoadingRowRecyclerAdapter {

    private static final int VIEW_TYPE_MARKET_FEED = 1;
    private static final int VIEW_TYPE_AD = 2;

    private final Context context;
    private final List<Feed> feedItems;
    private final ImageLoader feedItemImageLoader;
    private FeedItemClickListener feedItemClickListener;

    private boolean isLongPressed = false;
    public MarketFeedRecyclerAdapter(Context context, List<Feed> feedItems, ImageLoader feedItemImageLoader, List<Feed> adItems) {
        this.context = context;
        this.feedItems = feedItems;
        this.feedItemImageLoader = feedItemImageLoader;
        if (!adItems.isEmpty()) { feedItems.addAll(adItems); }
    }

    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        switch (viewType) {
            case VIEW_TYPE_MARKET_FEED:
                return new MarketFeedViewHolder(new FeedItemView(context));
            case VIEW_TYPE_AD:
                return new AdViewHolder(new MarketFeedAdItemView(context));
        }
        return super.onCreateViewHolder(parent, viewType);
    }

    // Differentiate between feedItem views and nativeAds
    @Override
    public int getItemViewType(int position) {
            int viewType = 1;
            // Ads appear where the position in the list divided by 5 does not produce a remainder, i.e. 0, 5, 10, 15, 20, 25, etc.
            if ((position % 5 == 0)) viewType = 2;
            return viewType;
    }

    @Override
    public void onBindViewHolder(RecyclerView.ViewHolder viewHolder, int position) {
        if (getItemViewType(position) == VIEW_TYPE_MARKET_FEED) {
            bindMarketFeedItemView((MarketFeedViewHolder) viewHolder, position);
        }  else {
            bindAdItemView((AdViewHolder) viewHolder);
        }
    }

    // For Ad Objects
    private void bindAdItemView(AdViewHolder viewHolder) {

        showNativeAd(viewHolder);

    }

    private void bindMarketFeedItemView(MarketFeedViewHolder viewHolder, int position) {
        final FeedItemView feedItemView = viewHolder.feedItemView;
        final Feed feedDesign = feedItems.get(position);

        // TODO we can probably conditionally show or hide these based on the type of feed item, same as in FeedActivitySingle
        feedItemView.showOrHideEditButton(false);
        feedItemView.showOrHideBuyButton(true);
        feedItemView.showOrHideFlipButton(feedDesign.getDesign().getCompressedBackImage() != null);
        feedItemView.showOrHidePriceText(true);

        // Set the results into TextViews
        feedItemView.setProductPriceText(String.valueOf(feedDesign.getDesign().getPrice()));
        feedItemView.setDownloadsText(String.valueOf(feedDesign.getDesign().getDownloadCount()));
        feedItemView.setLikesText(String.valueOf(feedDesign.getDesign().getLikesCount()));
        feedItemView.setUsernameText(feedDesign.getDesign().getAuthor().getUsername());
        feedItemView.setTimestampText(feedDesign.getTimestampText());
        feedItemView.getSaveImage().setImageResource(feedDesign.isInPersonalGallery() ? R.drawable.ic_action_saved : R.drawable.ic_not_saved);
        feedItemView.getLikeImage().setImageResource(feedDesign.isLiked() ? R.drawable.ic_action_like_feed_full : R.drawable.ic_action_like_feed);

        feedItemView.getTrashImage().setVisibility(ParseHelper.isCurrentUser(feedDesign.getDesign().getAuthor().getObjectId()) ? View.VISIBLE : View.GONE);
        feedItemView.getFeedSocialShareImage().setVisibility(View.VISIBLE);

        switch(feedDesign.getDisplayedSide()) {
            case FRONT:
                feedItemImageLoader.DisplayImage(feedDesign.getDesign().getCompressedImage().getUrl(), feedItemView.getImage(), feedItemView.getProgressBar());
                break;
            case BACK:
                feedItemImageLoader.DisplayImage(feedDesign.getDesign().getCompressedBackImage().getUrl(), feedItemView.getImage(), feedItemView.getProgressBar());
                break;
        }

        if(feedDesign.getDesign().getAuthor().getProfilePicture() != null) {
            feedItemImageLoader.DisplayImage(feedDesign.getDesign().getAuthor().getProfilePicture().getUrl(), feedItemView.getProfilePicture(), null); // TODO should this use profilePictureFileCache?
        } else {
            viewHolder.feedItemView.getProfilePicture().setImageResource(R.drawable.ic_anonymous);
        }

        SetCommentViews(feedItemView, feedDesign.getComments());
        SetClickListeners(feedItemView, feedDesign, position);
    }

    @Override
    protected int getContentDataSize() {
        return feedItems.size();
    }

    @Override
    protected int getViewType(int position) {
        return VIEW_TYPE_MARKET_FEED;
    }

    class MarketFeedViewHolder extends RecyclerView.ViewHolder {

        FeedItemView feedItemView;

        public MarketFeedViewHolder(FeedItemView view) {
            super(view);
            this.feedItemView = view;
        }
    }

    class AdViewHolder extends RecyclerView.ViewHolder {

        MarketFeedAdItemView adItemView;

        public AdViewHolder(MarketFeedAdItemView view) {
            super(view);
            this.adItemView = view;
        }

    }

    private NativeAd nativeAd;
    private AdChoicesView adChoicesView;

    private void showNativeAd(AdViewHolder viewHolder){
        AdSettings.addTestDevice("STRING");
        nativeAd = new NativeAd(context, "STRING");
        nativeAd.setAdListener(new AdListener() {

            @Override
            public void onError(Ad ad, AdError adError) {

            }

            @Override
            public void onAdLoaded(Ad ad) {

                final MarketFeedAdItemView adItemView = viewHolder.adItemView;

                // Setting the Text
                adItemView.nativeAdSocialContext.setText(nativeAd.getAdSocialContext());
                adItemView.nativeAdCallToAction.setText(nativeAd.getAdCallToAction());
                adItemView.nativeAdTitle.setText(nativeAd.getAdTitle());
                adItemView.nativeAdBody.setText(nativeAd.getAdBody());

                // Downloading and setting the ad icon
                NativeAd.Image adIcon = nativeAd.getAdIcon();
                NativeAd.downloadAndDisplayImage(adIcon, adItemView.nativeAdIcon);

                // Download and setting the cover image
                /*NativeAd.Image adCoverImage = nativeAd.getAdCoverImage();*/
                adItemView.nativeAdMedia.setNativeAd(nativeAd);

                // Add adChoices icon
                if (adChoicesView == null) {
                    adChoicesView = new AdChoicesView(context, nativeAd, true);
                    adItemView.addView(adChoicesView, 0);
                }

                nativeAd.registerViewForInteraction(adItemView);

            }

            @Override
            public void onAdClicked(Ad ad) {

            }
        });

        nativeAd.loadAd();
    }
}

错误输出

    java.lang.IndexOutOfBoundsException: Invalid index 0, size is 0
         at java.util.ArrayList.throwIndexOutOfBoundsException(ArrayList.java:255)
         at java.util.ArrayList.get(ArrayList.java:308)
         at com.elgami.market.MarketFeedRecyclerAdapter.bindMarketFeedItemView(MarketFeedRecyclerAdapter.java:94)
         at com.elgami.market.MarketFeedRecyclerAdapter.onBindViewHolder(MarketFeedRecyclerAdapter.java:79)
         at android.support.v7.widget.RecyclerView$Adapter.onBindViewHolder(RecyclerView.java:5768)
         at android.support.v7.widget.RecyclerView$Adapter.bindViewHolder(RecyclerView.java:5801)
         at android.support.v7.widget.RecyclerView$Recycler.getViewForPosition(RecyclerView.java:5037)
         at android.support.v7.widget.RecyclerView$Recycler.getViewForPosition(RecyclerView.java:4913)
         at android.support.v7.widget.LinearLayoutManager$LayoutState.next(LinearLayoutManager.java:2029)
         at android.support.v7.widget.LinearLayoutManager.layoutChunk(LinearLayoutManager.java:1414)
         at android.support.v7.widget.LinearLayoutManager.fill(LinearLayoutManager.java:1377)
         at android.support.v7.widget.LinearLayoutManager.onLayoutChildren(LinearLayoutManager.java:578)
         at android.support.v7.widget.RecyclerView.dispatchLayoutStep2(RecyclerView.java:3260)
         at android.support.v7.widget.RecyclerView.dispatchLayout(RecyclerView.java:3069)
         at android.support.v7.widget.RecyclerView.onLayout(RecyclerView.java:3518)
         at android.view.View.layout(View.java:16636)
         at android.view.ViewGroup.layout(ViewGroup.java:5437)
         at android.support.v4.widget.SwipeRefreshLayout.onLayout(SwipeRefreshLayout.java:598)
         at android.view.View.layout(View.java:16636)
         at android.view.ViewGroup.layout(ViewGroup.java:5437)
         at android.widget.LinearLayout.setChildFrame(LinearLayout.java:1743)
         at android.widget.LinearLayout.layoutVertical(LinearLayout.java:1586)
         at android.widget.LinearLayout.onLayout(LinearLayout.java:1495)
         at android.view.View.layout(View.java:16636)
         at android.view.ViewGroup.layout(ViewGroup.java:5437)
         at android.widget.RelativeLayout.onLayout(RelativeLayout.java:1079)
         at android.view.View.layout(View.java:16636)
         at android.view.ViewGroup.layout(ViewGroup.java:5437)
         at android.widget.FrameLayout.layoutChildren(FrameLayout.java:336)
         at android.widget.FrameLayout.onLayout(FrameLayout.java:273)
         at android.view.View.layout(View.java:16636)
         at android.view.ViewGroup.layout(ViewGroup.java:5437)
         at android.widget.LinearLayout.setChildFrame(LinearLayout.java:1743)
         at android.widget.LinearLayout.layoutVertical(LinearLayout.java:1586)
         at android.widget.LinearLayout.onLayout(LinearLayout.java:1495)
         at android.view.View.layout(View.java:16636)
         at android.view.ViewGroup.layout(ViewGroup.java:5437)
         at android.widget.FrameLayout.layoutChildren(FrameLayout.java:336)
         at android.widget.FrameLayout.onLayout(FrameLayout.java:273)
         at android.view.View.layout(View.java:16636)
         at android.view.ViewGroup.layout(ViewGroup.java:5437)
         at android.widget.LinearLayout.setChildFrame(LinearLayout.java:1743)
         at android.widget.LinearLayout.layoutVertical(LinearLayout.java:1586)
         at android.widget.LinearLayout.onLayout(LinearLayout.java:1495)
         at android.view.View.layout(View.java:16636)
         at android.view.ViewGroup.layout(ViewGroup.java:5437)
         at android.widget.FrameLayout.layoutChildren(FrameLayout.java:336)
         at android.widget.FrameLayout.onLayout(FrameLayout.java:273)
         at com.android.internal.policy.PhoneWindow$DecorView.onLayout(PhoneWindow.java:2678)
         at android.view.View.layout(View.java:16636)
         at android.view.ViewGroup.layout(ViewGroup.java:5437)
         at android.view.ViewRootImpl.performLayout(ViewRootImpl.java:2171)
         at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:1931)
         at android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:1107)
         at android.view.ViewRootImpl$TraversalRunnable.run(ViewRootImpl.java:6013)
         at android.view.Choreographer$CallbackRecord.run(Choreographer.java:858)
         at android.view.Choreographer.doCallbacks(Choreographer.java:670)
        at android.view.Choreographe

LoadingRowRecyclerAdapter:

public abstract class LoadingRowRecyclerAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {

    public static final int ROW_VIEW_TYPE_LOADING = 72398; // obscure number
    private boolean mContainsLoadingRow;

    protected abstract int getContentDataSize();

    protected abstract int getViewType(int position);

    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        switch (viewType) {
            case ROW_VIEW_TYPE_LOADING:
                return new LoadingViewHolder(LayoutInflater.from(parent.getContext()).inflate(R.layout.loading_row, parent, false));
        }

        throw new IllegalArgumentException("viewType is not ROW_VIEW_TYPE_LOADING. You must handle all other values of viewType (defined by getViewType) before calling super.");
    }

    @Override
    public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {
        // empty
    }

    @Override
    public final int getItemCount() {
        return mContainsLoadingRow ? (getContentDataSize() + 1) : getContentDataSize();
    }

    @Override
    public int getItemViewType(int position) {
        return (position == getContentDataSize()) ? ROW_VIEW_TYPE_LOADING : getViewType(position);
    }

    /**
     * Sets a boolean which is used by getItemCount and in turn getItemViewType to determine which view type the row should be (loading view vs. other view).
     * Should only be called when there is more results to load in an upcoming api request (determined by calling fragment).
     */
    public void toggleLoadingRowOn() {
        mContainsLoadingRow = true;
    }

    /**
     * Checks to see if a loading row exists by checking an instance boolean and removes the row / clears the boolean.
     * This helps to 'replace' a loading row with a different row.
     */
    public void toggleLoadingRowOff() {
        if (mContainsLoadingRow) {
            mContainsLoadingRow = false;

            // removes the loading row explicitly instead of allowing it to be 'pushed' down when new user suggestion rows are added.
            // this is only required to maintain consistency with the rest of the app.
            int position = getContentDataSize();
            if (position >= 0) {
                notifyItemRemoved(position);
            }
        }
    }

    protected class LoadingViewHolder extends RecyclerView.ViewHolder {

        public LoadingViewHolder(View v) {
            super(v);
        }
    }
}

LoadingRowRecyclerAdapter:

public abstract class EndlessRecyclerOnScrollListener extends RecyclerView.OnScrollListener {

    private static final int VISIBLE_THRESHOLD = 5; // The minimum amount of items to have below your current scroll position before loading more
    private LinearLayoutManager mLinearLayoutManager;

    public abstract void onLoadMore();

    public EndlessRecyclerOnScrollListener(LinearLayoutManager linearLayoutManager) {
        this.mLinearLayoutManager = linearLayoutManager;
    }

    @Override
    public void onScrolled(RecyclerView recyclerView, int dx, int dy) {
        super.onScrolled(recyclerView, dx, dy);

        int totalItemCount = mLinearLayoutManager.getItemCount();
        int firstVisibleItem = mLinearLayoutManager.findFirstVisibleItemPosition();
        int visibleItemCount = recyclerView.getChildCount();

        int lastItemVisible = firstVisibleItem + visibleItemCount;

        // once the last visible item is within VISIBLE_THRESHOLD from the bottom, we want to load more
        if ((totalItemCount - lastItemVisible) <= VISIBLE_THRESHOLD) {
            onLoadMore();
        }
    }
}

新堆栈跟踪

java.lang.IndexOutOfBoundsException: Invalid index 0, size is 0
   at java.util.ArrayList.throwIndexOutOfBoundsException(ArrayList.java:255)
   at java.util.ArrayList.get(ArrayList.java:308)
   at com.elgami.market.MarketFeedRecyclerAdapter.bindMarketFeedItemView(MarketFeedRecyclerAdapter.java:93)
   at com.elgami.market.MarketFeedRecyclerAdapter.onBindViewHolder(MarketFeedRecyclerAdapter.java:78)
   at android.support.v7.widget.RecyclerView$Adapter.onBindViewHolder(RecyclerView.java:5768)
   at android.support.v7.widget.RecyclerView$Adapter.bindViewHolder(RecyclerView.java:5801)
   at android.support.v7.widget.RecyclerView$Recycler.getViewForPosition(RecyclerView.java:5037)
   at android.support.v7.widget.RecyclerView$Recycler.getViewForPosition(RecyclerView.java:4913)
   at android.support.v7.widget.LinearLayoutManager$LayoutState.next(LinearLayoutManager.java:2029)
   at android.support.v7.widget.LinearLayoutManager.layoutChunk(LinearLayoutManager.java:1414)
   at android.support.v7.widget.LinearLayoutManager.fill(LinearLayoutManager.java:1377)
   at android.support.v7.widget.LinearLayoutManager.onLayoutChildren(LinearLayoutManager.java:578)
   at android.support.v7.widget.RecyclerView.dispatchLayoutStep2(RecyclerView.java:3260)
   at android.support.v7.widget.RecyclerView.dispatchLayout(RecyclerView.java:3069)
   at android.support.v7.widget.RecyclerView.onLayout(RecyclerView.java:3518)
   at android.view.View.layout(View.java:16636)
   at android.view.ViewGroup.layout(ViewGroup.java:5437)
   at android.support.v4.widget.SwipeRefreshLayout.onLayout(SwipeRefreshLayout.java:598)
   at android.view.View.layout(View.java:16636)
   at android.view.ViewGroup.layout(ViewGroup.java:5437)
   at android.widget.LinearLayout.setChildFrame(LinearLayout.java:1743)
   at android.widget.LinearLayout.layoutVertical(LinearLayout.java:1586)
   at android.widget.LinearLayout.onLayout(LinearLayout.java:1495)
   at android.view.View.layout(View.java:16636)
   at android.view.ViewGroup.layout(ViewGroup.java:5437)
   at android.widget.RelativeLayout.onLayout(RelativeLayout.java:1079)
   at android.view.View.layout(View.java:16636)
   at android.view.ViewGroup.layout(ViewGroup.java:5437)
   at android.widget.FrameLayout.layoutChildren(FrameLayout.java:336)
   at android.widget.FrameLayout.onLayout(FrameLayout.java:273)
   at android.view.View.layout(View.java:16636)
   at android.view.ViewGroup.layout(ViewGroup.java:5437)
   at android.widget.LinearLayout.setChildFrame(LinearLayout.java:1743)
   at android.widget.LinearLayout.layoutVertical(LinearLayout.java:1586)
   at android.widget.LinearLayout.onLayout(LinearLayout.java:1495)
   at android.view.View.layout(View.java:16636)
   at android.view.ViewGroup.layout(ViewGroup.java:5437)
   at android.widget.FrameLayout.layoutChildren(FrameLayout.java:336)
   at android.widget.FrameLayout.onLayout(FrameLayout.java:273)
   at android.view.View.layout(View.java:16636)
   at android.view.ViewGroup.layout(ViewGroup.java:5437)
   at android.widget.LinearLayout.setChildFrame(LinearLayout.java:1743)
   at android.widget.LinearLayout.layoutVertical(LinearLayout.java:1586)
   at android.widget.LinearLayout.onLayout(LinearLayout.java:1495)
   at android.view.View.layout(View.java:16636)
   at android.view.ViewGroup.layout(ViewGroup.java:5437)
   at android.widget.FrameLayout.layoutChildren(FrameLayout.java:336)
   at android.widget.FrameLayout.onLayout(FrameLayout.java:273)
   at com.android.internal.policy.PhoneWindow$DecorView.onLayout(PhoneWindow.java:2678)
   at android.view.View.layout(View.java:16636)
   at android.view.ViewGroup.layout(ViewGroup.java:5437)
   at android.view.ViewRootImpl.performLayout(ViewRootImpl.java:2171)
   at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:1931)
   at android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:1107)
   at android.view.ViewRootImpl$TraversalRunnable.run(ViewRootImpl.java:6013)
   at android.view.Choreographer$CallbackRecord.run(Choreographer.java:858)
   at android.view.Choreographer.doCallbacks(Choreographer.java:670)
at android.view.Choreograph

【问题讨论】:

  • 放置整个Adapter类代码。
  • 好的,我都贴进去了。
  • 我也添加了错误。
  • 错误出现在onBindViewHolder 而不是getItemViewType()
  • 如果我设置 if (position != 1 &amp;&amp; position % 5 == 0) 一切都很好,但没有其他组合有效。

标签: java android arraylist android-recyclerview


【解决方案1】:

我认为你只需要这样做:

private static final int VIEW_TYPE_MARKET_FEED = 0;
private static final int VIEW_TYPE_AD = 1;

...

@Override
public int getItemViewType(int position) {
    int viewType = VIEW_TYPE_MARKET_FEED;
    if (position % 6 == 5) {
        viewType = VIEW_TYPE_AD;
    }
    return viewType;
}

因为您不能拥有视图类型 2,因为:“Integers must be in the range 0 to getViewTypeCount() - 1”。

另外,在方法@​​987654323@中,改行:

bindMarketFeedItemView((MarketFeedViewHolder) viewHolder, position);

与:

bindMarketFeedItemView((MarketFeedViewHolder) viewHolder, position - position / 6);

为了删除您已插入的添加项。

【讨论】:

  • 我是否应该将 viewTypes 更改为 0 和 1 而不是 1 和 2?
  • 好吧,不管怎样我还是会得到java.lang.IndexOutOfBoundsException: Invalid index 0, size is 0
  • 实际上崩溃出现在第 94 行,应该是:“final Feed feedDesign = feedItems.get(position);”。您的“feedItems”列表似乎是空的。
  • 是的,我看到了,我一直在试图找出原因。
  • :) 很奇怪,它不应该崩溃。如果您删除 &amp;&amp; position &gt; 0,请检查是否没有任何项目丢失,因为 position - position / 6 预计第一个位置没有广告。
猜你喜欢
  • 2016-03-22
  • 2018-12-03
  • 1970-01-01
  • 2014-01-14
  • 2015-11-07
  • 2013-11-06
  • 2012-05-02
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多