Why am i getting stackoverflow error in android-pageradapter? [closed]
S

1

7

i just want to inflate one xml file and show it in viewpager pages.

lll

logcat:

FATAL EXCEPTION: main java.lang.StackOverflowError at android.graphics.drawable.BitmapDrawable.setAlpha(BitmapDrawable.java:406) at android.graphics.drawable.DrawableContainer.jumpToCurrentState(DrawableContainer.java:179) at android.widget.CompoundButton.jumpDrawablesToCurrentState(CompoundButton.java:319) at android.view.ViewGroup.jumpDrawablesToCurrentState(ViewGroup.java:5158) at android.view.ViewGroup.jumpDrawablesToCurrentState(ViewGroup.java:5158) at android.view.ViewGroup.jumpDrawablesToCurrentState(ViewGroup.java:5158) at android.view.ViewGroup.jumpDrawablesToCurrentState(ViewGroup.java:5158) at android.view.ViewGroup.jumpDrawablesToCurrentState(ViewGroup.java:5158) at android.view.ViewGroup.jumpDrawablesToCurrentState(ViewGroup.java:5158) at android.view.ViewGroup.jumpDrawablesToCurrentState(ViewGroup.java:5158) at android.view.ViewGroup.jumpDrawablesToCurrentState(ViewGroup.java:5158) at android.view.ViewGroup.jumpDrawablesToCurrentState(ViewGroup.java:5158) at android.view.ViewGroup.jumpDrawablesToCurrentState(ViewGroup.java:5158) at android.view.ViewGroup.jumpDrawablesToCurrentState(ViewGroup.java:5158) at android.view.ViewGroup.jumpDrawablesToCurrentState(ViewGroup.java:5158) at android.view.ViewGroup.jumpDrawablesToCurrentState(ViewGroup.java:5158) at android.view.ViewGroup.jumpDrawablesToCurrentState(ViewGroup.java:5158) at android.view.ViewGroup.jumpDrawablesToCurrentState(ViewGroup.java:5158) at android.view.ViewGroup.jumpDrawablesToCurrentState(ViewGroup.java:5158) at android.view.ViewGroup.jumpDrawablesToCurrentState(ViewGroup.java:5158) at android.view.ViewGroup.jumpDrawablesToCurrentState(ViewGroup.java:5158) at android.view.ViewGroup.jumpDrawablesToCurrentState(ViewGroup.java:5158) at android.view.ViewGroup.jumpDrawablesToCurrentState(ViewGroup.java:5158) at android.view.ViewGroup.jumpDrawablesToCurrentState(ViewGroup.java:5158) at android.view.ViewGroup.jumpDrawablesToCurrentState(ViewGroup.java:5158) at android.view.ViewGroup.jumpDrawablesToCurrentState(ViewGroup.java:5158) at android.view.ViewGroup.jumpDrawablesToCurrentState(ViewGroup.java:5158) at android.view.ViewGroup.jumpDrawablesToCurrentState(ViewGroup.java:5158) at android.view.ViewGroup.jumpDrawablesToCurrentState(ViewGroup.java:5158) at android.view.ViewGroup.jumpDrawablesToCurrentState(ViewGroup.java:5158) at android.view.ViewGroup.jumpDrawablesToCurrentState(ViewGroup.java:5158) at android.view.ViewGroup.jumpDrawablesToCurrentState(ViewGroup.java:5158) at android.view.ViewGroup.jumpDrawablesToCurrentState(ViewGroup.java:5158) at android.view.ViewGroup.jumpDrawablesToCurrentState(ViewGroup.java:5158) at android.view.ViewGroup.jumpDrawablesToCurrentState(ViewGroup.java:5158) at android.view.ViewGroup.jumpDrawablesToCurrentState(ViewGroup.java:5158) at android.view.ViewGroup.jumpDrawablesToCurrentState(ViewGroup.java:5158) at android.view.ViewGroup.jumpDrawablesToCurrentState(ViewGroup.java:5158) at android.view.ViewGroup.jumpDrawablesToCurrentState(ViewGroup.java:5158) at android.view.ViewGroup.jumpDrawablesToCurrentState(ViewGroup.java:5158) at android.view.ViewGroup.jumpDrawablesToCurrentState(ViewGroup.java:5158) at android.view.ViewGroup.jumpDrawablesToCurrentState(ViewGroup.java:5158) at android.view.ViewGroup.jumpDrawablesToCurrentState(ViewGroup.java:5158) at android.view.ViewGroup.jumpDrawablesToCurrentState(ViewGroup.java:5158) at android.view.ViewGroup.jumpDrawablesToCurrentState(ViewGroup.java:5158) at android.view.ViewGroup.jumpDrawablesToCurrentState(ViewGroup.java:5158) at android.view.ViewGroup.jumpDrawablesToCurrentState(ViewGroup.java:5158) at android.view.ViewGroup.jumpDrawablesToCurrentState(ViewGroup.java:5158) at android.view.ViewGroup.jumpDrawablesToCurrentState(ViewGroup.java:5158) at android.view.ViewGroup.jumpDrawablesToCurrentState(ViewGroup.java:5158) at android.view.ViewGroup.jumpDrawablesToCurrentState(ViewGroup.java:5158) at android.view.ViewGroup.jumpDrawablesToCurrentState(ViewGroup.java:5158) at android.view.ViewGr

