为什么我在 android-pageradapter 中出现 stackoverflow 错误?
Why am i getting stackoverflow error in android-pageradapter?
i just want to inflate one xml file and show it in viewpager pages.
会
logcat:
FATAL EXCEPTION: main
java.lang.WhosebugError
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 我要膨胀并在 viewpager 中显示的文件,所有 4 页都相同:->
<?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>
您的 instantiateItem()
returns ViewPager
的容器视图而不是要放入容器中的项目。
这是 inflate()
的一个特殊问题,之前可能已经被很多人咬过:如果你调用 inflate(R.layout.createnew, container)
,它 returns 容器视图:
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.
(强调部分是我的)
您无法访问膨胀层次结构的实际根,只能访问已附加新子视图的容器。
在这种情况下,addView()
实际上是多余的,因为新视图已经在容器中。但是,在您的代码中,它将容器添加到自身。 Android 不检查视图层次结构中的循环,但在某些时候会出现无限遍历循环(导致 WhosebugError
)。
自instanceItem()
needs to return the (child) view以来,这个有点问题:
Returns an Object representing the new page. This does not need to be a View, but can be some other container of the page.
要防止将新视图添加到容器中,请将调用更改为
View view = inflate(R.layout.createnew, container, false)
;
并按原样保留对 addView()
的调用。现在有必要,因为适配器是 required to add the view to the container:
The adapter is responsible for adding the view to the container given here, [...]
i just want to inflate one xml file and show it in viewpager pages.
会
logcat:
FATAL EXCEPTION: main java.lang.WhosebugError 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 我要膨胀并在 viewpager 中显示的文件,所有 4 页都相同:->
<?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>
您的 instantiateItem()
returns ViewPager
的容器视图而不是要放入容器中的项目。
这是 inflate()
的一个特殊问题,之前可能已经被很多人咬过:如果你调用 inflate(R.layout.createnew, container)
,它 returns 容器视图:
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.
(强调部分是我的)
您无法访问膨胀层次结构的实际根,只能访问已附加新子视图的容器。
在这种情况下,addView()
实际上是多余的,因为新视图已经在容器中。但是,在您的代码中,它将容器添加到自身。 Android 不检查视图层次结构中的循环,但在某些时候会出现无限遍历循环(导致 WhosebugError
)。
自instanceItem()
needs to return the (child) view以来,这个有点问题:
Returns an Object representing the new page. This does not need to be a View, but can be some other container of the page.
要防止将新视图添加到容器中,请将调用更改为
View view = inflate(R.layout.createnew, container, false)
;
并按原样保留对 addView()
的调用。现在有必要,因为适配器是 required to add the view to the container:
The adapter is responsible for adding the view to the container given here, [...]