Android工作室Google Map API带有片段 [英] Android studio Google Map API with fragments

查看:183
本文介绍了Android工作室Google Map API带有片段的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想用片段显示简单的Google地图。
我尝试在mainActivity中调用片段:

  MapFragment.newInstance(); 

这里是片段类:

  public class MapFragment extends Fragment {

MapView MapView;

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

@Override
public View onCreateView(LayoutInflater inflater,@Nullable ViewGroup容器,@Nullable Bundle savedInstanceState){
返回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);


$ / code $ / pre

和布局片段:

 <?xml version =1.0encoding =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
/>

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



whtas my wrong?



有些错误:


01-15 05:00:54.770 23995-24019 /? E / GMPM:getGoogleAppId失败,
状态:10 01-15 05:00:54.770 23995-24019 /? E / GMPM:上传不是
可能。应用测量已停用01-15 05:00:55.010 23995-23995 /?
E / AndroidRuntime:致命例外:主。 。 。
android.view.Choreographer.doCallbacks(Choreographer.java:606)
at android.view.Choreogr 01-15 05:17:00.890 3281-3460 /?
E / InputDispatcher:频道'682f930
com.github.florent37.materialviewpager.sample / com.github.florent37.materialviewpager.sample.MainActivity
(服务器)'〜频道无法恢复,将被处置!
01-15 05:17:00.890 3281-3731 /? E / HsmCoreServiceImpl:
中的onTransact代码是: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



更改您的分段布局Xml

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

并在您的片段中找到GoogleMap实例

  MapView mapView; 
GoogleMap地图;

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

//从XML布局中获取MapView并创建它
mapView =(MapView)v.findViewById(R.id.mapview);
mapView.onCreate(savedInstanceState);

//从MapView获取到GoogleMap并执行初始化操作
map = mapView.getMap();
}

ref: https://gist.github.com/joshdholtz/4522551


I want to show a simple google map with fragment. I try call a fragment in mainActivity:

MapFragment.newInstance();

And here is fragment class:

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);
    }
}

and layout 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"
    />

But when test it in my device, the app be restart.

whtas my wrong?

SOME errors:

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 is not MapView

Change your fragment layout 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" />

and find GoogleMap instance on your fragment

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();
}

ref : https://gist.github.com/joshdholtz/4522551

这篇关于Android工作室Google Map API带有片段的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