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

java.lang.IllegalArgumentException: No view found for id 0x7f0c0050

Using regular relative layout for activit_main.xml & FrameLayout with an id of active_main in fragment_example.xml just showing a textview in a relative layout. Then calling all of this:

FragmentExample fragment = new FragmentExample();
        FragmentManager fragmentManager = getFragmentManager();
        //Replace intent with Bundle and put it in the transaction
        FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
        fragmentTransaction.add(R.id.active_main, fragment);
        fragmentTransaction.replace(R.id.active_main,fragment);
        fragmentTransaction.commit();
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/active_main"
    android:orientation="vertical"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent">

        <TextView
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:text="HELLO WORLD"
            android:textSize="50sp"
            android:gravity="center"
            android:layout_centerVertical="true"
            android:layout_alignParentLeft="true"
            android:layout_alignParentStart="true" />

    </RelativeLayout>

</FrameLayout>

I have to be honest when following the video step for step and things still go wrong. I'm sure others can agree it can be frustrating... :/

2 Answers

Seth Kroger
Seth Kroger
56,413 Points

The id you use in add() or replace() should be the id of the container in activity_main.xml you want to place the fragment into. The fragment's xml layout hasn't been loaded yet, and is only accessed by the Fragment, so you won't be able to reference any views with id's in the fragment itself from the activity.

I just tried it, and it worked. I had to add a FrameLayout to the activity_main.xml. Then replaced that id with the one from the framelayout. It works, so thank you tons.