1

I am trying to replace a Fragment inside another Fragment and I get a weird NullPointerException, could you please take a look and tell me what do you think?

Here is my code:

public class HomeScreenFragment extends Fragment implements FolderViewInterface {
//stuff before this

@Override
    public void onFolderOpened(int folder_index) {
        System.out.println("THe callback brought me here, passed index:"+folder_index);

        Fragment newFragment = new FolderViewFragment(); 
        // 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.fragment_container, newFragment);
        transaction.addToBackStack(null);

        // Commit the transaction
        transaction.commit(); 

    }
}

The stacktrace:

08-01 16:48:11.587: E/AndroidRuntime(14399): FATAL EXCEPTION: main
08-01 16:48:11.587: E/AndroidRuntime(14399): java.lang.NullPointerException
08-01 16:48:11.587: E/AndroidRuntime(14399):    at ro.gebs.captoom.fragments.HomeScreenFragment.onFolderOpened(HomeScreenFragment.java:320)
08-01 16:48:11.587: E/AndroidRuntime(14399):    at ro.gebs.captoom.utils.FolderListView.onInterceptTouchEvent(FolderListView.java:48)
08-01 16:48:11.587: E/AndroidRuntime(14399):    at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:2110)
08-01 16:48:11.587: E/AndroidRuntime(14399):    at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2470)
08-01 16:48:11.587: E/AndroidRuntime(14399):    at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:2212)
08-01 16:48:11.587: E/AndroidRuntime(14399):    at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2470)

I am getting here by callback, I am in a ListView context handling a touch event. Any suggestions are welcome.

Blo
  • 11,903
  • 5
  • 45
  • 99
Adrian Olar
  • 2,883
  • 4
  • 35
  • 63
  • you are using the native support for the fragment. Does the device where you are testing the application support it (has it a version of android grater then 3.0 ?) ? – Blackbelt Aug 01 '13 at 13:59
  • the exception is thrown here: `FragmentTransaction transaction = getFragmentManager().beginTransaction();` – Adrian Olar Aug 01 '13 at 14:01

1 Answers1

1

Your FragmentManager looks null. Try using the getChildFragmentManager().

Daan Olislagers
  • 3,253
  • 2
  • 17
  • 35
  • Solution: I implemented a callback to the main Activity which allows me to use getFragmentManager with no null pointer exception thrown. – Adrian Olar Aug 01 '13 at 14:34