SupportMapFragment or GoogleMap is null

2020-03-06 06:44发布

Managed to get the codes error free, however upon launching, I somehow always get a null pointer exception at the line mMap = mapFrag.getMap();

Why is this so? Am I missing some imports or some steps? I am unsure if it's the SupportMapFragment or GoogleMap object that's causing the problem.

package com.fragments;

import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.google.android.gms.maps.GoogleMap;
import com.google.android.gms.maps.SupportMapFragment;

public class MapFragment extends SherlockMapFragment {

    private GoogleMap mMap;

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View root = super.onCreateView(inflater, container, savedInstanceState);
        SupportMapFragment mapFrag= (SupportMapFragment)getSupportFragmentManager().findFragmentById(R.id.fragment_map);
        mMap = mapFrag.getMap(); //null pointer is here
        return root;
    }
}

Edit: This is part of the implementation based on the solution given in this question here

2条回答
做个烂人
2楼-- · 2020-03-06 07:19

There is no need of implementing an extra class such as SherlockMapFragment. You can handle the SupportMapFragment inside of the Fragment or the SherlockFragment class code. Look at this.

查看更多
甜甜的少女心
3楼-- · 2020-03-06 07:35

Note that you do not need to use a custom Fragment subclass necessarily to use Maps V2. If your fragment is just purely a map, you can create the MapFragment or SupportMapFragment from the activity and configure it there.

You do not even need to create some sort of SherlockMapFragment to be able to have a map be part of an ActionBarSherlock-based app. The regular SupportMapFragment works just fine.

If you do want to have more business smarts in your fragment, and if you are using ActionBarSherlock, and the business logic in question needs to do things related to ActionBarSherlock (e.g., contribute action items to the action bar), then and only then do you need to worry about having some sort of SherlockMapFragment.

I can confirm that this gist contains a working SherlockMapFragment. Note that it goes into the com.actionbarsherlock.app package, as it needs some package-protected access to the rest of ActionBarSherlock.

You can then subclass that, such as creating a MyMapFragment:

public class MyMapFragment extends SherlockMapFragment {
  @Override
  public void onActivityCreated(Bundle savedInstanceState) {
    super.onActivityCreated(savedInstanceState);

    if (getMap() != null) {
      Log.d(getClass().getSimpleName(), "Map ready for use!");
    }
  }
}

You have to be a bit careful on the timing of calling getMap() -- too soon and it will return null. onActivityCreated() seems like a fairly safe time, though you are free to experiment.

Then, you just use MyMapFragment wherever you would have used SupportMapFragment:

<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.commonsware.android.mapsv2.sherlock.MyMapFragment"/>

Here is the complete project containing the above code.

查看更多
登录 后发表回答