问题描述
我得到一个NullPointerException异常有以下code:
I'm getting an NullPointerException with the following code:
private ProjectionProxy proxy = new ProjectionProxy();
private GoogleMap mMap = null;
private Context context = this;
@Override
protected void onCreate(Bundle load) {
super.onCreate(load);
setContentView(R.layout.fragment_ride_tracking);
//mMap = ((MapFragment) getFragmentManager().findFragmentById(R.id.myMapView)).getMap();
mMap = ((MapFragment) getFragmentManager().findFragmentById(R.id.map)).getMap();
int result = GooglePlayServicesUtil.isGooglePlayServicesAvailable(context);
mMap.setMapType(GoogleMap.MAP_TYPE_NORMAL);
mMap.setMyLocationEnabled(true);
//SupportMapFragment mapFragment = (SupportMapFragment) getSupportFragmentManager().findFragmentById(R.id.myMapView);
//mMap = mapFragment.getMap();
Log.e("RideTracking", "Google Map VALUE:"+mMap);
if (mMap != null) {
proxy.setProjection(mMap.getProjection());
}
其中NullPointerException异常是发生该生产线是该行的code:
The line where the NullPointerException is happening is this line of code:
mMap = ((MapFragment) getFragmentManager().findFragmentById(R.id.map)).getMap();
我不初始化我MMAP变量设置为null。什么是我的问题吗?
I'm not initializing my mMap variable to null. What is my issue here?
推荐答案
尝试检查您在活动的XML所创建的MapFragment。检查我的。它的工作。
Try to check the MapFragment that you've created in the XML of the Activity. Check mine; it's working.
...
<fragment
android:id="@+id/map"
android:name="com.google.android.gms.maps.MapFragment"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1" />
...
是你的有什么不同?另外,你检查,如果你把正确的权限和元数据在AndroidManifest.xml中?
Is yours different? Also, you checked if you put the correct permissions and metadata on the AndroidManifest.xml?
下面的权限:
<uses-permission android:name="com.google.android.providers.gsf.permission.READ_GSERVICES" />
和这里的元数据部分:
<application>
...
<meta-data android:name="com.google.android.maps.v2.API_KEY"
android:value="HERE GOES YOUR API KEY" />
</application>
我希望这可以帮助你!
I hope this helps you!
这篇关于MapFragment - NullPointerException异常 - 谷歌地图V2 - 机器人的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!