user7153966
user7153966

Reputation:

How do I fix: No view found for id 0x7f0d00aa?

I've seen people with the same issue and they said that there is something wrong with the .replace() and the way I've constructed the way the fragment and container are written using the: fragmentManager.beginTransaction().replace (R.id.container, new Fragment()).commit(); And that I must do it some other way, which I implemented into my code but I still got the Error. ERROR:

E/AndroidRuntime: FATAL EXCEPTION: main
              Process: mcshreker.pocketmath, PID: 3215
              java.lang.IllegalArgumentException: No view found for id 0x7f0d00aa (mcshreker.pocketmath:id/content_main_container) for fragment secondFragment{bf0f022 #0 id=0x7f0d00aa}
                  at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1292)
                  at android.support.v4.app.FragmentManagerImpl.moveFragmentsToInvisible(FragmentManager.java:2323)
                  at android.support.v4.app.FragmentManagerImpl.executeOpsTogether(FragmentManager.java:2136)
                  at android.support.v4.app.FragmentManagerImpl.optimizeAndExecuteOps(FragmentManager.java:2092)
                  at android.support.v4.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:1998)
                  at android.support.v4.app.FragmentManagerImpl$1.run(FragmentManager.java:709)
                  at android.os.Handler.handleCallback(Handler.java:751)
                  at android.os.Handler.dispatchMessage(Handler.java:95)
                  at android.os.Looper.loop(Looper.java:154)
                  at android.app.ActivityThread.main(ActivityThread.java:6119)
                  at java.lang.reflect.Method.invoke(Native Method)
                  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:886)
                  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:776)

Code (Main Activity)

package mcshreker.pocketmath;

import android.app.FragmentTransaction;
import android.os.Build;
import android.os.Bundle;
import android.support.annotation.RequiresApi;
import android.support.v4.app.FragmentManager;
import android.support.design.widget.NavigationView;
import android.support.v4.view.GravityCompat;
import android.support.v4.widget.DrawerLayout;
import android.support.v7.app.ActionBarDrawerToggle;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.view.Menu;
import android.view.MenuItem;

public class MainActivity extends AppCompatActivity
        implements NavigationView.OnNavigationItemSelectedListener {

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
    setSupportActionBar(toolbar);

    DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
    ActionBarDrawerToggle toggle = new ActionBarDrawerToggle(
            this, drawer, toolbar, R.string.navigation_drawer_open, R.string.navigation_drawer_close);
    drawer.setDrawerListener(toggle);
    toggle.syncState();

    NavigationView navigationView = (NavigationView) findViewById(R.id.nav_view);
    navigationView.setNavigationItemSelectedListener(this);

}

@Override
public void onBackPressed() {
    DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
    if (drawer.isDrawerOpen(GravityCompat.START)) {
        drawer.closeDrawer(GravityCompat.START);
    } else {
        super.onBackPressed();
    }
}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.main, menu);
    return true;
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    // Handle action bar item clicks here. The action bar will
    // automatically handle clicks on the Home/Up button, so long
    // as you specify a parent activity in AndroidManifest.xml.
    int id = item.getItemId();

    //noinspection SimplifiableIfStatement


@RequiresApi(api = Build.VERSION_CODES.M)
@SuppressWarnings("StatementWithEmptyBody")
@Override
public boolean onNavigationItemSelected(MenuItem item) {
    // Handle navigation view item clicks here.
    int id = item.getItemId();
return super.onOptionsItemSelected(item);
    FragmentManager fragmentManager = getSupportFragmentManager();

    if (id == R.id.nav_first_layout) {
        fragmentManager.beginTransaction().replace
                (R.id.content_main_container, new firstFragment()).commit();

        // Handle the camera action

}

    } else if (id == R.id.nav_second_layout) {
        fragmentManager.beginTransaction().replace
                (R.id.content_main_container, new secondFragment()).commit();
    } else if (id == R.id.nav_third_layout) {
        fragmentManager.beginTransaction().replace
                (R.id.content_main_container, new thirdFragment()).commit();
    } else if (id == R.id.nav_fourth_layout) {
        fragmentManager.beginTransaction().replace
                (R.id.content_main_container, new fourthFragment()).commit();
    } else if (id == R.id.nav_fifth_layout) {
        fragmentManager.beginTransaction().replace
                (R.id.content_main_container, new fifthFragment()).commit();
    } else if (id == R.id.nav_sixth_layout) {
        fragmentManager.beginTransaction().replace
                (R.id.content_main_container, new sixthFragment()).commit();
    } else if (id == R.id.nav_seventh_layout) {
        fragmentManager.beginTransaction().replace
                (R.id.content_main_container, new seventhFragment()).commit();
    } else if (id == R.id.nav_eighth_layout) {
        fragmentManager.beginTransaction().replace
                (R.id.content_main_container, new eighthFragment()).commit();
    } else if (id == R.id.nav_ninth_layout) {
        fragmentManager.beginTransaction().replace
                (R.id.content_main_container, new ninthFragment()).commit();
    }

    DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
    drawer.closeDrawer(GravityCompat.START);
    return true;
}
}

HERE is my content_main_container XML:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/content_main_container"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    app:layout_behavior="@string/appbar_scrolling_view_behavior"
    tools:context="mcshreker.pocketmath.MainActivity">

    <LinearLayout
        android:orientation="vertical"
        android:layout_width="match_parent"
        android:layout_height="match_parent">

        <ImageView
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            app:srcCompat="@drawable/e"
            android:id="@+id/imageView7"
            android:layout_marginTop="96dp"
            android:layout_alignParentTop="true"
            android:layout_centerHorizontal="true"
            android:contentDescription=""
            tools:ignore="ContentDescription" />

        <ImageView
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            app:srcCompat="@drawable/welcome"
            android:id="@+id/imageView2"
            android:layout_alignParentTop="true"
            android:layout_alignParentLeft="true"
            android:layout_alignParentStart="true"
            tools:ignore="ContentDescription" />
    </LinearLayout>

</RelativeLayout>

Here is my Activity_main XML:

 <?xml version="1.0" encoding="utf-8"?>
    <android.support.v4.widget.DrawerLayout
        xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:app="http://schemas.android.com/apk/res-auto"
        xmlns:tools="http://schemas.android.com/tools"
        android:id="@+id/drawer_layout"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:fitsSystemWindows="true"
        tools:openDrawer="start">

    <include
        layout="@layout/app_bar_main"
        android:layout_width="match_parent"
        android:layout_height="match_parent" />

    <android.support.design.widget.NavigationView
        android:id="@+id/nav_view"
        android:layout_width="wrap_content"
        android:layout_height="match_parent"
        android:layout_gravity="start"
        android:fitsSystemWindows="true"
        app:headerLayout="@layout/nav_header_main"
        app:menu="@menu/activity_main_drawer" />

        </android.support.v4.widget.DrawerLayout>

Help is greatly appreciated!

Upvotes: 0

Views: 104

Answers (1)

Manza
Manza

Reputation: 3527

No view found for id 0x7f0d00aa (mcshreker.pocketmath:id/content_main_container)

check if content_main_container is defined in your activity_main layout

Upvotes: 2

Related Questions