在应用程序的这一部分中,我从服务器获取数据博客帖子项目,我使用Jsoup节点Document
来解析包含博客帖子图像的项目内容,然后使用Jsoup select Elements
对象从文档内容中获取图像,然后使用Glide将其设置为ImageView
错误日志
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)
这是我的适配器类中的代码
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来观察和调试代码,然后明白了
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
如您所见,我得到了前四个图像路径和引发的异常。
注意:当我注释Log and Glide代码时,该应用正常运行,但没有图像
答案 0 :(得分:2)
也许elements
列表为空,这意味着没有为特定项目找到img
个元素?
Elements elements = document.select("img");
Log.d("CODE", "Image: "+elements.get(0).attr("src"));
在尝试使用elements.size()
检索第一个元素之前,尝试记录elements.isEmpty()
或检查get(0)
。