首页
学习
活动
专区
工具
TVP
发布
社区首页 >问答首页 >尝试从服务器获取图像时出现IndexOutOfBoundsException

尝试从服务器获取图像时出现IndexOutOfBoundsException
EN

Stack Overflow用户
提问于 2019-03-21 03:55:49
回答 1查看 151关注 0票数 -1

在我从服务器获取数据博客文章项目的这一部分中,我使用Jsoup节点Document解析包含博客文章图像的项目内容,然后使用Jsoup select Elements对象从文档内容中获取图像,并使用Glide将其设置为ImageView

错误日志

代码语言:javascript
复制
E/AndroidRuntime: FATAL EXCEPTION: main
    Process: abtallaldigital.blogspot.com.dummyapp, PID: 26246
    java.lang.IndexOutOfBoundsException: Index: 0, Size: 0
        at java.util.ArrayList.get(ArrayList.java:437)
        at abtallaldigital.blogspot.com.dummyapp.PostAdapter.onBindViewHolder(PostAdapter.java:124)
        at abtallaldigital.blogspot.com.dummyapp.PostAdapter.onBindViewHolder(PostAdapter.java:23)
        at android.support.v7.widget.RecyclerView$Adapter.onBindViewHolder(RecyclerView.java:6673)
        at android.support.v7.widget.RecyclerView$Adapter.bindViewHolder(RecyclerView.java:6714)
        at android.support.v7.widget.RecyclerView$Recycler.tryBindViewHolderByDeadline(RecyclerView.java:5647)
        at android.support.v7.widget.RecyclerView$Recycler.tryGetViewHolderForPositionByDeadline(RecyclerView.java:5913)
        at android.support.v7.widget.RecyclerView$Recycler.getViewForPosition(RecyclerView.java:5752)
        at android.support.v7.widget.RecyclerView$Recycler.getViewForPosition(RecyclerView.java:5748)
        at android.support.v7.widget.LinearLayoutManager$LayoutState.next(LinearLayoutManager.java:2232)
        at android.support.v7.widget.GridLayoutManager.layoutChunk(GridLayoutManager.java:556)
        at android.support.v7.widget.LinearLayoutManager.fill(LinearLayoutManager.java:1519)
        at android.support.v7.widget.LinearLayoutManager.onLayoutChildren(LinearLayoutManager.java:614)
        at android.support.v7.widget.GridLayoutManager.onLayoutChildren(GridLayoutManager.java:170)
        at android.support.v7.widget.RecyclerView.dispatchLayoutStep2(RecyclerView.java:3812)
        at android.support.v7.widget.RecyclerView.dispatchLayout(RecyclerView.java:3529)
        at android.support.v7.widget.RecyclerView.onLayout(RecyclerView.java:4082)
        at android.view.View.layout(View.java:19715)
        at android.view.ViewGroup.layout(ViewGroup.java:6077)
        at android.support.v4.widget.SwipeRefreshLayout.onLayout(SwipeRefreshLayout.java:606)
        at android.view.View.layout(View.java:19715)
        at android.view.ViewGroup.layout(ViewGroup.java:6077)
        at android.widget.RelativeLayout.onLayout(RelativeLayout.java:1083)
        at android.view.View.layout(View.java:19715)
        at android.view.ViewGroup.layout(ViewGroup.java:6077)
        at android.support.design.widget.HeaderScrollingViewBehavior.layoutChild(HeaderScrollingViewBehavior.java:132)
        at android.support.design.widget.ViewOffsetBehavior.onLayoutChild(ViewOffsetBehavior.java:42)
        at android.support.design.widget.AppBarLayout$ScrollingViewBehavior.onLayoutChild(AppBarLayout.java:1361)
        at android.support.design.widget.CoordinatorLayout.onLayout(CoordinatorLayout.java:894)
        at android.view.View.layout(View.java:19715)
        at android.view.ViewGroup.layout(ViewGroup.java:6077)
        at android.support.v4.widget.DrawerLayout.onLayout(DrawerLayout.java:1171)
        at android.view.View.layout(View.java:19715)
        at android.view.ViewGroup.layout(ViewGroup.java:6077)
        at android.widget.FrameLayout.layoutChildren(FrameLayout.java:323)
        at android.widget.FrameLayout.onLayout(FrameLayout.java:261)
        at android.view.View.layout(View.java:19715)
        at android.view.ViewGroup.layout(ViewGroup.java:6077)
        at android.widget.LinearLayout.setChildFrame(LinearLayout.java:1791)
        at android.widget.LinearLayout.layoutVertical(LinearLayout.java:1635)
        at android.widget.LinearLayout.onLayout(LinearLayout.java:1544)
        at android.view.View.layout(View.java:19715)
        at android.view.ViewGroup.layout(ViewGroup.java:6077)
        at android.widget.FrameLayout.layoutChildren(FrameLayout.java:323)
        at android.widget.FrameLayout.onLayout(FrameLayout.java:261)
        at android.view.View.layout(View.java:19715)
        at android.view.ViewGroup.layout(ViewGroup.java:6077)
        at android.widget.LinearLayout.setChildFrame(LinearLayout.java:1791)
        at android.widget.LinearLayout.layoutVertical(LinearLayout.java:1635)
        at android.widget.LinearLayout.onLayout(LinearLayout.java:1544)
        at android.view.View.layout(View.java:19715)
        at android.view.ViewGroup.layout(ViewGroup.java:6077)
        at android.widget.FrameLayout.layoutChildren(FrameLayout.java:323)
        at android.widget.FrameLayout.onLayout(FrameLayout.java:261)
