【问题标题】:Android Google Map Fragment in another FragmentAndroid Google Map Fragment 在另一个 Fragment
【发布时间】:2016-07-19 00:12:47
【问题描述】:

我似乎无法在片段中添加谷歌地图。这是我的代码

activity_main.xml

<RelativeLayout 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="com.example.test.MainActivity">
    <fragment
        android:id="@+id/fragment1"
        android:name="com.example.test.MyMapFragment"
        android:layout_width="match_parent"
        android:layout_height="wrap_content" />
</RelativeLayout>

fragment_mymap.xml

<RelativeLayout 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=".MainActivity" >
    <fragment
        android:id="@+id/google_map"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        class="com.google.android.gms.maps.SupportMapFragment" />
</RelativeLayout>

MyMapFragment.java

public class MyMapFragment extends Fragment implements OnMapReadyCallback
{
    private GoogleMap mMap;

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View v = inflater.inflate(R.layout.fragment_mymap, container, false);

        SupportMapFragment mapFragment = (SupportMapFragment) getFragmentManager().findFragmentById(R.id.google_map);
        mapFragment.getMapAsync(this);

        // Inflate the layout for this fragment
        return v;
    }

    @Override
    public void onMapReady(GoogleMap googleMap) {
        mMap = googleMap;
    }
}

然而,我收到android.view.InflateException: Binary XML file line #8: Error inflating class fragment 的错误。

我尝试在片段中使用MapView,就像在this postthis one 中一样。但问题是,mapView.getMap() 现在已被弃用。虽然程序没有报错,但地图显示了,但它是空白的(只在左下角显示 Google 徽标)。

欢迎提出任何建议。

【问题讨论】:

  • 将此行 class="com.google.android.gms.maps.SupportMapFragment" 更改为 app:class="com.google.android.gms.maps.SupportMapFragment" 并添加 xmlns:app= “schemas.android.com/apk/res-auto”。希望对您有所帮助
  • @Shubham 它仍然不起作用。编辑器在app:class 下划线,错误消息为Unexpected namespace prefix "app" found for tag fragment。还有其他建议吗?
  • 是在片段或其父级中使用命名空间 xmlns:app="schemas.android.com/apk/res-auto"
  • @Shubham 我都试过了。如果我将它添加到其父级,它将在app:class 下加下划线。如果我将它添加到片段中,它仍然会下划线并抛出错误android.view.InflateException: Binary XML file line #8: Error inflating class fragment
  • @d.datul1990 我想我没有碰巧将谷歌地图片段嵌套到另一个片段中..我采用了另一种方法..

标签: java android google-maps


【解决方案1】:

是的,'mapView.getMap()' 现在已弃用。使用'getMapAsync'地图可以使用时触发的对象。

对于错误“Error inflating class fragment.”,请检查项目构建目标和最低目标 sdk 版本。另外,不要忘记包含您的地图权限。

我发现这个相关的SO ticket 与您的问题有关。其他 OP 提供了一些解决方案,它可能会解决您的问题。

您还可以使用 Google Maps Android API 官方文档,该文档是有关如何将 Google 地图创建到 Android 应用程序的快速入门: https://developers.google.com/maps/documentation/android-api/start

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-09-06
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多