我的 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/