Why am I getting stackoverflow error in android-pageradapter?

I just want to inflate one xml file and show it on viewpager pages.

Lll

LogCat:

FATAL EXCEPTION: main java.lang.StackOverflowError in android.graphics.drawable.BitmapDrawable.setAlpha (BitmapDrawable.java:406) in android.graphics.drawable.DrawableContainer.jumpToCurrentState (DrawableContainer.java:idbutton. jumpDrawablesToCurrentState (CompoundButton.javahaps19) in android.view.ViewGroup.jumpDrawablesToCurrentState (ViewGroup.java∗158) in android.view.ViewGroup.jumpDrawablesToCurrentState (ViewGroup.java UP158) in android.view.ViewGatejroupumpur.urgurjurumpurgroup : 5158) in android.view.View Group.jumpDrawablesToCurrentState (ViewGroup.java//158) in android.view.ViewGroup.jumpDrawablesToCurrentState (ViewGroup.java►158) in android.view.ViewGroup.jumpDrawablesToCurrentState (ViewGroup.javaump15158) in android.view.viewGroupTjroupGroup.j .java: 5158) in android.view.ViewGroup.jumpDrawablesToCurrentState (ViewGroup.java∗158) in android.view.ViewGroup.jumpDrawablesToCurrentState (ViewGroup.java UP158) in android.view.ViewGroup.jumpDrawablesToCurrentState (ViewGroup.roup in android.view.ViewGroup.jumpDrawablesToCurrentState (ViewGroup.java=158) in android.view.ViewGroup.jumpDrawablesToCurrentState (ViewGroup.java=158) in android.view.ViewGroup.jumpDrawablesToCurrentState (ViewGroup.java//158) in android.view.ViewGroup.jumpDrawablesToCurrentState (ViewGroup.java UP158). ViewGroup.jumpDrawablesToCurrentState (ViewGroup.java UP158) in android.view.ViewGroup.jumpDrawablesToCurrentState (ViewGroup.java∗158) in android.view.ViewGroup.jumpDrawablesToCurrentState (ViewGroup.javahaps158) in android.viewGroupGroupPreviewGroupGroupPreview .java: 5158) in android.view.ViewGroup.jumpDrawablesToCurrentState (ViewGroup.javahaps 158) in android.view.ViewGroup.jumpDrawablesToCurrentState (ViewGroup.javaD15ables) in android.view.ViewGroup.jumpDrawablesToCurrentState (ViewGroup.java//158) in android.view.ViewGroup.jumpDrawablesToCurrentState (ViewGroup.java1515 view.ViewGroup.jumpDrawablesToCurrentState (ViewGroup.java∗158) in android.view.ViewGroup.jumpDrawablesToCurrentState (ViewGroup.java∗158) in android.view.ViewGroup.jumpDrawablesToCurrentState (ViewGroup.javahaps158) in android.viewviewGroup.jump (ViewGroup.java∗158) in android.view.ViewGroup.jump DrawablesToCurrentState (ViewGroup.java∗158) in android.view.ViewGroup.jumpDrawablesToCurrentState (ViewGroup.java UP158) in android.view.ViewGroup.jumpDrawablesToCurrentState (ViewGroup.java UP158) in android.view.ViewGroup.jumpDrawTributes : 5158) in android.view.ViewGroup.jumpDrawablesToCurrentState (ViewGroup.javaPoint158) in android.view.ViewGroup.jumpDrawablesToCurrentState (ViewGroup.java//158) in android.view.ViewGroup.jumpDrawablesToCurrentState (ViewGroup.java15 .view.ViewGroup.jumpDrawablesToCurrentState (ViewGroup.java∗158) in android.view.ViewGroup.jumpDrawablesToCurrentState (ViewGroup.java=158) in android.view.ViewGroup.jumpDrawablesToCurrentState (ViewGroup.java//158) in android.view.ViewGroup.jumpDrawablesToCurrentState (ViewGroup.java UP158). ViewGroup.jumpDrawablesToCurrentState (ViewGroup.java UP158) in android.view.ViewGroup.jumpDrawablesToCurrentState (ViewGroup.java∗158) in android.view.ViewGroup.jumpDrawablesToCurrentState (ViewGroup.javahaps158) in android.viewGroupGroupPreviewGroupGroupPreview .java: 5158) in android.view.ViewGroup.jumpDrawablesToCurrentState (ViewGroup.java∗158) in android.view.ViewGroup.jumpDrawablesToCurrentState (ViewGroup.java=158) in android.view.ViewGroup.jumpDrawablesToCurrentState (ViewGroup.java//158) in android.view.ViewGroup.jumpDrawablesToCurrentState (ViewGroup.java UP158). ViewGroup.jumpDrawablesToCurrentState (ViewGroup.java UP158) in android.view.ViewGroup.jumpDrawablesToCurrentState (ViewGroup.java∗158) in android.view.ViewGroup.jumpDrawablesToCurrentState (ViewGroup.javahaps158) in android.viewGroupGroupPreviewGroupGroupPreview .java: 5158) on android.view.ViewGr

class pageradapter:

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

The xml file I want to inflate and show it in the viewpager is the same on 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> 
+5
source share
1 answer

Your instantiateItem() returns a ViewPager container container instead of the element that should be inside the container.

This is a special muck inflate() , which probably bit many before: if you call inflate(R.layout.createnew, container) , it returns a container like:

Returns

The root view of an overestimated hierarchy. If root has been set, this is root View ; otherwise, this is the root of the inflated XML file.

(selection is partly mine)

You do not 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() will actually be redundant because the new view is already in the container. However, in your code, it adds the container to itself. Android does not check the loops in the view hierarchy, but at some point you get an endless traversal loop (raising a StackOverflowError ).

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

Gets an object representing the new page. This should not be a View, but it could be another page container.

To prevent a new view from being added to the container, change the call to

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

and leave the addView() call as is. Now it is necessary, since the adapter should add a view to the container :

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

+11
source

Source: https://habr.com/ru/post/1242519/


All Articles