【发布时间】: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 post 或this 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