Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

Android

Lukas Baumgartner
Lukas Baumgartner
14,817 Points

Start new Fragment with ButtonClick from Fragment

I'm trying to start a different Fragment from a fragment but it won't work.

The error i get is:

java.lang.IllegalArgumentException: No view found for id 0x7f0e0073 (com.baumgartner.webtree.wizard:id/placeholder) for fragment GameSettingsFragment{3ff6b52 #2 id=0x7f0e0073}

with the fragment Ihave the button in:

   @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_startscreen, container, false);

        Button newGameButton = (Button) view.findViewById(R.id.newGameButton);
        newGameButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                    swapFragment();
            }
        });
        return view;
    }

    private void swapFragment(){
        GameSettingsFragment newGamefragment = new GameSettingsFragment();
        FragmentTransaction fragmentTransaction = getFragmentManager().beginTransaction();
        fragmentTransaction.replace(R.id.placeholder, newGamefragment);
        fragmentTransaction.addToBackStack(null);
        fragmentTransaction.commit();
    }

and my Fragment i want to show after the button is clicked

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_gamesettings, container,false);

        Spinner playerCountSpinner = (Spinner)view.findViewById(R.id.playerCountSpinner);
        ArrayAdapter<CharSequence>adapter = ArrayAdapter.createFromResource(getActivity().getBaseContext(),
                                                                            R.array.player_count,
                                                                            R.layout.support_simple_spinner_dropdown_item);
        adapter.setDropDownViewResource(R.layout.support_simple_spinner_dropdown_item);
        playerCountSpinner.setAdapter(adapter);

        return view;
    }
}

1 Answer

Lukas Baumgartner
Lukas Baumgartner
14,817 Points

This code works for me:

        GameSettingsFragment newGamefragment = new GameSettingsFragment();
        FragmentTransaction fragmentTransaction = getActivity().getSupportFragmentManager().beginTransaction();
        fragmentTransaction.replace(R.id.placeholder, newGamefragment);
        fragmentTransaction.addToBackStack(null);
        fragmentTransaction.commit();