-2

this code is giving me a null pointer please help me fix it.

private void homeFragment(Profile profile) {
    if (profile != null) {
        Bundle mBundle = new Bundle();
        mBundle.putParcelable(PARCEL_KEY, profile);
        HomeFragment hf = new HomeFragment();
        hf.setArguments(mBundle);

        FragmentManager fragmentManager = getActivity().getSupportFragmentManager();
        FragmentTransaction fragmentTransaction = fragmentManager
                .beginTransaction();
        fragmentTransaction.replace(R.id.mainContainer, new HomeFragment());
        fragmentTransaction.commit();
    }
}
Phantômaxx
  • 37,901
  • 21
  • 84
  • 115

1 Answers1

0

The code you are writing should be in a class similar to this:

import android.support.v4.app.FragmentActivity;
public class YourFragmentActivity extends FragmentActivity

Then getSupportFragmentManager() is visible without getActivity(), e.g.

getSupportFragmentManager().beginTransaction().replace(R.id.frame_container, mBullet_frag).commit();

You must include the v4 library in the final build (just find it in the sdk and drop it in your project libs folder).

This will work, if you follow all the rules.

Of course it could be you did not intend to use getSupportFragmentManager() but wanted the standard one (getFragmentManager()). In this case you don't need the v4 library, or reference to it.

Jon Goodwin
  • 9,053
  • 5
  • 35
  • 54