在遇到一些解析问题inflating the XML之后,我决定采用一种程序化的解决方法。地图已正确添加到场景中,但我将其作为类成员缓存以供以后使用时出现问题。这是爆炸点的代码片段。
的活动:
public class MoogliActivity extends FragmentActivity {
public void onCreate(final Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
this.setContentView(R.layout.main);
final SupportMapFragment supportMap = SupportMapFragment.newInstance();
final FragmentTransaction fragmentTransaction = this.getSupportFragmentManager()
.beginTransaction();
fragmentTransaction.add(R.id.maplayout, supportMap);
fragmentTransaction.commit();
mGoogleMap = supportMap.getMap(); // mGoogleMap = null after this
// mGoogleMap.setMyLocationEnabled(true); Obviously NullPointerException
}
}
main.xml中
<RelativeLayout
android:id="@+id/maplayout"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_alignBottom="@+id/tracking"
android:layout_below="@id/header" />
答案 0 :(得分:0)
此代码:
mGoogleMap = supportMap.getMap()
;
应该在onCreate之后调用。试试onResume(),就像这样。
@Override
protected void onResume() {
super.onResume();
// here it should work
mGoogleMap = supportMap.getMap();
// In case Google Play services has since become available.
setUpMapIfNeeded();
}