Android Fragment No view for id

I am trying to create a dual panel / single setup for an application. It worked well for a while, before I put all the hunted in fragments and I don't know why it didn't work. The problem arises when I do an orientation change. It didn't recreate the show at first for some reason. It will call onCreateView, but I was unable to get the handle to the view inside the view and got null (I know I have the correct layouts for landscape and portrait as it works in both portrait and landscape if it starts there ). So what I did was add setRetainInstance to true, thinking it would fix my problem. Well this caused another problem, now I am getting No View Found For Id.

Now I think that it is trying to bind itself to the ID that it had before the orientation change, and it does not exist as it does now in the other layout. I tried just creating a new snippet and adding it, but that doesn't work either. I'm bordering on my hair while trying to work with Android, I thought of a fragment system that is almost impossible to work with. Any help would be greatly appreciated. Here is the relevant code

@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();
    //}
}

      

Ive tried to find a fragment with a fragment manager and remap it to a different layout, but for some reason it is still looking for the old one.

+3


source to share


1 answer


You should rewrite your code like this:



    //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();

    }

      

+2


source







All Articles