Domanda

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.

È stato utile?

Soluzione

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

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top