9

我拿不到地图!我能得到的都是空的。

这是代码。

       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() 返回 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);


        }
4

5 回答 5

17

您正在通过FragmentTransaction. 当你调用commit()时,片段还没有被添加到屏幕上,因为FragmentTransaction它只是被安排发生——它还没有发生。因此,SupportMapFragment还没有被调用onCreateView(),所以没有GoogleMap

要么切换到静态片段(<fragment>布局中的标记),要么延迟使用,GoogleMap直到事务处理完毕。

于 2013-02-16T11:52:48.313 回答
6

FragmentManager 类中的 executePendingTransactions() 旨在修复此延迟。来自文档:在使用 FragmentTransaction.commit() 提交 FragmentTransaction 后,它被安排在进程的主线程上异步执行。如果您想立即执行任何此类挂起的操作,您可以调用此函数(仅从主线程)来执行此操作。请注意,所有回调和其他相关行为都将在此调用中完成,因此请注意从何处调用它。

于 2013-02-17T01:30:13.087 回答
6

我扩展了 MapFragment 类并添加了一个监听器。关于 getMap() 的文档说:

... 如果片段的视图尚未准备好,则为 Null。如果片段生命周期尚未通过 onCreateView(LayoutInflater, ViewGroup, Bundle) ,则可能会发生这种情况......

然后我在 onCreateView 之后调用监听器。我添加类

public class MySupportMapFragment extends SupportMapFragment{

private MySupportMapFragmentListener listener;

public interface MySupportMapFragmentListener{
    public void onMapCreated(GoogleMap googleMap);
}

// value taken from source code
private static final String MAP_OPTIONS = "MapOptions";

public static MySupportMapFragment newInstance() {
        MySupportMapFragment f = new MySupportMapFragment();
        return f;
}

public static MySupportMapFragment newInstance(GoogleMapOptions options) {
        MySupportMapFragment f = new MySupportMapFragment();
        Bundle args = new Bundle();
        args.putParcelable(MAP_OPTIONS, options);
        f.setArguments(args);
        return f;
}

// other newInstance methods ...


/* (non-Javadoc)
 * @see android.support.v4.app.Fragment#onViewCreated(android.view.View, android.os.Bundle)
 */
@Override
public void onViewCreated(View view, Bundle savedInstanceState) {
    // TODO Auto-generated method stub
    super.onViewCreated(view, savedInstanceState);
    // here, as doc say, the map can be initialized, or the service is not available
    if(listener!=null){
        listener.onMapCreated(getMap());
    }

}
/**
 * @return the listener
 */
public MySupportMapFragmentListener getListener() {
    return listener;
}
/**
 * @param listener the listener to set
 */
public void setListener(MySupportMapFragmentListener listener) {
    this.listener = listener;
}

}

在调用活动或片段之后......

    mapFragment = MySupportMapFragment.newInstance();
    mapFragment.setListener(new MySupportMapFragmentListener() {
        @Override
        public void onMapCreated(GoogleMap googleMap) {
            // TODO Auto-generated method stub          
            if(googleMap!=null){
                  // service is unaviable
            }               
        }
于 2014-11-07T16:45:01.620 回答
3

移动代码片段:

谷歌地图;地图 = 片段.getMap();

//这里地图不为空

如果(地图!=空){

UiSettings mm = map.getUiSettings();
map.setMyLocationEnabled(true);

}

到 onResume() 方法,这将解决您的问题。

于 2013-08-28T04:38:05.273 回答
1

另一种解决方法。在 MapFragment 中实现一个接口,以便在活动准备好创建 GoogleMap 时与活动通信。

    @Override
public void onActivityCreated(Bundle savedInstanceState) {
    // TODO Auto-generated method stub
    super.onActivityCreated(savedInstanceState);
    listener.onGoogleMapCreattion();


}

然后你只需要在activtiy中实现监听器:

    @Override
public void onGoogleMapCreation() {
    setUpMapIfNeeded();

}

    private void setUpMapIfNeeded() {
    // Do a null check to confirm that we have not already instantiated the map.
    if (mMap == null) {
         mMap = mMapFragment.getMap();
        // Check if we were successful in obtaining the map.
        if (mMap != null) {
          // The Map is verified. It is now safe to manipulate the map.
            setUpMap();

        }
    }
  }
于 2013-05-06T10:23:35.350 回答