2014-04-24 84 views
0

我有以下的android代碼。它在我向前瀏覽標籤時正常工作,但只要我嘗試向後滑動應用程序崩潰。 問題在線「rootView =(ScrollView)gTabView.get(t);」當它試圖從全局變量第二次分配視圖時。 我該如何避免這種情況? 謝謝動態標籤內容與片段

public class DocPageActivity extends FragmentActivity { 
    DemoCollectionPagerAdapter mDemoCollectionPagerAdapter; 
    ViewPager mViewPager; 


    public static ActionBar actionBar = null; 
    public static int gtab=0; 
    public static ArrayList gTabView = new ArrayList(); 


    /** Called when the activity is first created. */ 
    @Override 
    public void onCreate(Bundle savedInstanceState) { 
     //InstState = savedInstanceState; 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.pager_obj); 
     gTabView.clear(); 
     gtab = 0; 
     BuildTabsContents();//creates view to dislay in each tab in gTabView,store countNo of tabs in gtab 
     mDemoCollectionPagerAdapter = new DemoCollectionPagerAdapter(getSupportFragmentManager()); 

     // Set up action bar. 
     final ActionBar actionBar = getActionBar(); 

     // Specify that the Home button should show an "Up" caret, indicating that touching the 
     // button will take the user one step up in the application's hierarchy. 
     actionBar.setDisplayHomeAsUpEnabled(true); 

     // Set up the ViewPager, attaching the adapter. 
     mViewPager = (ViewPager) findViewById(R.id.pager); 


     mViewPager.setAdapter(mDemoCollectionPagerAdapter); 
} 


    /** 
    * A {@link android.support.v4.app.FragmentStatePagerAdapter} that returns a fragment 
    * representing an object in the collection. 
    */ 

    public static class DemoCollectionPagerAdapter extends FragmentStatePagerAdapter { 


     public DemoCollectionPagerAdapter(FragmentManager fm) { 
      super(fm); 
     } 

     @Override 
     public Fragment getItem(int i) { 
      Fragment fragment = new DemoObjectFragment(); 
      Bundle args = new Bundle(); 
      args.putInt(DemoObjectFragment.ARG_OBJECT, i); 

      fragment.setArguments(args); 
      return fragment; 
     } 

     @Override 
     public int getCount() { 
      return gtab; 
     } 

     @Override 
     public CharSequence getPageTitle(int position) { 

      return "TAB " + (position + 1); 
     } 
    } 


    /** 
    * A dummy fragment representing a section of the app, but that simply displays dummy text. 
    */ 
    public static class DemoObjectFragment extends Fragment { 
     public static final String ARG_OBJECT = "object"; 

     @Override 
     public View onCreateView(LayoutInflater inflater, ViewGroup container, 
       Bundle savedInstanceState) { 
      View rootView; 
      Bundle args = getArguments(); 
      int t = args.getInt(ARG_OBJECT);    

      rootView = (ScrollView) gTabView.get(t); 

      return rootView;   

     } 
    } 
} 

回答

0

我已經在類級別和壓倒一切的方法如下聲明rootview解決了我的問題。

@Override 
    public void onDestroyView() { 
    super.onDestroyView(); 
    if (rootView != null) { 

     ViewGroup parentViewGroup = (ViewGroup) rootView.getParent(); 
     if (parentViewGroup != null) { 
      parentViewGroup.removeAllViews(); 
     } 
    } 
}