android - 在 MapFragment 中膨胀类 fragment 时出错

标签 android google-maps

我的 MapFragment 有 xml 文件

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

和 fragment 使用它:

public class CallistoMapFragment extends MapFragment {
private GoogleMap mMap;

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
        Bundle savedInstanceState) {
    View v = inflater.inflate(R.layout.map_fragment, null);
    mMap = ((MapFragment) getFragmentManager().findFragmentById(R.id.map))
            .getMap();
    setUpMap();
    return v;
}

private void setUpMap() {
    mMap.addMarker(new MarkerOptions().position(new LatLng(0, 0)).title(
            "Marker"));
}

} 尝试编译时出现错误

11-20 15:31:15.343: E/AndroidRuntime(10705): android.view.InflateException: Binary XML file line #6: Error inflating class fragment

android list 中的 API key 是正确的。怎么了?

最佳答案

使用:

XML:

<fragment
        android:id="@+id/map"
        android:name="com.google.android.gms.maps.SupportMapFragment"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:clickable="true"
         />

GoogleMap mMap;
SupportMapFragment mapFrag;

Now set it:

if (mMap == null) {
            mapFrag= (SupportMapFragment)getSupportFragmentManager().findFragmentById(R.id.map);
            mMap =  mapFrag.getMap();

            // Check if we were successful in obtaining the map.
            if (mMap != null) {
                // The Map is verified. It is now safe to manipulate the map.

            }
        }

还在 list 文件中添加此元标记。

 <meta-data
            android:name="com.google.android.gms.version"
            android:value="@integer/google_play_services_version" />

<meta-data
            android:name="com.google.android.maps.v2.API_KEY"
            android:value="myApiKey"/> 

关于android - 在 MapFragment 中膨胀类 fragment 时出错,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20096177/

相关文章:

ios - 使用未解析的标识符 GMSPlacePickerConfig 和 GooglePlacePicker 时出错? (谷歌地方信息 2.0)

java - 致命异常 : main java. lang.RuntimeException:无法启动 Activity

android - Google Material Design 图标 - 从相应的可绘制文件夹中使用哪个文件(18dp、24、36 和 48)

android - ActionBar 显示图标,但它显示菜单命令

javascript - 如何从 Google 版本 map 中拖动的标记获取格式化地址

javascript - 刷新谷歌地图javascript ajax

android - 在 Webview 中加载一个 Url 并得到它的响应

multithreading - 在等待线程完成时显示 progressDialog 2 秒

javascript - Google map - 加载时可以出现弹出窗口吗?

javascript - 谷歌地图 : Display custom marker multiple times on wide map