2013-02-17 132 views
3

我正在尝试为应用程序创建双窗格/单个窗格设置。在我把所有的内脏放入碎片之前,它工作得很好,我不知道是什么导致它不能工作。当我进行方向变更时,问题就在发生。起初,它不是由于某种原因重新创建视图。它会调用onCreateView,但我无法获得视图内的列表视图的处理,并得到空(我知道我有布局正确的风景和肖像,因为它在纵向和横向工作,如果它开始在那里)。所以,我所做的是将setRetainInstance添加为true,认为这可以解决我的问题。那么,现在我得到了没有发现身份证的其他问题。Android碎片未找到id为id

我认为现在发生的事情是,它试图将自己重新附加到方向改变之前的ID中,并且它不像现在在不同的布局上那样存在。我试着创建一个新的片段并添加它,但这也不起作用。我边缘拉着我的头发试图与Android的工作有思想片段系统,几乎不可能合作。任何帮助,将不胜感激。下面是相关代码

@Override 
public void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    this.setContentView(R.layout.study_guide); 
    //Create The Ad Service 

    //Check to see if the view is landscape or portrait 
    if (this.findViewById(R.id.singlePaneStudyGuide) != null) { 
     mDualPane = false; 
    } else if (this.findViewById(R.id.doublePaneStudyGuide) != null) { 
     mDualPane = true; 
    } 
    LinearLayout layout = (LinearLayout)findViewById(R.id.addbox); 
    adView = new AdView(this,AdSize.SMART_BANNER,"a1511f0352b42bb"); 
    layout.addView(adView); 
    AdRequest r = new AdRequest(); 
    r.setTesting(true); 
    adView.loadAd(r); 



    //Inflate Fragments depending on which is selected 
    //if (savedInstanceState == null) { 
     FragmentManager fm = this.getSupportFragmentManager(); 
     FragmentTransaction ft = fm.beginTransaction(); 

     SGListFragment newFrag = new SGListFragment(); 

     if (mDualPane == true) { 
      ft.add(R.id.sgScrollContainer, newFrag, "SgListView").commit(); 
     } else { 
      ft.add(R.id.singlePaneStudyGuide, newFrag, "SgListView").commit(); 

     } 
     fm.executePendingTransactions(); 
    //} 
} 

我用尽寻找与该片段经理片段和重新分配给不同的布局,但由于某种原因,它仍然在寻找旧的。

+1

请发表您的代码 – Kerry 2013-02-17 08:22:44

+0

也请添加study_guide.xml的内容为纵向和横向 – 2013-02-18 17:41:30

回答

2

你必须重写代码如下:

//Inflate Fragments depending on which is selected 
    //if (savedInstanceState == null) { 
    FragmentManager fm = this.getSupportFragmentManager(); 
    FragmentTransaction ft = fm.beginTransaction(); 
    // here the trick starts 
    int oldId = (mDualPane == true) ? R.id.singlePaneStudyGuide 
            : R.id.sgScrollContainer; 
    Fragment oldFragment = fm.findFragmentById(oldId); 
    if(null != oldFragment){ 
     ft.remove(oldFragment); 
     ft.commit(); 
     fm.executePendingTransactions(); 
     ft = fragmentManager.beginTransaction(); 
    } 
    // end of tricks 
    SGListFragment newFrag = new SGListFragment(); 

    if (mDualPane == true) { 
     ft.add(R.id.sgScrollContainer, newFrag, "SgListView").commit(); 
    } else { 
     ft.add(R.id.singlePaneStudyGuide, newFrag, "SgListView").commit(); 

    } 
+0

我设法用findView而不是getActivity(修复它)片段端的.findViewById。我会在本周的某个时候尝试,看看它是否更好。现在当我改变方向时,它会丢失原来的位置,并开始一个新的片段而不是旧的片段。所以,我会给你一个镜头,让你知道。谢谢 – Shaun 2013-03-05 19:03:31

+0

为了让片段在配置更改期间生存,请尝试setRetainInstance(true)(http://developer.android.com/reference/android/app/Fragment.html#setRetainInstance(boolean))。另外在这里查看关于片段生命周期的更多信息:http://marakana.com/s/post/1250/android_fragments_tutorial – 2013-03-05 20:34:20

+0

是的,setRetainInstance(true)仍然导致FC。它寻找的观点,它最初是在,而不是新的... – Shaun 2013-03-06 00:14:34