java.lang.NullPointerException: MapFragment.getMapAsync(com.google.android.gms.maps.OnMapReadyCallback)

java.lang.NullPointerException: MapFragment.getMapAsync(com.google.android.gms.maps.OnMapReadyCallback)

我正在处理地理围栏,并尝试初始化地图,但出现此错误:

java.lang.NullPointerException: Attempt to invoke virtual method 'void com.google.android.gms.maps.MapFragment.getMapAsync(com.google.android.gms.maps.OnMapReadyCallback)' on a null object reference

我也尝试了我在 Whosebug 上找到的所有解决方案,但我找不到问题的解决方案。

 private void initGMaps(){
    mapFragment = (MapFragment) getFragmentManager().findFragmentById(R.id.map);
    mapFragment.getMapAsync(this);
}



 protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        textLat = (TextView) findViewById(R.id.lat);
        textLong = (TextView) findViewById(R.id.lon);

        // initialize GoogleMaps

        initGMaps();

        // create GoogleApiClient
        createGoogleApi();
    }

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

E/AndroidRuntime: FATAL EXCEPTION: main Process: com.utkukayman.locationbasedreminderbyutku, PID: 22875 java.lang.RuntimeException: Unable to start activity ComponentInfo{com.utkukayman.locationbasedreminderbyutku/com.utkukayman.locationbasedreminderbyutku.MapsActivity}: java.lang.NullPointerException: Attempt to invoke virtual method 'void com.google.android.gms.maps.MapFragment.getMapAsync(com.google.android.gms.maps.OnMapReadyCallback)' on a null object reference at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2665) at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2726) at android.app.ActivityThread.-wrap12(ActivityThread.java) at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1477) at android.os.Handler.dispatchMessage(Handler.java:102) at android.os.Looper.loop(Looper.java:154) at android.app.ActivityThread.main(ActivityThread.java:6119) at java.lang.reflect.Method.invoke(Native Method) at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:886) at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:776) Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'void com.google.android.gms.maps.MapFragment.getMapAsync(com.google.android.gms.maps.OnMapReadyCallback)' on a null object reference at com.utkukayman.locationbasedreminderbyutku.MapsActivity.initGMaps(MapsActivity.java:188) at com.utkukayman.locationbasedreminderbyutku.MapsActivity.onCreate(MapsActivity.java:82) at android.app.Activity.performCreate(Activity.java:6679) at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1118) at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2618) at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2726)  at android.app.ActivityThread.-wrap12(ActivityThread.java)  at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1477)  at android.os.Handler.dispatchMessage(Handler.java:102)  at android.os.Looper.loop(Looper.java:154)  at android.app.ActivityThread.main(ActivityThread.java:6119)  at java.lang.reflect.Method.invoke(Native Method)  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:886)  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:776)  Application terminated.

谁能帮帮我?感谢您的回答

你在膨胀 SupportMapFragment,而不是 MapFragment

在 activity 中使用 getSupportFragmentManager() 并将 findFragment() 结果转换为 SupportMapFragment

您的 activity 需要根据您的用例扩展 AppCompatActivityFragmentActivity

对不起,这都是我的愚蠢错误。

super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); ----->应该是activity_maps