2014-04-08 72 views
3

我有一個ViewPager,它是一個彈出窗口的一部分。當我第一次打開一個彈出窗口時,一切正常,但是當我第二次用相同的數據打開它時,我有一個IllegalStateException錯誤。在Android中使用ViewPager時發生java.lang.IllegalStateException

這裏是一個堆棧跟蹤:

04-08 15:17:48.370:E/AndroidRuntime(10929):致命異常:主 04-08 15:17:48.370:E/AndroidRuntime( 10929): java.lang.IllegalStateException:指定的子項已具有 父級。您必須先調用子對象的父對象的removeView()。 04-08 15:17:48.370:E/AndroidRuntime(10929):at android.view.ViewGroup.addViewInner(ViewGroup.java:3337)04-08 15:17:48.370:E/AndroidRuntime(10929):在 android.view.ViewGroup.addView(ViewGroup.java:3208)04-08 15:17:48.370:E/AndroidRuntime(10929):at android.support.v4.view.ViewPager.addView(ViewPager.java :920)04-08 15:17:48.370:E/AndroidRuntime(10929):at android.view.ViewGroup.addView(ViewGroup.java:3165)04-08 15:17:48.370:E/AndroidRuntime 10929):at android.view.ViewGroup.addView(ViewGroup.java:3145)04-08 15:17:48.370:E/AndroidRuntime(10929):at com.componentix.imwizard.SamplePagerAdapter.instantia teItem(SamplePagerAdapter.java:22) 04-08 15:17:48.370:E/AndroidRuntime(10929):at android.support.v4.view.ViewPager.addNewItem(ViewPager.java:649)04-08 15 :17:48.370:E/AndroidRuntime(10929):at android.support.v4.view.ViewPager.populate(ViewPager.java:783)04-08 15:17:48.370:E/AndroidRuntime(10929):at android.support.v4.view.ViewPager.completeScroll(ViewPager.java:1280) 04-08 15:17:48.370:E/AndroidRuntime(10929):at android.support.v4.view.ViewPager.computeScroll( ViewPager.java:1176) 04-08 15:17:48.370:E/AndroidRuntime(10929):at android.view.ViewGroup.drawChild(ViewGroup.java:2729)04-08 15:17:48.370:E/AndroidRuntime(10929):在.android.view.ViewGroup.dispatchDraw(ViewGroup.java:2489)04-08 15:17:48.370:E/AndroidRuntime(10929):at android.view.View.buildDrawingCache(View.java:10693)04- 08 15:17:48.370:E/AndroidRuntime(10929):at android.view.ViewGroup.drawChild(ViewGroup.java:2747)04-08 15:17:48.370:E/AndroidRuntime(10929):at android.view.View.Group.dispatchDraw(ViewGroup.java:2489)04-08 15:17:48.370:E/AndroidRuntime(10929):at android.view.View.draw(View.java:10981)04-08 15/17:48.370: E/AndroidRuntime(10929):at android.view.View.getDisplayList(View.java:10417)04-08 15:17:48.370: E/AndroidRuntime(10929):at android.view.ViewGroup.dispatchGetDisplayList(ViewGroup.java:2597) 04-08 15:17:48.370:E/AndroidRuntime(10929):at android.view.ViewDisplayList(View.java:10380)04- 08 15:17:48.370: E/AndroidRuntime(10929):at android.view.HardwareRenderer $ GlRenderer.draw(HardwareRenderer.java:842) 04-08 15:17:48.370:E/AndroidRuntime(10929):在 android.view.ViewRootImpl.draw(ViewRootImpl.java:1910)04-08 15:17:48.370:E/AndroidRuntime(10929):在 android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:1634) 04-08 15:17:48.370:E/AndroidRuntime(10929):在 android.view.ViewRootImpl.handleMessage(ViewRootImpl。java:2442)04-08 15:17:48.370:E/AndroidRuntime(10929):at android.os.Handler.dispatchMessage(Handler.java:99)04-08 15:17:48.370:E/AndroidRuntime (10929):at android.os.Looper.loop(Looper.java:137)04-08 15:17:48.370: E/AndroidRuntime(10929):at android.app.ActivityThread.main(ActivityThread.java :4430)04-08 15:17:48.370:E/AndroidRuntime(10929):at java.lang.reflect.Method.invokeNative(Native Method)04-08 15:17:48.370:E/AndroidRuntime(10929) ): java.lang.reflect.Method.invoke(Method.java:511)04-08 15:17:48.370: E/AndroidRuntime(10929):at com.android.internal.os.ZygoteInit $ MethodAndArgsCaller .RUN(合子Init.java:792) 04-08 15:17:48.370:E/AndroidRuntime(10929):at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:559)04-08 15:17 :48.370:E/AndroidRuntime(10929):在 dalvik.system.NativeStart.main(本機方法)

這裏是我的代碼:

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

} 

任何想法?

+0

嘗試這樣'@覆蓋 公共布爾isViewFromObject(查看視圖,Object對象){返回查看==((查看)對象);}'在你的'SamplePagerAdapter' –

+0

@SimplePlan仍然不起作用 –

回答

3

的問題是,在你的適配器方法instantiateItem
你叫container.addView(v);

但每View只能有一個家長,
所以它只能一次通過addView(...)被添加到一個容器中。

當您第一次打開彈出窗口時,一切正常,因爲v
當時沒有父母。但是,當您第二次打開popupwinow時,它將再次向容器添加視圖。這消除了錯誤。

嘗試,如果你從它關閉彈出查看或刪除所有兒童的意見與
container.removeAllViews()

它應該工作,然後摧毀視圖。

+0

是的,我這麼認爲,但我該如何解決這個問題? –

+0

@MykhailoGranik我已經更新了答案 – bricklore

+0

是的,謝謝,您的解決方案有效。 ViewGroup parent =(ViewGroup)v.getParent();如果(parent!= null){ } \t parent.removeAllViews(); } –

2

問題是當您第二次在container.addView(v)中添加View v時。

我已經找到了解決方案添加此:

 if(v.getParent()!=null) 
     ((ViewGroup)v.getParent()).removeView(v); 

之前container.addView(V)

相關問題