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.

2
  • 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 ?) ? Commented Aug 1, 2013 at 13:59
  • the exception is thrown here: FragmentTransaction transaction = getFragmentManager().beginTransaction(); Commented Aug 1, 2013 at 14:01

1 Answer 1

1

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

Sign up to request clarification or add additional context in comments.

1 Comment

Solution: I implemented a callback to the main Activity which allows me to use getFragmentManager with no null pointer exception thrown.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.