IndexOutOfBoundsException, когда я пытаюсь получить изображения с сервера - PullRequest
0 голосов
/ 20 марта 2019

В этой части приложения я получаю данные с записей в блоге с сервера, использую узлы Jsoup Document для анализа содержимого элемента, который содержит изображение сообщения в блоге, а затем с помощью объекта Jsoup select Elements получаемизображения из содержимого документа и установки его в ImageView с помощью Glide

журнал ошибок

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);

Я решил наблюдатьи отладки кода с помощью журнала, то я получил

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, приложение работает нормально, но без изображений, конечно

1 Ответ

2 голосов
/ 20 марта 2019

Возможно, список elements пуст, что означает, что для определенного элемента не найдено элементов img?

Elements elements = document.select("img");
Log.d("CODE", "Image: "+elements.get(0).attr("src"));

Попробуйте записать elements.size() или проверить elements.isEmpty() перед попыткой получить первый элемент с помощью get(0).

Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...