【发布时间】:2014-02-09 16:23:02
【问题描述】:
我正在实施 Google Map V2 api,并获取当前位置。我使用的代码与 Google 示例项目完全相同,
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_add_map);
}
@Override
protected void onResume() {
super.onResume();
setUpMapIfNeeded();
setUpLocationClientIfNeeded();
mLocationClient.connect();
}
@Override
public void onPause() {
super.onPause();
if (mLocationClient != null) {
mLocationClient.disconnect();
}
}
private void setUpMapIfNeeded() {
// Do a null check to confirm that we have not already instantiated the map.
if (mMap == null) {
// Try to obtain the map from the SupportMapFragment.
mMap = ((SupportMapFragment) getSupportFragmentManager().findFragmentById(R.id.map_AddFragment))
.getMap();
// Check if we were successful in obtaining the map.
if (mMap != null) {
mMap.setMyLocationEnabled(true);
mMap.setOnMyLocationButtonClickListener(this);
}
}
}
我的布局:
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
xmlns:map="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<fragment
android:id="@+id/map_AddFragment"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:name="com.google.android.gms.maps.MapFragment"
tools:ignore="MissingPrefix" />
</RelativeLayout>
当我从示例项目运行此代码时,它可以工作,但是当我将此代码复制到我的项目时,它在mMap = ((SupportMapFragment) getSupportFragmentManager().findFragmentById(R.id.map_AddHTag))
.getMap(); 行上给了我一个Null pointer exception
我真的不知道发生了什么,因为它运行良好,并且在示例项目中仍然运行良好。
谢谢。
【问题讨论】:
-
如何将
SupportMapFragment添加到活动中? -
尝试项目 -> 清理。如果您在 XML 文件中移动了一些东西,这通常可以解决问题。
-
@CommonsWare 谢谢,我已经更新了我的帖子。我还清理了项目,关闭了 eclipse 并重新打开它,但仍然无法正常工作
标签: android google-maps