Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/google-maps/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Android 使用GoogleMaps';第二次破片_Android_Google Maps_Android Fragments_Fragment - Fatal编程技术网

Android 使用GoogleMaps';第二次破片

Android 使用GoogleMaps';第二次破片,android,google-maps,android-fragments,fragment,Android,Google Maps,Android Fragments,Fragment,我的布局如下。 当我第一次运行时,一切正常,我看到地图: <?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"

我的布局如下。 当我第一次运行时,一切正常,我看到地图:

<?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();
标记“fragment”上的错误消息点:

有什么想法吗?
谢谢

嘿,因为你的map1片段已经是视图了。 然后在视图上添加另一个map2片段视图。 所以它会产生一个错误

为此,您只需在替换片段之前放置此代码

 Fragment fragment = (getFragmentManager().findFragmentById(R.id.map));  
    FragmentTransaction ft = getActivity().getSupportFragmentManager().beginTransaction();
    ft.remove(fragment);
    ft.commit();

在第二次使用之前,您必须删除映射片段。不幸的是,getActivity()在我的intelliJ 14.1中被标记为红色(无法解析方法)。我在清单中的目标SDK是15。我尝试了不同的方法来代替getActivity():MyActivity、MyActivity.class、MyActivity.this、getApplicationContext()。还是编译错误我找到了这段代码。我第二次用它打开地图。我还是得到了错误:Fragment newFragment=(getFragmentManager().findffragmentbyid(R.id.map1));FragmentTransaction=getFragmentManager().beginTransaction();事务.replace(R.id.map1,newFragment);transaction.addToBackStack(空);commit();我找到了这根线。我也有同样的问题。但答案中遗漏了一些东西,我有编译错误:
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)
 Fragment fragment = (getFragmentManager().findFragmentById(R.id.map));  
    FragmentTransaction ft = getActivity().getSupportFragmentManager().beginTransaction();
    ft.remove(fragment);
    ft.commit();