Reputation: 35
I'm not sure what I have changed here.
My code to move to the WatchingFragment was working fine.
I have since added the code to move to the news and buynsell fragments. Now when I click that particular items from the Nav menu. The app crashes.
Any help would be appreciated.
Calling Fragments
@SuppressWarnings("StatementWithEmptyBody")
@Override
public boolean onNavigationItemSelected(MenuItem item) {
// Handle navigation view item clicks here.
int id = item.getItemId();
if (id == R.id.nav_home) {
// Handle the camera action
} else if (id == R.id.nav_watching) {
WatchingFragment watchingFragment = new WatchingFragment();
FragmentManager manager = getSupportFragmentManager();
manager.beginTransaction().replace(R.id.relativeLayout_for_fragment, watchingFragment, watchingFragment.getTag()).commit();
} else if (id == R.id.nav_news) {
NewsFragment newsFragment = new NewsFragment();
FragmentManager manager = getSupportFragmentManager();
manager.beginTransaction().replace(R.id.relativeLayout_for_fragment, newsFragment, newsFragment.getTag()).commit();
} else if (id == R.id.nav_buynsell) {
BuynSellFragment buynSellFragment = new BuynSellFragment();
FragmentManager manager = getSupportFragmentManager();
manager.beginTransaction().replace(R.id.relativeLayout_for_fragment, buynSellFragment, buynSellFragment.getTag()).commit();
} else if (id == R.id.nav_feedback) {
} //else if (id == R.id.nav_send) {
//}
DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
drawer.closeDrawer(GravityCompat.START);
return true;
}
}
java.lang.IllegalArgumentException: No view found for id 0x7f0d0097 (uk.co.cyclesavy.cyclesavy:id/relativeLayout_for_fragment) for fragment NewsFragment{ba864be #0 id=0x7f0d0097}
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:6077)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:865)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:755)
Application terminated.
XML of fragment I am trying to navigate to
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="uk.co.cyclesavy.cyclesavy.WatchingFragment">
<RadioButton
android:text="RadioButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:layout_marginTop="216dp"
android:id="@+id/radioButton" />
<CheckBox
android:text="CheckBox"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBottom="@+id/radioButton"
android:layout_alignLeft="@+id/radioButton"
android:layout_alignStart="@+id/radioButton"
android:layout_marginBottom="71dp"
android:id="@+id/checkBox" />
<Button
android:text="Button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/radioButton"
android:layout_alignRight="@+id/checkBox"
android:layout_alignEnd="@+id/checkBox"
android:layout_marginTop="30dp"
android:id="@+id/button2" />
</FrameLayout>
Upvotes: 0
Views: 65
Reputation: 93542
You need to have a view in your layout that's being replaced by your fragment when you call replace. There is no view with that id in your layout.
Upvotes: 1