IllegalArgumentException:No view found for id

676 views Asked by At

I get the following exception:
java.lang.IllegalArgumentException:
No view found for id 0x7f04001a (com.whatever:layout/fragment_main)

Looking at similar questions on SO:

One answer suggests using getChildFragmentManager() instead of getSupportFragmentManager(). This is not an option since MainActivity is not a Fragment.

Another answer suggests that fragment_main is not a child of activity_main, but I think I have that setup in activity_main.xml with tools:layout="@layout/fragment_main

Another suggests that setContent() has not been called, but I have that in onCreate()

What am I missing?

MainActivity.java

public class MainActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
    }

    @Override
    public void onStart() {
        super.onStart();

        // create fragment immediately
        FragmentManager fragmentManager = getSupportFragmentManager();
        FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();

        fragmentTransaction.add(R.layout.fragment_main, new MainActivityFragment());
        fragmentTransaction.commit();
        boolean wereThereWereAnyPendingTransactions = fragmentManager.executePendingTransactions();
    }
}

MainActivityFragment.java

public class MainActivityFragment extends Fragment  implements Observer<String> {
        public MainActivityFragment() {
        }

        @Override
        public View onCreateView(LayoutInflater inflater, ViewGroup container,Bundle savedInstanceState) {
          return inflater.inflate(R.layout.fragment_main, container, false);
}

activity_main.xml

<fragment xmlns:android="http://schemas.android.com/apk/res/android"
          xmlns:tools="http://schemas.android.com/tools"
          android:id="@+id/fragment"
          android:name="com.whatever.MainActivityFragment"
          tools:layout="@layout/fragment_main"
          android:layout_width="match_parent"
          android:layout_height="match_parent"/>

fragment_main.xml

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
                xmlns:tools="http://schemas.android.com/tools"
                android:id="@+id/fragment_main_table"
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:paddingLeft="@dimen/activity_horizontal_margin"
                android:paddingRight="@dimen/activity_horizontal_margin"
                android:paddingTop="@dimen/activity_vertical_margin"
                android:paddingBottom="@dimen/activity_vertical_margin"
                tools:context=".MainActivityFragment">

    <TextView
        android:text="@string/fragment_main_xml"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"/>

</RelativeLayout>

The answer with respect to code: actvity_main.xml

<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
             xmlns:tools="http://schemas.android.com/tools"
             android:id="@+id/activity_frame_layout"
             android:layout_width="fill_parent"
             android:layout_height="fill_parent"
             tools:context=".MainActivity" >
</FrameLayout>

MainActivity.java:

  ...
  fragmentTransaction.add(R.id.activity_frame_layout, new MainActivityFragment());
  ...
1

There are 1 answers

4
Larry Schiefer On BEST ANSWER

If you embed a Fragment inside of an Activity XML layout, you do not create it dynamically. The system does it for you. So there is no need to create the transaction and commit it, the Fragment will be created and added when you called setContentView().

Note that the tools:layout= line does not do anything at runtime. It is strictly for the tools to better link layouts and components together.