I'm trying to use the following code to open a Fragment
that is inside another Fragment but is gives me a Null Pointer Exception with no cause.
Fragment newFragment = new SecondFragment();
// consider using Java coding conventions (upper char class names!!!)
FragmentTransaction transaction = getFragmentManager().beginTransaction();
// Replace whatever is in the fragment_container view with this fragment,
// and add the transaction to the back stack
transaction.replace(R.id.menuitem_detail_container, newFragment);
transaction.addToBackStack(null);
// Commit the transaction
transaction.commit();
This piece of code is inside a basic onItemClickListener
inside the onCreateView
method inside of a Fragment (android.support.v4.app.Fragment
)
Any ideas on what I might be doing wrong?
When you use nested fragments you need to getChildFragmentManager() for the inner fragments.
Also make sure you're importing from the support library for both the FragmentManager and ChildFragmentManager.