Android SupportMapFragment.getMap() returns null

后端 未结 2 970
梦如初夏
梦如初夏 2021-01-07 03:49

in my following code, getMap() returns null, which stop the app. If I don\'t do anything with the map (i.e, removing the last two lines), it shows correctly. Any idea why? T

相关标签:
2条回答
  • 2021-01-07 04:43

    If getMap() is returning null it's because Map isn't ready yet or GooglePlayServices is out to date. You better use something like that :

    int resultCode = GooglePlayServicesUtil.isGooglePlayServicesAvailable(this);
            if(resultCode != ConnectionResult.SUCCESS)
            {
                Builder builder = new AlertDialog.Builder(Main.this);
                builder.setMessage(getResources().getString(R.string.error_getting_maps));
                builder.setCancelable(true);
                builder.setPositiveButton("OK", null);
                AlertDialog dialog = builder.create();
                dialog.show();
            } else {
                _map = ((SupportMapFragment)getSupportFragmentManager().findFragmentById(R.id.map)).getMap();
                [ETC]
            }
    
    0 讨论(0)
  • 2021-01-07 04:48

    Duplicate of: SupportMapFragment.getmap() returns null

    Check your layout xml file. I noticed that I wasn't referencing: android:name="com.google.android.gms.maps.SupportMapFragment" but rather: android:name="com.google.android.gms.maps.MapFragment"

    The name should be: android:name="com.google.android.gms.maps.SupportMapFragment"

    0 讨论(0)
提交回复
热议问题