javaandroidxmlandroid-fragmentsandroid-identifiers

"No View Found For ID" - Android Fragment


So I'm working on an app for a friend and it keeps crashing and is unable to run on my device (OnePlus One).

I keep getting an error here saying "No view found for id": Logcat view for Error

I have two classes; a NavigationDrawer class (which is my essentially my MainActivity):

Java --- http://pastebin.com/4BiYvxiS | XML --- http://pastebin.com/Fnat83uf

And a class named StartingFragment (which I want to be the main view when the drawer is closed/not active).

Java --- http://pastebin.com/HNeHzx1h | XML --- http://pastebin.com/2viTEWR8

Here is the code in which there is an error (from NavDrawer.java):

        /** Swaps fragments in the main content view */
        /** Starts an Activity when item is clicked */
        private void selectItem(int position) {
                // Create a new fragment and specify the tea type
                // to show based on position
                Fragment fragment = new StartingFragment();
                Bundle args = new Bundle();
                args.putInt(StartingFragment.TEA_TYPE_POS, position);
                fragment.setArguments(args);

                // Insert the fragment by replacing any existing fragment
                FragmentManager fragManager = getFragmentManager();
                fragManager.beginTransaction().replace(R.id.starting_fragment, fragment)
                                .commit();

                // Highlight the selected item, update the title, and close the drawer
                mDrawerList.setItemChecked(position, true);
                setTitle(navDrawerTitles[position]);
                navDrawerLayout.closeDrawer(mDrawerList);

        }

I've looked all over StackOverflow and I see how other's are getting this error, but I'm not entirely sure of why I keep getting this error, or how to fix it.

I looked at this question here: https://stackoverflow.com/a/8158916 and I see that R.id.starting_fragment should be a child of the R.layout.nav_drawer.

However, I don't know what to adjust in my code. Should I remove the Buttons I have and then have fragment code in my nav_drawer.xml? Such as this:

<fragment android:name="com.fv4.app.StartingFragment"
              android:id="@+id/starting_fragment"
              android:layout_width="0dp"
              android:layout_height="match_parent" />

Or really...what should I be replacing R.id.starting_fragment with in my code?


Solution

  • In the xml you gave for your activity there's no nothing with the id 'starting_fragment' therefore nothing to replace, in the xml for the activity there needs to be a view/layout/fragmentContainer with the id 'starting_fragment'. Also you're a bit confused, the Id in the .replace() method represents the thing you're going to replace not the layout of the fragment

    Simple fix

    in xml for the Activity create a frameLayout, give it the id 'fragment_replace'

    Change .replace(R.id.starting_fragment,fragment) to .replace(R.id.fragment_replace,fragment)