java - Android studio Google Map API 与 fragment

标签 java android google-maps android-fragments

我想显示一个带有 fragment 的简单谷歌地图。 我尝试在 mainActivity 中调用一个 fragment :

MapFragment.newInstance();

这是 fragment 类:

public class MapFragment extends Fragment {

    private MapView mapView;

    public static MapFragment newInstance() {
        return new MapFragment();
    }

    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup   container, @Nullable Bundle savedInstanceState) {
        return inflater.inflate(R.layout.fragment_map, container, false);
    }
    @Override
    public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        mapView = (MapView) view.findViewById(R.id.mapview);
    }
}

和布局 fragment :

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

但是当在我的设备中测试它时,应用程序会重新启动。

我哪里错了?

一些错误:

01-15 05:00:54.770 23995-24019/? E/GMPM: getGoogleAppId failed with status: 10 01-15 05:00:54.770 23995-24019/? E/GMPM: Uploading is not possible. App measurement disabled 01-15 05:00:55.010 23995-23995/? E/AndroidRuntime: FATAL EXCEPTION: main . . . at android.view.Choreographer.doCallbacks(Choreographer.java:606) at android.view.Choreogr 01-15 05:17:00.890 3281-3460/? E/InputDispatcher: channel '682f930 com.github.florent37.materialviewpager.sample/com.github.florent37.materialviewpager.sample.MainActivity (server)' ~ Channel is unrecoverably broken and will be disposed! 01-15 05:17:00.890 3281-3731/? E/HsmCoreServiceImpl: onTransact in code is: 102 01-15 05:17:06.890 2778-2778/? E/Thermal-daemon: [pa_0] temp_new :32 temp_old :33 01-15 05:17:06.890 2778-2778/? E/Thermal-daemon: [charger_ic] temp_new :32 temp_old :33

最佳答案

com.google.android.gms.maps.MapFragment 不是 MapView

更改 fragment 布局 Xml

<?xml version="1.0" encoding="utf-8"?>
<com.google.android.gms.maps.MapView 
    android:id="@+id/mapview"
    android:layout_width="match_parent" 
    android:layout_height="match_parent" />

并在您的 fragment 上找到 GoogleMap 实例

MapView mapView;
GoogleMap map;

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

    // Gets the MapView from the XML layout and creates it
    mapView = (MapView) v.findViewById(R.id.mapview);
    mapView.onCreate(savedInstanceState);

    // Gets to GoogleMap from the MapView and does initialization stuff
    map = mapView.getMap();
}

引用:https://gist.github.com/joshdholtz/4522551

关于java - Android studio Google Map API 与 fragment ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34803095/

相关文章:

java - mybatis-插入事务

java - 重复数据删除在Reducer中的工作不符合预期

java - RxJava/Retrofit2/Java - NetworkBoundResource 未按预期工作

java - 每次滚动/平移/拖动时,相机都会移回到原始位置

jquery - 构建像 Airbnb 一样的地理搜索自动完成功能

java - 为什么不在 C++ 中将所有函数都设为虚拟函数?

java - 使用 Itext 在 PDF 页面上现有内容的底部添加表格

javascript - 电话间隙 : is it possible to detect swipeLeft and SwipeRight touch from Javascript?

android - 使用自定义适配器将数据从 firebase 数据库检索到多个 TextView 中,不显示任何错误/不显示任何内容?

google-maps - Google 地理编码 API 限制