我有一个ViewPager,它是弹出窗口的一部分。当我第一次打开弹出窗口时,一切正常,但当我使用相同数据第二次打开它时,就会出现IllegalStateException错误。
以下是堆栈跟踪:
04-08 15:17:48.370: E/AndroidRuntime(10929): 致命异常:主线程错误,导致应用程序无法正常运行。错误类型为java.lang.IllegalStateException,提示子元素已经有了其它父级元素,请先从该子元素的当前父级元素中移除该子元素。相关代码如下:android.view.ViewGroup.addViewInner(ViewGroup.java:3337); android.view.ViewGroup.addView(ViewGroup.java:3208); android.support.v4.view.ViewPager.addView(ViewPager.java:920); android.view.ViewGroup.addView(ViewGroup.java:3165); android.view.ViewGroup.addView(ViewGroup.java:3145); com.componentix.imwizard.SamplePagerAdapter.instantiateItem(SamplePagerAdapter.java:22)。这个错误还导致了ViewPager无法正常完成滚动和绘制工作,具体表现为应用程序卡顿或崩溃。
任何想法?
以下是堆栈跟踪:
04-08 15:17:48.370: E/AndroidRuntime(10929): 致命异常:主线程错误,导致应用程序无法正常运行。错误类型为java.lang.IllegalStateException,提示子元素已经有了其它父级元素,请先从该子元素的当前父级元素中移除该子元素。相关代码如下:android.view.ViewGroup.addViewInner(ViewGroup.java:3337); android.view.ViewGroup.addView(ViewGroup.java:3208); android.support.v4.view.ViewPager.addView(ViewPager.java:920); android.view.ViewGroup.addView(ViewGroup.java:3165); android.view.ViewGroup.addView(ViewGroup.java:3145); com.componentix.imwizard.SamplePagerAdapter.instantiateItem(SamplePagerAdapter.java:22)。这个错误还导致了ViewPager无法正常完成滚动和绘制工作,具体表现为应用程序卡顿或崩溃。
以下是我的代码:
public void showPopupWindow(Drawable picture)
{
int imageResource = R.drawable.background;
Drawable image = getResources().getDrawable(imageResource);
popupLayout = new LinearLayout(context);
popupLayout.setOrientation(LinearLayout.VERTICAL);
popupLayout.setBackgroundResource(R.drawable.background);
LayoutInflater ltInflater = getLayoutInflater();
RelativeLayout rl = (RelativeLayout)ltInflater.inflate(R.layout.viewpager_layout, null);
PagerContainer pagerContainer = (PagerContainer)(rl.getChildAt(0));
CirclePageIndicator circlePageIndicator = (CirclePageIndicator)(rl.getChildAt(1));
Button button = (Button)(rl.getChildAt(2));
int maxWidth = 1;
int maxHeight = 1;
for (Drawable drawable: bigImages) {
Bitmap bmp = drawableToBitmap(drawable);
maxWidth = Math.max(maxWidth, bmp.getWidth());
maxHeight = Math.max(maxHeight, bmp.getHeight());
}
ViewPager viewPager = pagerContainer.getViewPager();
//viewPager.setBackgroundDrawable(image);
ArrayList<ImageView> tmpImageViewsWithScreenshots = (ArrayList<ImageView>) imageViewsWithScreenshots.clone();
SamplePagerAdapter pagerAdapter = new SamplePagerAdapter(tmpImageViewsWithScreenshots);
viewPager.setAdapter(pagerAdapter);
viewPager.setCurrentItem(imageIndex);
circlePageIndicator.setViewPager(viewPager);
circlePageIndicator.setCurrentItem(imageIndex);
button.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
int childCount = checkboxLayout.getChildCount();
for (int i = 0; i < childCount; ++i) {
RelativeLayout currentLayout = (RelativeLayout) checkboxLayout
.getChildAt(i);
int innerChildCount = currentLayout
.getChildCount();
for (int j = 0; j < innerChildCount; ++j) {
View view = currentLayout.getChildAt(j);
view.setEnabled(true);
}
}
imageViewLayout.setEnabled(true);
lockableScrollView.setmScrollable(true);
back.setEnabled(true);
next.setEnabled(true);
popupWindow.dismiss();
}
});
popupLayout.addView(rl);
LinearLayout.LayoutParams nlp= new LinearLayout.LayoutParams(450, 600);
rl.setLayoutParams(nlp);
popupWindow = new PopupWindow(popupLayout,
RelativeLayout.LayoutParams.WRAP_CONTENT,
RelativeLayout.LayoutParams.WRAP_CONTENT);
int childCount = checkboxLayout.getChildCount();
for (int i = 0; i < childCount; ++i) {
RelativeLayout currentLayout = (RelativeLayout) checkboxLayout
.getChildAt(i);
int innerChildCount = currentLayout.getChildCount();
for (int j = 0; j < innerChildCount; ++j) {
View view = currentLayout.getChildAt(j);
view.setEnabled(false);
}
}
next.setEnabled(false);
back.setEnabled(false);
imageViewLayout.setEnabled(false);
lockableScrollView.setmScrollable(false);
popupWindow
.showAtLocation(mainLayout, Gravity.CENTER, 0, 0);
}
这是我适配器的代码:
public class SamplePagerAdapter extends PagerAdapter{
List<ImageView> pages = null;
public SamplePagerAdapter(List<ImageView> pages){
this.pages = pages;
}
@Override
public Object instantiateItem(ViewGroup container, int position){
View v = pages.get(position);
//((ViewPager) collection).addView(v, 0);
container.addView(v);
return v;
}
@Override
public void destroyItem(ViewGroup container, int position, Object object){
container.removeView((View)object);
}
@Override
public int getCount(){
return pages.size();
}
@Override
public boolean isViewFromObject(View view, Object object){
return view.equals(object);
}
}
任何想法?
SamplePagerAdapter
中尝试这种方式:@Override public boolean isViewFromObject(View view, Object object) {return view == ((View) object);}
- M D