0

我正在编写一个使用 Google 地图 APIv2 的 Android 应用程序。它几乎适用于所有设备。但是Android 4.0.4 和4.2.2 有问题。地图初始化期间应用程序崩溃。

在 Android 4.0.4 上它在重新启动后工作,在 4.2.2 上它不起作用。

这里的初始化代码:

public class MyMapActivity extends FragmentActivity implements LocationListener
{
      private GoogleMap map;
      @Override
      protected void onCreate(Bundle savedInstanceState) 
      {
         super.onCreate(savedInstanceState);
         setContentView(R.layout.activity_my_map);
         // Getting Google Play availability status
         int status = GooglePlayServicesUtil.isGooglePlayServicesAvailable(getBaseContext());

         // Showing status
         if(status!=ConnectionResult.SUCCESS)
         { // Google Play Services are not available
               int requestCode = 10;
               Dialog dialog = GooglePlayServicesUtil.getErrorDialog(status, this, requestCode);
               dialog.show();
         }
         else 
         {// Google Play Services are available
               // Getting reference to the SupportMapFragment of activity_main.xml
               SupportMapFragment fm = (SupportMapFragment)getSupportFragmentManager().findFragmentById(R.id.map);

               // Getting GoogleMap object from the fragment
               map = fm.getMap();

               // Enabling MyLocation Layer of Google Map
               map.setMyLocationEnabled(true);
         }
      }   
}

activity_my_map.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
    android:id="@+id/map"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    class="com.google.android.gms.maps.SupportMapFragment" />

</RelativeLayout> 
4

1 回答 1

0

我的猜测是 getMap 可能返回 null,尝试设置您的代码有点像这样:

private GoogleMap mMap;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.basic_demo);

    SupportMapFragment mapFragment =
            (SupportMapFragment) getSupportFragmentManager().findFragmentById(R.id.map);

    if (savedInstanceState == null) {
        // First incarnation of this activity.
        mapFragment.setRetainInstance(true);
    } else {
        // Reincarnated activity. The obtained map is the same map instance in the previous
        // activity life cycle. There is no need to reinitialize it.
        mMap = mapFragment.getMap();
    }
    setUpMapIfNeeded();
}

@Override
protected void onResume() {
    super.onResume();
    setUpMapIfNeeded();
}

private void setUpMapIfNeeded() {
    if (mMap == null) {
        mMap = ((SupportMapFragment) getSupportFragmentManager().findFragmentById(R.id.map))
                .getMap();
        if (mMap != null) {
            // Enabling MyLocation Layer of Google Map
            map.setMyLocationEnabled(true);
        }
    }
}

代码来自 API 演示 RetainMapActivity

于 2013-10-27T12:46:41.110 回答