我无法得到地图!我可以得到的都是null.
这里是代码.
public class MainActivity extends FragmentActivity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); SupportMapFragment fragment = new SupportMapFragment(); getSupportFragmentManager().beginTransaction() .add(android.R.id.content,fragment).commit(); GoogleMap map; map = fragment.getMap(); //here i cant access this snippet because map = null if(map!=null){ UiSettings mm = map.getUiSettings(); map.setMyLocationEnabled(true); } } }
清单:
<?xml version="1.0" encoding="utf-8"?> <manifest xmlns:android="http://schemas.android.com/apk/res/android" package="com.example.anywhere_reminder" android:versionCode="1" android:versionName="1.0" > <uses-sdk android:minSdkVersion="8" android:targetSdkVersion="17" /> <uses-feature android:glEsVersion="0x00020000" android:required="true"/> <permission android:name="com.example.anywhere_reminder.permission.MAPS_RECEIVE" android:protectionLevel="signature" /> <uses-permission android:name="com.example.anywhere_reminder.permission.MAPS_RECEIVE" /> <uses-permission android:name="android.permission.INTERNET" /> <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" /> <uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" /> <uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" /> <uses-permission android:name="com.google.android.providers.gsf.permission.READ_GSERVICES" /> <application android:allowBackup="true" android:icon="@drawable/ic_launcher" android:label="@string/app_name" android:theme="@style/AppTheme" > <activity android:name="com.google.android.gms.BuildConfig" /> <activity android:name="com.example.anywhere_reminder.MainActivity" android:label="@string/app_name" > <intent-filter> <action android:name="android.intent.action.MAIN" /> <category android:name="android.intent.category.LAUNCHER" /> </intent-filter> </activity> <Meta-data android:name="com.google.android.maps.v2.API_KEY" android:value=" my key "/> </application> </manifest>
这里是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 xmlns:android="http://schemas.android.com/apk/res/android" android:id="@+id/map" android:layout_width="match_parent" android:layout_height="match_parent" class="com.google.android.gms.maps.MapFragment"/> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_centerHorizontal="true" android:layout_centerVertical="true" android:text="@string/hello_world" /> </RelativeLayout>
我试图解决它像这个解决方案Google Maps Android API v2 throws GooglePlayServicesNotAvailableException,out of date,SupportMapFragment.getMap() returns null
..但仍然没有工作
更新:
我的地图现在工作了..这里是编辑的工作版本
public class MainActivity extends FragmentActivity { private GoogleMap myMap; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); android.support.v4.app.FragmentManager myFragmentManager = getSupportFragmentManager(); SupportMapFragment mySupportMapFragment = (SupportMapFragment)myFragmentManager.findFragmentById(R.id.map); myMap = mySupportMapFragment.getMap(); if(myMap!=null) myMap.setMyLocationEnabled(true); }