E/AndroidRuntime:     at com.android.internal.policy.DecorView.onLayout(DecorView.java:764)
        at android.view.View.layout(View.java:19715)
        at android.view.ViewGroup.layout(ViewGroup.java:6077)
        at android.view.ViewRootImpl.performLayout(ViewRootImpl.java:2556)
        at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:2272)
        at android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:1452)
        at android.view.ViewRootImpl$TraversalRunnable.run(ViewRootImpl.java:6917)
        at android.view.Choreographer$CallbackRecord.run(Choreographer.java:1029)
        at android.view.Choreographer.doCallbacks(Choreographer.java:841)
        at android.view.Choreographer.doFrame(Choreographer.java:772)
        at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:1015)
        at android.os.Handler.handleCallback(Handler.java:794)
        at android.os.Handler.dispatchMessage(Handler.java:99)
        at android.os.Looper.loop(Looper.java:173)
        at android.app.ActivityThread.main(ActivityThread.java:6653)
        at java.lang.reflect.Method.invoke(Native Method)
        at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:547)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:821)

下面是我的适配器类中的代码

代码语言:javascript
复制
 final Item item = items.get(position);
        holder.postTitle.setText(item.getTitle());
        final Document document = Jsoup.parse(item.getContent());
        Elements elements = document.select("img");
        Log.d("CODE", "Image: "+elements.get(0).attr("src"));
        Log.d("Text",document.text());
        holder.postDescription.setText(document.text());
        Glide.with(context).load(elements.get(0)
                .attr("src"))
                .into(holder.postImage);

我决定使用Log来观察和调试代码,然后我就得到了

代码语言:javascript
复制
D/CODE: Image: http://2.bp.blogspot.com/-OSCe_Ifv6z4/VffMEP73ogI/AAAAAAAAOJE/JWBvtEQUijQ/s1600/health_fitness-is-easy_128K.jpg
I/zygote64: Do full code cache collection, code=124KB, data=91KB
I/zygote64: After code cache collection, code=66KB, data=38KB
D/CODE: Image: http://4.bp.blogspot.com/-G_aTQTCsk0Y/VffXDPrs8TI/AAAAAAAAPCk/Qgi2Lnz35vs/s1600/world_businessman-as-president_329K.jpg
D/CODE: Image: http://2.bp.blogspot.com/-sRql_dC2VZE/VffLdCbxjsI/AAAAAAAAOFo/4EAkYcN83g8/s1600/games_dino-transformers_128K.jpg
W/StaticLayout: maxLineHeight should not be -1.  maxLines:2 lineCount:2
W/StaticLayout: maxLineHeight should not be -1.  maxLines:4 lineCount:4
W/StaticLayout: maxLineHeight should not be -1.  maxLines:2 lineCount:2
W/StaticLayout: maxLineHeight should not be -1.  maxLines:4 lineCount:4
    maxLineHeight should not be -1.  maxLines:2 lineCount:2
W/StaticLayout: maxLineHeight should not be -1.  maxLines:2 lineCount:2
W/StaticLayout: maxLineHeight should not be -1.  maxLines:4 lineCount:4
W/StaticLayout: maxLineHeight should not be -1.  maxLines:2 lineCount:2
W/StaticLayout: maxLineHeight should not be -1.  maxLines:4 lineCount:4
    maxLineHeight should not be -1.  maxLines:2 lineCount:2
W/StaticLayout: maxLineHeight should not be -1.  maxLines:2 lineCount:2
W/StaticLayout: maxLineHeight should not be -1.  maxLines:4 lineCount:4
W/StaticLayout: maxLineHeight should not be -1.  maxLines:2 lineCount:2
W/StaticLayout: maxLineHeight should not be -1.  maxLines:4 lineCount:4
    maxLineHeight should not be -1.  maxLines:2 lineCount:2
D/CODE: Image: http://3.bp.blogspot.com/-ZCSEvQ_fOVE/VffVcXx7KjI/AAAAAAAAO5s/kBoq2HNeNY4/s1600/travel_flying-with-freedom-field_112K.jpg

如你所见,我得到了前四个图像路径和抛出的异常。

注意:当我注释日志和幻灯片代码时,应用程序可以正常运行,但当然没有图像

EN

回答 1

Stack Overflow用户

发布于 2019-03-21 04:00:27

也许elements列表是空的,这意味着没有找到特定项目的img元素?

代码语言:javascript
复制
Elements elements = document.select("img");
Log.d("CODE", "Image: "+elements.get(0).attr("src"));

在尝试使用get(0)检索第一个元素之前,请尝试记录elements.size()或检查elements.isEmpty()

票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/55269171

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档