Reputation: 2179
I have a ViewPager
in my main activity that show some Fragment
s when I select a tab. Each fragment contains some buttons that when I click on one of them, it will replace a fragment with current fragment.
What did I do is that I build a Fragment that it acts like a Fragment container.
database_fragment_container.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:id="@+id/database_fragment_container">
</LinearLayout>
DatabaseFragmentContainer.Java
public class DatabaseFragmentContainer extends Fragment {
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// TODO Auto-generated method stub
View view = inflater.inflate(R.layout.database_fragment_container, container, false);
getFragmentManager().beginTransaction().replace(R.id.database_fragment_container, new DatabaseFragment()).commit();
return view;
}
}
I wrote an onClickListener
that When I click on a button in DatabaseFragment
, It gets parent fragment and replace another fragment with R.id.database_fragment_container
DatabaseFragment.Java
public class DatabaseFragment extends Fragment {
public static DatabaseFragmentContainer parentFragment;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.database_fragment, container, false);
((CirculeButtonWithCaption)view.findViewById(R.id.raw_materials_button)).setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View arg0) {
getParentFragment().getChildFragmentManager().beginTransaction().replace(R.id.database_fragment_container, new RawMaterialsFragment()).commit();
}
});
return view;
}
}
And at last, it's my FragmentPagerAdapter
:
public class MainActivityTabsPagerAdapter extends FragmentPagerAdapter {
public MainActivityTabsPagerAdapter(FragmentManager fm) {
super(fm);
}
@Override
public Fragment getItem(int i) {
switch (i) {
case 0:
return new DatabaseFragmentContainer();
case 1:
return new CostumerFragment();
default:
break;
}
return null;
}
@Override
public int getCount() {
// TODO Auto-generated method stub
return 2;
}
}
The problem is that when I click on a button, NullPointerException
thrown becuase of getParentFragment()
return null
! What should I do?
P.S: I know that my English is bad and my question has some grammer issues. But please edit it gently and don't put my question [on hold]. I need the Answer :D
Upvotes: 1
Views: 4593
Reputation: 1141
I am sure is too late to answer but I had the same problem and I resolve it getting the reference of the parent fragment in the list of fragments from FragmentManager.
XFragment xFragment = null;
for(Fragment fragment : getFragmentManager().getFragments()){
if(fragment instanceof XFragment){
xFragment = (XFragment) fragment;
break;
}
}
XFragment is the parent fragment to search.
It is not the faster way but I don't find another.
Upvotes: 1
Reputation: 4333
The problem seems to be that you add the fragment using the fragment manager but then inside the new fragment you call the child fragment manager which will be null because it wasn't used. Try just calling get fragment manager.
Upvotes: 0