2014-01-10 25 views
7

所以這是一個複雜的問題,但我會盡量讓它儘可能容易地遵循。View分頁器中的子片段在交換後消失

首先,我有一個查看尋呼機的主要活動。其中有2個頁面,其中2個操作欄選項卡鏈接每個頁面。 我也有一個抽屜有4個選項,前2個是視圖尋呼機中的2個頁面,第二個2是不是的頁面。如果您選擇第二個2中的一個,我將ViewPager配置爲使用其他2個碎片替換碎片。所有這些片段都是單例,所以來回切換不會導致任何內存問題。

在切換到第二組片段時,其中一個片段中有一個框架,用於在選擇按鈕時替換子片段。這是問題所在。第一次加載片段後,它可以正常工作。但是,如果您使用抽屜切換到第一組,然後再次回到第二組,則子片段消失。然後在你做任何事情之後,它往往會崩潰。

這是崩潰日誌爲它

01-10 15:55:05.272: E/MessageQueue-JNI(21034): java.lang.NullPointerException: Attempt to invoke virtual method 'void android.support.v4.app.FragmentManagerImpl.performPendingDeferredStart(android.support.v4.app.Fragment)' on a null object reference 

如果我與周圍的代碼有點玩,我得到這樣的事情。這兩個錯誤是我似乎產生最多的錯誤:

01-10 16:00:13.072: E/AndroidRuntime(22227): java.lang.NullPointerException: Attempt to invoke virtual method 'java.lang.ClassLoader android.support.v4.app.FragmentActivity.getClassLoader()' on a null object reference 

我可以繼續在幀視圖上調用replace(),所以它不會消​​失。所以我猜這不是問題。它

這是一些相關的代碼。

片段代碼:

public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { 
    View view = inflater.inflate(R.layout.calculator, container, false); 
    final FragmentManager fragmentManager = getChildFragmentManager(); 
    if(afFrag == null && topFrag == null){ 
     afFrag = CalculatorAfFragment.getInstance(); 
     topFrag = CalculatorTopFragment.getInstance(); 
     FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction(); 
     fragmentTransaction.replace(R.id.calc_frame, afFrag, "afFrag").commit(); 
    } 
    RadioGroup typeGroup = (RadioGroup) view.findViewById(R.id.type_buttons);   
    typeGroup.setOnCheckedChangeListener(new OnCheckedChangeListener() 
    { 
     public void onCheckedChanged(RadioGroup group, int checkedId) { 
      if (checkedId == R.id.af_button){ 
       FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction(); 
       fragmentTransaction.replace(R.id.calc_frame, afFrag, "afFrag").commit(); 
      } 
      else if (checkedId == R.id.top_button){ 
       FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction(); 
       fragmentTransaction.replace(R.id.calc_frame, topFrag, "topFrag").commit(); 
      } 
     } 
    }); 
    return view; 

的FragmentStatePagerAdapter代碼:

@Override 
    public Fragment getItem(int position) { 
     if(tools){ 
      return toolsFragmentsList.get(position); 
     } else { 
      return guidesFragmentsList.get(position); 
     } 
    } 

    @Override 
    public int getItemPosition(Object object) { 
     return POSITION_NONE; 
    } 

    public void swapFragments(int position){ 
     if (tools){ 
      tools = false; 
      stores = false; 
      notifyDataSetChanged(); 
      this.setPrimaryItem(mPager, position-1, getItem(position - 1)); 
     } else { 
      tools = true; 
      notifyDataSetChanged(); 
      this.setPrimaryItem(mPager, position-4, getItem(position - 4)); 
     } 
    } 
+0

您在調用「getClassLoader()」時出錯,因此必須在問題出現的地方。檢查您調用「getClassLoader()」的實例是否在此時完全初始化。 – Sulfkain

+0

請在logcat中顯示更多的日誌,我發現這個消息是最相關的。 – mobilepotato7

回答

0

嘗試通過mPager.setId(View.generateViewId())ViewPager設置一個唯一的ID。