gpt4 book ai didi

android - 为什么我在 android-pageradapter 中出现 stackoverflow 错误?

转载 作者:行者123 更新时间:2023-12-04 09:58:39 30 4
gpt4 key购买 nike

关闭。这个问题需要details or clarity .它目前不接受答案。












想改进这个问题?通过 editing this post 添加详细信息并澄清问题.


6年前关闭。







Improve this question




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



页面适配器类:
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;
}
}

我想膨胀并在 viewpager 中显示的 xml 文件,在所有 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()返回 ViewPager 的容器 View 而不是要进入容器的项目。

这是 inflate() 的一个特别讨厌的地方。 ,这可能已经被很多人咬过:如果你调用 inflate(R.layout.createnew, 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.



(强调部分我的)

您无法访问膨胀层次结构的实际根,只能访问已附加新 subview 的容器。

在这种情况下, addView()实际上是多余的,因为新 View 已经在容器中。但是,在您的代码中,它将容器添加到自身。 Android 不会检查 View 层次结构中的循环,但在某些时候会出现无限遍历循环(导致 StackOverflowError )。

由于 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 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, [...]

关于android - 为什么我在 android-pageradapter 中出现 stackoverflow 错误?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35265898/

30 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com