Search code examples
androidandroid-fragmentslayout-inflater

cannot inflate layout in onCreateView of fragment


In my MenuContentActivity, I call the IntercomFragment like the following:

IntercomFragment myContent= IntercomFragment.newInstance(index);
FragmentTransaction ft = getFragmentManager().beginTransaction();
ft.add(android.R.id.content, myContent, "intercomFrag").commit();

Then in my IntercomFragment, I try to inflate the layout "intercom", but it fails! It throws "java.lang.RuntimeException: Unable to start activity ComponentInfo{com.mycompany.myapp/com.mycompany.myapp.MenuContentActivity}: android.content.res.Resources$NotFoundException: Resource ID #0x7f030030"

@Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {

        View mainView = inflater.inflate(R.layout.intercom, container, false);
......
......
......
}

inflater.inflate fails. R.layout.intercom exsits, intercom.xml is already in the layout directory. I can call and show my other fragments in the same way, but i didnt understand why i cannot call and show the IntercomFragment in the same way. What is this error, any idea?


Solution

  • I realized that my application doesn't crash in landspace mode then i suspected from intercom layout itself.

    The layout includes other layouts with <include layout=""......\>

    I realized that one of the included layout has only landspace file(in layout-land folder). So i guess because of that when loading the layout file it cannot find the portrait version of the included layout so it fails.

    I just added the missing layout file and it is solved now!