R.K. GUPTA R.K. GUPTA - 5 months ago 80
Android Question

App crashes when a opening a new item ( fragment) from the navigationDrawer (see code)?

I am trying to make app with navigationView . The navigation view has many options which when clicked opens a new fragment.I have set the home fragment as default. But the app is crashing whenever i click any option from the navigationView. I am new to android development, please help!

my log

ava.lang.IllegalArgumentException: No view found for id 0x7f0c0086 (rishabh.example.com.navigationdrawer:id/home_id) for fragment ProfileFragment{10c46e27 #1 id=0x7f0c0086}
at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1059)
at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1252)
at android.support.v4.app.BackStackRecord.run(BackStackRecord.java:742)
at android.support.v4.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:1617)
at android.support.v4.app.FragmentManagerImpl$1.run(FragmentManager.java:517)
at android.os.Handler.handleCallback(Handler.java:739)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:135)
at android.app.ActivityThread.main(ActivityThread.java:5343)
at java.lang.reflect.Method.invoke(Native Method)
at java.lang.reflect.Method.invoke(Method.java:372)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:905)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:700)


MY MainActivity.java

import android.os.Bundle;
import android.os.PersistableBundle;
import android.support.design.widget.NavigationView;
import android.support.v4.widget.DrawerLayout;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.util.Log;
import android.view.MenuItem;
public class MainActivity extends AppCompatActivity {

DrawerLayout drawerLayout;
android.support.v7.app.ActionBarDrawerToggle actionBarDrawerToggle;
Toolbar toolbar;
android.support.v4.app.FragmentTransaction fragmentTransaction;
NavigationView navigationView;

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


actionBarDrawerToggle=new android.support.v7.app.ActionBarDrawerToggle(this,drawerLayout,toolbar,R.string.drawer_open,R.string.drawer_close);

drawerLayout.addDrawerListener(actionBarDrawerToggle);


fragmentTransaction=getSupportFragmentManager().beginTransaction();
fragmentTransaction.add(R.id.container_fragment,new HomeFragment());
fragmentTransaction.commit();
getSupportActionBar().setTitle("Home");

navigationView=(NavigationView)findViewById(R.id.navView);
navigationView.setNavigationItemSelectedListener(new NavigationView.OnNavigationItemSelectedListener() {
@Override
public boolean onNavigationItemSelected(MenuItem item) {


switch(item.getItemId()){
case R.id.home_id:

fragmentTransaction=getSupportFragmentManager().beginTransaction();
fragmentTransaction.replace(R.id.home_id,new HomeFragment());
fragmentTransaction.commit();
getSupportActionBar().setTitle("Home");
item.setChecked(true);
drawerLayout.closeDrawers();
break;
case R.id.profile_id:
Log.i("tag","inside profile");
fragmentTransaction=getSupportFragmentManager().beginTransaction();
fragmentTransaction.replace(R.id.home_id,new ProfileFragment());
fragmentTransaction.commit();
Log.i("tag","inside profile commit");
getSupportActionBar().setTitle("Profile");
item.setChecked(true);
drawerLayout.closeDrawers();
break;
case R.id.tracker_id:
fragmentTransaction=getSupportFragmentManager().beginTransaction();
fragmentTransaction.replace(R.id.home_id,new TracerFragment());
fragmentTransaction.commit();
getSupportActionBar().setTitle("Tracker");
item.setChecked(true);
drawerLayout.closeDrawers();
break;
case R.id.siren_id:
fragmentTransaction=getSupportFragmentManager().beginTransaction();
fragmentTransaction.replace(R.id.home_id,new SirenFragment());
fragmentTransaction.commit();
getSupportActionBar().setTitle("Siren");
item.setChecked(true);
drawerLayout.closeDrawers();
break;
case R.id.settings_id:
fragmentTransaction=getSupportFragmentManager().beginTransaction();
fragmentTransaction.replace(R.id.home_id,new SettingsFragment());
fragmentTransaction.commit();
getSupportActionBar().setTitle("Settings");
item.setChecked(true);
drawerLayout.closeDrawers();
break;
case R.id.support_id:
fragmentTransaction=getSupportFragmentManager().beginTransaction();
fragmentTransaction.replace(R.id.home_id,new SupportFragment());
fragmentTransaction.commit();
getSupportActionBar().setTitle("Support");
item.setChecked(true);
drawerLayout.closeDrawers();
break;
case R.id.about_id:
fragmentTransaction=getSupportFragmentManager().beginTransaction();
fragmentTransaction.replace(R.id.home_id,new AboutUsFragment());
fragmentTransaction.commit();
getSupportActionBar().setTitle("About Us");
item.setChecked(true);
drawerLayout.closeDrawers();
break;




}
return true;
}
});

}


activity_layout

<?xml version="1.0" encoding="utf-8"?>
<android.support.v4.widget.DrawerLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/drawerLayout"
tools:context="rishabh.example.com.navigationdrawer.MainActivity">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<include android:layout_height="wrap_content"
android:layout_width="match_parent"
layout="@layout/toolbar_layout"
/>
<FrameLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/container_fragment">

</FrameLayout>

</LinearLayout>
<android.support.design.widget.NavigationView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/navView"
android:layout_gravity="start"
app:menu="@menu/drawer_menu"
app:headerLayout="@layout/navigation_drawer_head"
>


</android.support.design.widget.NavigationView>


ProfileFragment

public class ProfileFragment extends Fragment {

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

}

sJy sJy
Answer

There is no home_id in your layout. It is container_fragment to which you should be loading the fragments.

fragmentTransaction.replace(R.id.home_id,new HomeFragment());
                fragmentTransaction.commit();

Replace it with

fragmentTransaction.replace(R.id.container_fragment,new HomeFragment());

Do the same change for all replace() calls(container_fragment in place of home_id).

Comments