Google Maps v2在单击两次时出现碎片崩溃

时间:2013-06-15 20:06:03

标签: android google-maps android-fragments google-maps-android-api-2

我正在尝试使用标记在我的应用中设置谷歌地图。当我第一次点击它时它起作用,但是当我点击另一个片段然后返回谷歌地图时它会崩溃。不确定是什么问题?任何帮助都会很棒,只有初学者。

Heres代码:

XML

<?xml version="1.0" encoding="utf-8"?>
<fragment xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/map"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:name="com.google.android.gms.maps.SupportMapFragment"/>

Java google maps片段编辑工作代码

public class Fragment_8 extends Fragment{

static final LatLng BottleCapp = new LatLng(51.371986, 0.065593);
  private GoogleMap map;
  private static View view;

  @Override
  public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
      if (view != null) {
          ViewGroup parent = (ViewGroup) view.getParent();
          if (parent != null)
              parent.removeView(view);
      }
      try {
          view = inflater.inflate(R.layout.fragment_8, container, false);
      } catch (InflateException e) {
          /* map is already there, just return view as it is */
      }
    map = ((SupportMapFragment)getFragmentManager().findFragmentById(R.id.map)).getMap();

        Marker bottleapp = map.addMarker(new MarkerOptions().position(BottleCapp)
            .title("BottleCapp"));


        // Move the camera instantly to Bottlecapp with a zoom of 15.
        map.moveCamera(CameraUpdateFactory.newLatLngZoom(BottleCapp, 15));

        // Zoom in, animating the camera.
        map.animateCamera(CameraUpdateFactory.zoomTo(10), 2000, null);
        getActivity().getSupportFragmentManager().popBackStack();
        return view;

    }
  }

错误

 06-15 21:53:46.673: E/AndroidRuntime(8677): FATAL EXCEPTION: main
 06-15 21:53:46.673: E/AndroidRuntime(8677): android.view.InflateException: Binary XML file line #2: Error inflating class fragment
 06-15 21:53:46.673: E/AndroidRuntime(8677):    at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:704)
 06-15 21:53:46.673: E/AndroidRuntime(8677):    at android.view.LayoutInflater.inflate(LayoutInflater.java:466)
 06-15 21:53:46.673: E/AndroidRuntime(8677):    at android.view.LayoutInflater.inflate(LayoutInflater.java:396)
 06-15 21:53:46.673: E/AndroidRuntime(8677):    at com.bottlecapp.bottlecapp.Fragment_8.onCreateView(Fragment_8.java:29)
 06-15 21:53:46.673: E/AndroidRuntime(8677):    at android.support.v4.app.Fragment.performCreateView(Fragment.java:1460)
 06-15 21:53:46.673: E/AndroidRuntime(8677):    at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:911)
 06-15 21:53:46.673: E/AndroidRuntime(8677):    at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1088)
 06-15 21:53:46.673: E/AndroidRuntime(8677):    at android.support.v4.app.BackStackRecord.run(BackStackRecord.java:682)
 06-15 21:53:46.673: E/AndroidRuntime(8677):    at android.support.v4.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:1444)
 06-15 21:53:46.673: E/AndroidRuntime(8677):    at android.support.v4.app.FragmentManagerImpl$1.run(FragmentManager.java:429)
 06-15 21:53:46.673: E/AndroidRuntime(8677):    at android.os.Handler.handleCallback(Handler.java:725)
 06-15 21:53:46.673: E/AndroidRuntime(8677):    at android.os.Handler.dispatchMessage(Handler.java:92)
 06-15 21:53:46.673: E/AndroidRuntime(8677):    at android.os.Looper.loop(Looper.java:137)
 06-15 21:53:46.673: E/AndroidRuntime(8677):    at android.app.ActivityThread.main(ActivityThread.java:5041)
 06-15 21:53:46.673: E/AndroidRuntime(8677):    at java.lang.reflect.Method.invokeNative(Native Method)
 06-15 21:53:46.673: E/AndroidRuntime(8677):    at java.lang.reflect.Method.invoke(Method.java:511)
 06-15 21:53:46.673: E/AndroidRuntime(8677):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
 06-15 21:53:46.673: E/AndroidRuntime(8677):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)
 06-15 21:53:46.673: E/AndroidRuntime(8677):    at dalvik.system.NativeStart.main(Native Method)
 06-15 21:53:46.673: E/AndroidRuntime(8677): Caused by: java.lang.IllegalArgumentException: Binary XML file line #2: Duplicate id 0x7f05003c, tag null, or parent id 0x0 with another fragment for com.google.android.gms.maps.SupportMapFragment
 06-15 21:53:46.673: E/AndroidRuntime(8677):    at android.support.v4.app.FragmentActivity.onCreateView(FragmentActivity.java:285)
 06-15 21:53:46.673: E/AndroidRuntime(8677):    at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:676)
 06-15 21:53:46.673: E/AndroidRuntime(8677):    ... 18 more

2 个答案:

答案 0 :(得分:6)

根据您在此处提供的代码,您尝试使用的对象中存在一些错误,并且您正在将它们混合起来。我在谈论你在xml布局文件中使用的MapFragment对象:

class="com.google.android.gms.maps.MapFragment"

如果您的xml中使用的是MapFragment,那么我不明白为什么您要在活动代码中获取SupportMapFragment对象:

 map = ((SupportMapFragment)getFragmentManager().findFragmentById(R.id.map)).getMap();

很高兴看到您针对您的应用程序定位的API级别,但您需要决定要支持API V11及更低版本的设备,或者您只想支持API V12及更高版本。如果你想支持API V11并且更低,请查看我写的关于在你的应用程序中集成G​​oogle API V2地图的博客文章:

Google Maps API V2

我建议您只支持更高版本,然后将活动代码更改为:

map = ((MapFragment)getFragmentManager().findFragmentById(R.id.map)).getMap();

并相应地更改清单文件。

答案 1 :(得分:1)

com.google.android.gms.maps.MapFragment基于支持包中不包含的Fragment类。您需要更改xml才能使用com.google.android.gms.maps.SupportMapFragment

或者不在活动中使用SupportMapFragment。如果您正在寻找向后(pre Honeycomb)Fragment支持,我建议您将xml布局中的类更改为SupportMapFragment。如果没有,请考虑在您的活动中将Fragment投射到MapFragment

现在,对于第二次崩溃,地图似乎不可用。使用V2地图库,您需要在设备上运行最新的Google Play服务。通常Google Play应用会自动为您安装。如果您正在使用模拟器,我担心您现在运气不好,请参阅使用Play服务与模拟器的答案: How to download Google Play Services in an Android emulator?

为避免崩溃,请查看此code sample。另外,有关地图可用性的简要说明,请查看second paragraph in the Class Overview