pageradapter class:

public class pageradapter extends PagerAdapter {
    Context mContext;
    LayoutInflater mLayoutInflater;
    List<String> l = MainActivity.list;
    ImageLoader mImageLoader;

    public pageradapter(Context context) {
        mContext = context;

        mLayoutInflater = LayoutInflater.from(mContext.getApplicationContext());

    }

    @Override
    public int getCount() {
        return 4;
    }


    @Override
    public Object instantiateItem(ViewGroup container, int position) {
        //  View itemView = mLayoutInflater.inflate(R.layout.img, container, false);
        //   ImageLoader mImageLoader = ImageLoader.getInstance();


        View view = mLayoutInflater.inflate(R.layout.createnew, container);

        //   ImageView imageView = (ImageView) itemView.findViewById(R.id.imageView3);
        //  mImageLoader.displayImage("http://www.airtel.in/4g/images/airtel_4g_hotspot_responsive.jpg", imageView);

        container.addView(view);

        return view;
    }

    @Override
    public void destroyItem(ViewGroup container, int position, Object object) {
        container.removeView((LinearLayout) object);
    }

    @Override
    public boolean isViewFromObject(View view, Object object) {
        return view == object;
    }
}

xml file that i want to inflate and show it in viewpager ,same in all 4 pages:->

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical" android:layout_width="match_parent"
    android:layout_height="match_parent">

    <RadioButton
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="New RadioButton"
        android:id="@+id/radioButton"
        android:layout_gravity="center_horizontal" />

    <Switch
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="New Switch"
        android:id="@+id/switch1"
        android:layout_gravity="center_horizontal" />
</LinearLayout>
Semiaquatic answered 8/2, 2016 at 9:14 Comment(0)
U
18

Your instantiateItem() returns the container view of the ViewPager instead of the item to go inside the container.

This is a particular nastiness of inflate(), that has probably bitten many before: If you call inflate(R.layout.createnew, container), it returns the container view:

Returns

The root View of the inflated hierarchy. If root was supplied, this is the root View; otherwise it is the root of the inflated XML file.

(emphasis partially mine)

You don't get access to the actual root of the inflated hierarchy, only to the container with the new child view already attached.

In this case, addView() would actually be redundant, because the new View is already in the container. In your code, however, it adds the container to itself. Android does not check for loops in the view hierarchy, but you get an infinite traversal loop at some point (causing the StackOverflowError).

Since instanceItem() needs to return the (child) view, this is a little bit of a problem:

Returns an Object representing the new page. This does not need to be a View, but can be some other container of the page.

To prevent the new view to be added to the container, change the call to

View view = inflate(R.layout.createnew, container, false);

and leave the call to addView() as is. It is now necessary, since the adapter is required to add the view to the container:

The adapter is responsible for adding the view to the container given here, [...]

Unscramble answered 8/2, 2016 at 9:43 Comment(2)
So the question arises, when should we add the container inside the inflate()?Teillo
@Dennis From experience: Whenever you can. The inflator will ask the root view to produce layout parameters for any children created from the resource.Unscramble

© 2022 - 2024 — McMap. All rights reserved.