"No view found for id" error when trying to open a Fragment on android

2

I have a problem trying to load a fragment, I use a navigation drawer, where I have different options: When I click on the Facebook tab, it has to open a fragment, the prolema is that when I click on the option the application closes, the error that shows is:

java.lang.IllegalArgumentException: No view found for id 0x7f0d008b (com.example.enriq.interfaces_de_usuario_enrique_espinosa:id/fragmentf) for fragment facebookfragment{5cdf54f #0 id=0x7f0d008b}

This is the code of my MainActivity:

package com.example.enriq.interfaces_de_usuario_enrique_espinosa;

import android.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.os.Bundle;
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
            if (id == R.id.Versionw) {
                return true;
            }
            else if (id == R.id.Compartir) {

            } else if (id == R.id.Configurar) {

            }

            return super.onOptionsItemSelected(item);
        }

        @SuppressWarnings("StatementWithEmptyBody")
        @Override
        public boolean onNavigationItemSelected(MenuItem item) {
            // Handle navigation view item clicks here.
            int id = item.getItemId();

            if (id == R.id.hometap) {
                // Handle the camera action
            } else if (id == R.id.facetap) {
                facebookfragment facebook = new facebookfragment();
                android.support.v4.app.FragmentManager manager = getSupportFragmentManager();
                manager.beginTransaction().replace(R.id.fragmentf, facebook).commit();

            } else if (id == R.id.instatap) {

            } else if (id == R.id.twittertap) {

            } else if (id == R.id.googletap) {

            }

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

This is the activity_main:

<?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>

And the code of my fragment is the following:

package com.example.enriq.interfaces_de_usuario_enrique_espinosa;


import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;


/**
 * A simple {@link Fragment} subclass.
 */
public class facebookfragment extends Fragment {


    public facebookfragment() {
        // Required empty public constructor
    }


    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        return inflater.inflate(R.layout.fragment_facebookfragment, container, false);
    }

}

and finally the layout of my fragment:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/fragmentf"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context="com.example.enriq.interfaces_de_usuario_enrique_espinosa.facebookfragment">


</RelativeLayout>

I do not know why I made that mistake.

    
asked by Kike hatake 26.12.2017 в 19:37
source

3 answers

0

The error is that the id that you use in the replace () must be from the container in which you want to place the fragment.

Add a id in the content_main.xml

file
android:id="@+id/contenedor_principal"

And in your MainActivity it would look like this:

facebookfragment facebook = new facebookfragment();
android.support.v4.app.FragmentManager manager =   getSupportFragmentManager();
manager.beginTransaction().replace(R.id.contenedor_principal, facebook).commit();

With this it should work.

    
answered by 27.12.2017 / 00:31
source
0

The problem is that you have to define the content_menu.xml with an ID eg: android: id="+ @ id / content_main", since Navigation Drawer has its own and does not need activity_main.xml to reference.

    
answered by 12.01.2018 в 14:37
0

The app_bar_main layout is included in the activity.xml file

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

the safest thing is that app_bar_main.xml includes content_main

<include layout="@layout/content_main"/>

If you have included the above, check in the content_main.xml file to see if you have the following

<FrameLayout
    android:id="@+id/content"
    android:layout_width="match_parent"
    android:layout_height="0dp"
    android:layout_weight="1">

</FrameLayout>

If you do not add it, once this is done we have the security that the container exists and it will be replaced by the fragments you want. since at the moment it is assigned in the MainActivity with

setContentView(R.layout.activity_main);

We can call and replace it. I create a method to do this.

 protected boolean openFragment(Fragment fragment){
    getSupportFragmentManager()
            .beginTransaction()
            .replace(R.id.content, fragment)
            .commit();
    return true;
}

and I call it where you need it in your case in onNavigationItemSelected it stays like this

 public boolean onNavigationItemSelected(MenuItem item) {
        // Handle navigation view item clicks here.
        int id = item.getItemId();

        if (id == R.id.hometap)
        else if (id == R.id.facetap) openFragment(new facebookfragment());
        else if (id == R.id.instatap) 
        else if (id == R.id.twittertap) 
        else if (id == R.id.googletap) 

        DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
        drawer.closeDrawer(GravityCompat.START);
        return true;
    }
    
answered by 26.09.2018 в 05:20