-1

my layout is below. When I run it first time all is okay and I see the map:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
          xmlns:tools="http://schemas.android.com/tools" android:layout_width="fill_parent"
          android:layout_height="match_parent"
          android:background="@color/backNormal"
          android:orientation="vertical"
    >
   <Button
            android:id="@+id/buttonDriverAgreeYes"
            android:gravity="center"
            android:text='Agree'
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            />
<fragment
        android:id="@+id/map1"
        android:name="com.google.android.gms.maps.MapFragment"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        />
</LinearLayout>

    setContentView(R.layout.my_page1)
    map1 = ((MapFragment) getFragmentManager().findFragmentById(R.id.map1))
                    .getMap();

But I get runtime error when I try to show this layout second time, or even when I try to show another similar layout with map named map2:

        setContentView(R.layout.my_page2)
    map2 = ((MapFragment) getFragmentManager().findFragmentById(R.id.map2))
                    .getMap();

Error message points on tag "fragment":

android.view.InflateException: Binary XML file line #60: Error inflating class fragment
            at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:763)
            at android.view.LayoutInflater.rInflate(LayoutInflater.java:806)
            at android.view.LayoutInflater.inflate(LayoutInflater.java:504)
            at android.view.LayoutInflater.inflate(LayoutInflater.java:414)
            at android.view.LayoutInflater.inflate(LayoutInflater.java:365)
            at com.android.internal.policy.impl.PhoneWindow.setContentView(PhoneWindow.java:377)
            at android.app.Activity.setContentView(Activity.java:2144)

Any ideas please? Thanks!

Niaz
  • 545
  • 2
  • 12
  • 21

1 Answers1

0

hey Because of Your map1 Fragment is already the View. and You add another map2 Fragment view on the View. so it generates an Error

for this You just put this code before You replace fragment

 Fragment fragment = (getFragmentManager().findFragmentById(R.id.map));  
    FragmentTransaction ft = getActivity().getSupportFragmentManager().beginTransaction();
    ft.remove(fragment);
    ft.commit();
Dilavar Malek
  • 1,157
  • 11
  • 18
  • Unfortunately getActivity() is marked with red in my intelliJ 14.1 (cannot resolve method). My target SDK in manifest is 15. – Niaz Mar 30 '15 at 06:18
  • I tries different things instead of getActivity(): MyActivity, MyActivity.class, MyActivity.this, getApplicationContext(). Still compilation error – Niaz Mar 30 '15 at 06:29
  • I found this code. I use it opening map second time. I got the error anyway:Fragment newFragment = (getFragmentManager().findFragmentById(R.id.map1)); FragmentTransaction transaction = getFragmentManager().beginTransaction(); transaction.replace(R.id.map1, newFragment); transaction.addToBackStack(null); transaction.commit(); – Niaz Mar 30 '15 at 06:39
  • I found this thread. I have exact the same problem. But something is omitted in the answer, and I have compilation errors:http://stackoverflow.com/questions/14565460/error-opening-supportmapfragment-for-second-time – Niaz Mar 30 '15 at 07:13