2013-11-23 42 views
0

我想在手機沒有連接到WiFi網絡時顯示另一個片段。通常情況下,我顯示MainFragment,當沒有連接可用ErrorFragment。我使用mWifi.isConnected()來檢查是否有連接。IlegalStateException當試圖替換片段

@Override 
public void onViewCreated(View view, Bundle savedInstanceState) { 
    if (mWifi.isConnected()) { 
     new PostTask(1).execute(); 
    } else { 
     MainFragment.firstPageListener.onSwitchToNextFragment(); 
    } 
} 

這是我的代碼(內MainFragment)切換到ErrorFragment,但它給我

11-23 20:57:29.588: E/AndroidRuntime(4025): java.lang.IllegalStateException: Recursive entry to executePendingTransactions 

這是我的MainActivity(我使用ViewPager):

public class SectionsPagerAdapter extends FragmentPagerAdapter { 

    private final class FirstPageListener implements FirstPageFragmentListener { 
     public void onSwitchToNextFragment() { 
      mFragmentManager.beginTransaction().remove(mFragmentAtPos0).commit(); 
      if (mFragmentAtPos0 instanceof MainFragment) { 
       mFragmentAtPos0 = new ErrorFragment(listener); 
      } else { // Instance of NextFragment 
       mFragmentAtPos0 = new MainFragment(listener); 
      } 
      notifyDataSetChanged(); 
     } 
    } 

    private String[] titles = { "SEARCH", "LIST" }; 
    private final FragmentManager mFragmentManager; 
    public Fragment mFragmentAtPos0; 
    private Context context; 
    FirstPageListener listener = new FirstPageListener(); 

    public SectionsPagerAdapter(FragmentManager fm) { 
     super(fm); 
     mFragmentManager = fm; 
    } 

    @Override 
    public Fragment getItem(int position) { 
     switch (position) { 
     case 0: // Fragment # 0 
      if (mFragmentAtPos0 == null) { 
       mFragmentAtPos0 = new MainFragment(listener); 
      } 
      return mFragmentAtPos0; 

     case 1: // Fragment # 1 
      return new ListFragment(); 
     } 
     return null; 
    } 

    @Override 
    public int getCount() { 
     return titles.length; 
    } 

    @Override 
    public int getItemPosition(Object object) { 
     if (object instanceof MainFragment && mFragmentAtPos0 instanceof ErrorFragment) { 
      return POSITION_NONE; 
     } 
     if (object instanceof MainFragment && mFragmentAtPos0 instanceof MainFragment) { 
      return POSITION_NONE; 
     } 
     return POSITION_UNCHANGED; 
    } 

    @Override 
    public CharSequence getPageTitle(int position) { 
     return titles[position]; 
    } 
} 

public interface MainFragmentListener { 
    void onSwitchToNextFragment(); 
} 

我試圖改變片段,片段:

FragmentManager fragmentManager2 = getFragmentManager(); 
FragmentTransaction fragmentTransaction2 = fragmentManager2.beginTransaction(); 
ErrorFragment fragment2 = new ErrorFragment(); 
fragmentTransaction2.addToBackStack("xyz"); 
fragmentTransaction2.hide(MainFragment.this); 
fragmentTransaction2.add(android.R.id.content, fragment2); 
fragmentTransaction2.commit(); 

但是當我滑動到另一個選項卡時,碎片重疊...應該怎麼做?謝謝!

(的拇指向下是ErrorFragment,按鈕和On/Off開關,在ListFragment)

enter image description here

回答

0

儘量去除 fragmentTransaction2.hide(MainFragment.this); 和更改 fragmentTransaction2.add(android.R.id.content,fragment2); 至 fragmentTransaction2.replace(android.R.id.content,fragment2);

+0

大拇指向下片段現在與兩個其他片段重疊... – moritzg