Java 谷歌地图-Android应用程序未加载-空对象引用

Java 谷歌地图-Android应用程序未加载-空对象引用,java,android,google-maps,Java,Android,Google Maps,我目前正在尝试开发一款Android应用程序,并与谷歌地图集成。目前我很难找到错误,因为代码来自google站点本身,只是它是一个SupportMapFragment。如果您知道使用SupportMapFragment的实际教程,也会很棒。实际的,因为我认为.getMap()被贬值了。如果我要发布整个代码,请让我知道(我是stackoverflow的新手) 如果尝试使用此代码: import android.content.Context; import android.location.Cri

我目前正在尝试开发一款Android应用程序,并与谷歌地图集成。目前我很难找到错误,因为代码来自google站点本身,只是它是一个SupportMapFragment。如果您知道使用SupportMapFragment的实际教程,也会很棒。实际的,因为我认为
.getMap()
被贬值了。如果我要发布整个代码,请让我知道(我是stackoverflow的新手)

如果尝试使用此代码:

import android.content.Context;
import android.location.Criteria;
import android.location.Location;
import android.location.LocationManager;
import android.os.Bundle;
import android.support.v4.app.FragmentManager;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.google.android.gms.maps.CameraUpdateFactory;
import com.google.android.gms.maps.GoogleMap;
import com.google.android.gms.maps.OnMapReadyCallback;
import com.google.android.gms.maps.SupportMapFragment;
import com.google.android.gms.maps.model.LatLng;
import com.google.android.gms.maps.model.MarkerOptions;

public class MapsFragment extends SupportMapFragment implements OnMapReadyCallback {
其中函数为:

    @Override
     public void onResume() {
    FragmentManager fragmentManager = getActivity().getSupportFragmentManager();
    SupportMapFragment supportMapFragment = (SupportMapFragment) fragmentManager
            .findFragmentById(R.id.map);

    supportMapFragment.getMapAsync(this);
}
错误是:
java.lang.NullPointerException:尝试在空对象引用上调用虚拟方法'void com.google.android.gms.maps.SupportMapFragment.getMapAsync(com.google.android.gms.maps.OnMapReadyCallback)'

片段xml如下所示:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/root"
    android:layout_width="match_parent"
    android:layout_height="match_parent">
    <fragment
        android:id="@+id/map"
        android:name="com.google.android.gms.maps.SupportMapFragment"
        android:layout_width="match_parent"
        android:layout_height="match_parent"/>
</RelativeLayout>

您似乎需要使用
片段
支持片段管理器

查看此

地图片段在棒棒糖上出现问题的答案,您可能需要此

SupportMapFragment supportMapFragment;
if (Build.VERSION.SDK_INT < 21) {
    supportMapFragment = (SupportMapFragment) getActivity()
        .getSupportFragmentManager().findFragmentById(R.id.map);
} else {
    supportMapFragment = (SupportMapFragment) getActivity()
        .getChildFragmentManager().findFragmentById(R.id.map);
}
supportMapFragment.getMapAsync(this);
SupportMapFragment SupportMapFragment;
if(Build.VERSION.SDK_INT<21){
supportMapFragment=(supportMapFragment)getActivity()
.getSupportFragmentManager().findFragmentById(R.id.map);
}否则{
supportMapFragment=(supportMapFragment)getActivity()
.getChildFragmentManager().findFragmentById(R.id.map);
}
supportMapFragment.getMapAsync(此文件);

此代码有所帮助,但应用程序仍然崩溃。出现此错误:
java.lang.NullPointerException:尝试在空对象引用上调用接口方法'void com.google.maps.api.android.lib6.c.ey.o()'
这正是我当前遇到的错误-“'void com.google.maps.api.android.lib6.c.ey.o()'在空对象引用上”