有人可以告诉我为什么我的getMapAsync()返回nullpointerexception吗?以及如何解决。我已经删除了setContentView()并使用了onCreateView(),所以通常我的视图应该膨胀,不是吗?
感谢您的帮助!
我从主代码中收到以下错误:
Process: com.example.flow, PID: 9070
java.lang.NullPointerException: Attempt to invoke virtual method 'void com.google.android.gms.maps.SupportMapFragment.getMapAsync(com.google.android.gms.maps.OnMapReadyCallback)' on a null object reference
at com.example.flow.displayClasses.WebscrapingScreens.GoogleMapsFragment.onCreate(GoogleMapsFragment.java:100)
at android.support.v4.app.Fragment.performCreate(Fragment.java:2414)
at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1418)
at android.support.v4.app.FragmentTransition.addToFirstInLastOut(FragmentTransition.java:1195)
at android.support.v4.app.FragmentTransition.calculateFragments(FragmentTransition.java:1078)
at android.support.v4.app.FragmentTransition.startTransitions(FragmentTransition.java:117)
at android.support.v4.app.FragmentManagerImpl.executeOpsTogether(FragmentManager.java:2408)
这是我的片段:
public class GoogleMapsFragment extends Fragment implements OnMapReadyCallback, LocationResultListener, PlaceSelectedListener {
private static final int ACTIVITY_RQEUEST_CODE = 1000;
private static final int PERMISSION_REQUEST_CODE = 1000;
private static final float ZOOM_LEVEL = 15.0f;
private static final int REQUEST_LIMIT = 3;
private final String API_KEY = "MyKey";
private final String PLACES_REQUEST = "https://maps.googleapis.com/maps/api/place/nearbysearch/json?&key=" + API_KEY;
private GoogleMap googleMap;
private ClusterManager<MarkClusterItem> clusterManager;
private LocationHandler locationHandler;
private int requestCount;
private String nextPageToken;
private ProgressBar progressBar;
private List<GooglePlace> listPlaces = new ArrayList<>();
private FragmentActivity myContext;
public GoogleMapsFragment() {
// Required empty public constructor
}
@Override
public void onAttach(Activity activity) {
myContext=(FragmentActivity) activity;
super.onAttach(activity);
}
@Nullable
@Override
public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
View RootView = inflater.inflate(R.layout.fragment_mapsgoogle, container, false);
return RootView;}
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
progressBar = getActivity().findViewById(R.id.progressBar);
locationHandler = new LocationHandler(this, this, ACTIVITY_RQEUEST_CODE, PERMISSION_REQUEST_CODE);
SupportMapFragment supportMapFragment = (SupportMapFragment) getActivity().getSupportFragmentManager().findFragmentById(R.id.googleMap);
supportMapFragment.getMapAsync(this);
}