Reputation: 521
I'm doing an app that uses Google Maps APIs (with two separate maps), and I want to use a navigation drawer to switch between the two maps (fragments).
I created the main activity with the pre-compiled Navigation Drawer Activity in android studio, and here is the pieces of code:
activity_main_activity2.xml
<android.support.v4.widget.DrawerLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/drawer_layout"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="it.radonmap.radonmap.MainActivity2">
<FrameLayout android:id="@+id/container" android:layout_width="match_parent"
android:layout_height="match_parent" />
<fragment
android:id="@+id/map"
android:layout_width="match_parent"
android:layout_height="match_parent"
class="com.google.android.gms.maps.SupportMapFragment" />
<fragment android:id="@+id/navigation_drawer"
android:layout_width="@dimen/navigation_drawer_width" android:layout_height="match_parent"
android:layout_gravity="start" android:name="it.radonmap.radonmap.NavigationDrawerFragment"
tools:layout="@layout/fragment_navigation_drawer" />
</android.support.v4.widget.DrawerLayout>
Here is the code which calls the .replace() method:
private void displayView(int position) {
// update the main content by replacing fragments
Fragment fragment = null;
switch (position) {
case 0:
//do something
break;
case 1:
fragment = new FragPunti();
break;
default:
break;
}
if (fragment != null) {
FragmentManager fragmentManager;
fragmentManager = getFragmentManager();
fragmentManager.beginTransaction().replace(R.id.containerPunti,fragment).commit();
} else {
// error in creating fragment
Log.e("MainActivity", "Error in creating fragment");
}
}
FragPunti.java
public class FragPunti extends android.support.v4.app.Fragment {
public FragPunti(){}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.frag_punti, null);
return rootView;
}
}
frag_punti.xml
<FrameLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_height="match_parent"
android:layout_width="match_parent"
android:id="@+id/containerPunti">
<FrameLayout
android:id="@+id/framePunti"
android:layout_width="0px"
android:layout_height="match_parent"
>
<TextView
android:id="@+id/txtLabel"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:textSize="16dp"
android:text="Punti"/>
</FrameLayout>
</FrameLayout>
And here is the logcat trace:
04-30 19:17:45.823 20909-20909/it.radonmap.radonmap E/AndroidRuntime﹕ FATAL EXCEPTION: main
Process: it.radonmap.radonmap, PID: 20909
java.lang.IllegalArgumentException: No view found for id 0x7f090058 (it.radonmap.radonmap:id/containerPunti) for fragment FragPunti{36b6aaa5 #2 id=0x7f090058}
at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:945)
at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1136)
at android.support.v4.app.BackStackRecord.run(BackStackRecord.java:739)
at android.support.v4.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:1499)
at android.support.v4.app.FragmentManagerImpl$1.run(FragmentManager.java:456)
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:5274)
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:909)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:704)
I had already look for a solution but with no results. Hope someone can help me.
Upvotes: 0
Views: 2288
Reputation: 802
You should pass id of container for fragment, and that's a view in which you want to show fragment. It is in activity, not in fragment you want to show. So change replace(R.id.containerPunti, fragment)
to replace(R.id.container, fragment)
.
Upvotes: 2