mapFragment.getMapAsync(this) - NullPointerException

Neu*_*ino 31 android google-maps android-maps-v2

我使用的com.google.android.gms:play-services-maps:7.5.0是谷歌地图服务版本.当试图打电话给下面我得到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.

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {
    SupportMapFragment mapFragment = (SupportMapFragment) getActivity().getSupportFragmentManager()
            .findFragmentById(R.id.map);
    mapFragment.getMapAsync(this); //error

    return inflater.inflate(R.layout.fragment_example_map, container, false);
}
Run Code Online (Sandbox Code Playgroud)

fragment_example_map.xml文件:

<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context="app.ExampleMap">

    <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"/>
</FrameLayout>
Run Code Online (Sandbox Code Playgroud)

Min*_*wzy 121

我在带有片段的地图中的评论,首先你应该提醒你的观点,因为你会从中调用一些东西,这就是为什么我先把我的观点充气
View view = inflater.inflate(R.layout.activity_maps, null, false);

第二次调用子片段管理器this.getChildFragmentManager()而不是getActivity().getSupportFragmentManager()

以下是查看地图的完整示例

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
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 ClinicFragment extends Fragment implements OnMapReadyCallback {

    private GoogleMap mMap;

    public static ClinicFragment newInstance() {
        ClinicFragment fragment = new ClinicFragment();
        return fragment;
    }

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.activity_maps, null, false);

        SupportMapFragment mapFragment = (SupportMapFragment) this.getChildFragmentManager()
                .findFragmentById(R.id.map);
        mapFragment.getMapAsync(this);

        return view;
    }


    /**
     * Manipulates the map once available.
     * This callback is triggered when the map is ready to be used.
     * This is where we can add markers or lines, add listeners or move the camera. In this case,
     * we just add a marker near Sydney, Australia.
     * If Google Play services is not installed on the device, the user will be prompted to install
     * it inside the SupportMapFragment. This method will only be triggered once the user has
     * installed Google Play services and returned to the app.
     */
    @Override
    public void onMapReady(GoogleMap googleMap) {
        mMap = googleMap;

        // Add a marker in Sydney and move the camera
        LatLng sydney = new LatLng(-34, 151);
        mMap.addMarker(new MarkerOptions().position(sydney).title("Marker in Sydney"));
        mMap.moveCamera(CameraUpdateFactory.newLatLng(sydney));
    }
}
Run Code Online (Sandbox Code Playgroud)

必要的许可

<permission
    android:name="your.package.name.permission.MAPS_RECEIVE"
    android:protectionLevel="signature" />
<uses-permission android:name="your.package.name.permission.MAPS_RECEIVE"/>

<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="com.google.android.providers.gsf.permission.READ_GSERVICES" />
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
Run Code Online (Sandbox Code Playgroud)

加上特征元素

<uses-feature
    android:glEsVersion="0x00020000"
    android:required="true"/>
Run Code Online (Sandbox Code Playgroud)

大多数导入谷歌地图键

<meta-data
    android:name="com.google.android.maps.v2.API_KEY"
    android:value="your_key_here" />
Run Code Online (Sandbox Code Playgroud)

如果您遇到错误膨胀类片段,更新添加此元数据

<meta-data 
      android:name="com.google.android.geo.API_KEY"  
      android:value="your_key_here" />
Run Code Online (Sandbox Code Playgroud)

主要节日的夏日

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="your.package.name" >
// permission here

 <application
        android:name=".Activities.MyApplication"
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/AppTheme" >

    // feature element 
    // maps key

  </application>

</manifest>
Run Code Online (Sandbox Code Playgroud)

activity_maps.xml

<fragment xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:map="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/map"
    android:name="com.google.android.gms.maps.SupportMapFragment"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context="com.imaadv.leaynik.ClinicFragment" />
Run Code Online (Sandbox Code Playgroud)

  • 为什么这个答案不是最佳答案?非常感谢你的回答. (11认同)

Aru*_*mar 20

用来 getChildFragmentManager()代替getActivity().getSupportFragmentManager() 片段.

喜欢:

SupportMapFragment mapFragment = (SupportMapFragment)getChildFragmentManager()
            .findFragmentById(R.id.map);
    mapFragment.getMapAsync(this); 
Run Code Online (Sandbox Code Playgroud)


Com*_*are 12

您试图在片段存在之前找到它.您指示具有该片段的布局是fragment_example_map.xml.但是,您试图在膨胀该布局文件之前找到地图片段.这不行.

除此之外,您似乎试图从该片段的onCreateView()方法中获取另一个片段内的地图片段.我不知道为什么你在这里嵌套片段,因为它似乎会使你的代码更复杂,更脆弱,没有明显的好处.

  • 摆脱了`onCreateView`,类现在扩展了`SupportMapFragment`,添加了`onActivityCreated`,它现在似乎正在工作,谢谢 (2认同)

Dev*_*nom 12

面临同样的问题。由于您使用的地图片段的片段中使用getChildFragmentManager(),而不是getActivity().getFragmentManager()getFragmentManager()


小智 9

如果您android:name="com.google.android.gms.maps.MapFragment"在片段中使用,则更改您的代码:

MapFragment mapFragment1 = (MapFragment)getFragmentManager().findFragmentById(R.id.map);
        mapFragment1.getMapAsync(this);
Run Code Online (Sandbox Code Playgroud)

但是,如果您android:name="com.google.android.gms.maps.SupportMapFragment" 在片段中使用,则使用以下代码:

SupportMapFragment mapFragment = (SupportMapFragment) getSupportFragmentManager().findFragmentById(R.id.map);
        mapFragment.getMapAsync(this);
Run Code Online (Sandbox Code Playgroud)


小智 5

这对我有用:

SupportMapFragment mapFragment = (SupportMapFragment)getChildFragmentManager()
            .findFragmentById(R.id.map);
    mapFragment.getMapAsync(this); 
Run Code Online (Sandbox Code Playgroud)