5

在我的应用程序中,我在片段中显示了谷歌地图版本 2。但我得到空指针异常

mMap = ((SupportMapFragment) getFragmentManager().findFragmentById(R.id.map)).getMap();

这是我的完整代码:

    public class SearchResultMap extends Fragment{

    private GoogleMap mMap;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
            Bundle savedInstanceState) {
        View fragmentView = inflater.inflate(R.layout.map, container, false);

        mMap = ((SupportMapFragment) getFragmentManager().findFragmentById(R.id.map)).getMap(); // line no : 28

         mMap.addMarker(new MarkerOptions() .position(new LatLng(xxxxxx,xxxxxx)) .title("Current Location")
         .icon(BitmapDescriptorFactory.fromResource(R.drawable.ball_pointer))
         .snippet("xxxxx"));

        return fragmentView;
    }

    @Override
    public void onActivityCreated(Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
    }

}

xml:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical" >

   <fragment
        android:id="@+id/map"
        android:name="com.google.android.gms.maps.SupportMapFragment"
        android:layout_width="match_parent"
        android:layout_height="match_parent" /> 

</LinearLayout>

清单 xml:

<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.fssd.spot"
    android:versionCode="1"
    android:versionName="1.0" >

    <uses-sdk
        android:minSdkVersion="8"
        android:targetSdkVersion="17" />

    <permission
        android:name="com.fssd.spot.permission.MAPS_RECEIVE"
        android:protectionLevel="signature" />

    <uses-feature
        android:glEsVersion="0x00020000"
        android:required="true" />

    <uses-permission android:name="com.fssd.spot.permission.MAPS_RECEIVE" />
    <uses-permission android:name="android.permission.INTERNET" />
    <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" />
    <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />

    <application
        android:allowBackup="true"
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/AppTheme" >
        <activity
            android:name="com.fssd.spot.SplashScreen"
            android:theme="@android:style/Theme.Light.NoTitleBar"
            android:noHistory="true"
            android:screenOrientation="portrait" >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
        <activity
            android:name="com.fssd.spot.MainActivity"
            android:screenOrientation="portrait"
            android:theme="@style/tabTheme" >
        </activity>

        <meta-data
            android:name="com.google.android.maps.v2.API_KEY"
            android:value="xxxxxxxxxxxxxxxxxxxxxxxx" />

    </application>

</manifest>

整只原木猫:

FATAL EXCEPTION: main
java.lang.NullPointerException
at com.fssd.spot.search.SearchResultMap.onCreateView(SearchResultMap.java:28)
at android.support.v4.app.Fragment.performCreateView(Fragment.java:1478)
at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:927)
at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1104)
at android.support.v4.app.BackStackRecord.run(BackStackRecord.java:682)
at android.support.v4.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:1460)
at android.support.v4.app.Fragment.performStart(Fragment.java:1499)
at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:957)
at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1104)
at android.support.v4.app.BackStackRecord.run(BackStackRecord.java:682)
at android.support.v4.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:1460)
at android.support.v4.app.FragmentManagerImpl$1.run(FragmentManager.java:440)
at android.os.Handler.handleCallback(Handler.java:587)
at android.os.Handler.dispatchMessage(Handler.java:92)
at android.os.Looper.loop(Looper.java:130)
at android.app.ActivityThread.main(ActivityThread.java:3683)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:507)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:875)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:633)
at dalvik.system.NativeStart.main(Native Method)
4

8 回答 8

17

这些解决方案都没有为我解决这个问题——但马克墨菲(https://code.google.com/p/android/issues/detail?id=77714)的回答确实:

使用 getChildFragmentManager() 访问嵌套片段,而不是 getFragmentManager()。http://developer.android.com/reference/android/support/v4/app/Fragment.html#getChildFragmentManager%28%29

IE:

public class MapFragment extends Fragment {
    private GoogleMap map;
    private SupportMapFragment mapFragment;
    ...

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup vg, Bundle data) {
        View view = inflater.inflate(R.layout.map_view, vg, false);
        FragmentManager fm = getChildFragmentManager();
        mapFragment = (SupportMapFragment) fm.findFragmentById(R.id.map);
        mapFragment.getMapAsync(new OnMapReadyCallback() {
            public void onMapReady(GoogleMap googleMap) {
                map = googleMap;
                initMap();
            }
        });
    }
}
于 2015-01-01T16:31:54.703 回答
11

尝试改变这一点:

FragmentManager myFM = getActivity().getSupportFragmentManager();

final SupportMapFragment myMAPF = (SupportMapFragment) myFM
                .findFragmentById(R.id.map);
于 2013-09-04T11:19:40.790 回答
0

尝试更改为

mMap = ((SupportMapFragment) getSupportFragmentManager().findFragmentById(R.id.map)).getMap();

您正在使用 getFragmentManager(),但将其转换为 SupportMapFragment

您还需要扩展 FragmentActivity:http: //developer.android.com/reference/android/support/v4/app/FragmentActivity.html

于 2013-09-04T11:12:44.950 回答
0
mMap = ((SupportMapFragment) getSupportFragmentManager().findFragmentById(R.id.map)).getMap();

if(map != null) {
         mMap.addMarker(new MarkerOptions() .position(new LatLng(xxxxxx,xxxxxx)) .title("Current Location")
         .icon(BitmapDescriptorFactory.fromResource(R.drawable.ball_pointer))
         .snippet("xxxxx"));
}

<meta-data
            android:name="com.google.android.maps.v2.API_KEY"
            android:value="xxxxxxxxxxxxxxxxxxxxxxxx" />

将您的 google api 密钥放在这里。你从 google 获得 阅读文档

于 2013-09-04T11:21:19.283 回答
0

经过数小时的搜索,我这样做了:

 public class MapFragment extends Fragment {
    private GoogleMap map;
    SupportMapFragment mapFrag;

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
            Bundle savedInstanceState) {
        // TODO Auto-generated method stub
        setHasOptionsMenu(true);
        View rootView = inflater.inflate(R.layout.fragment_map, container,
                false);

        fm = getActivity().getSupportFragmentManager();
        map = ((SupportMapFragment) fm.findFragmentById(R.id.map)).getMap();

        return rootView;

    }
}
于 2014-08-27T06:29:31.230 回答
0

尝试添加tools:context=".YourActivity"到片段 xml:

<fragment xmlns:android="http://schemas.android.com/apk/res/android"
    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=".YourActivity" />
于 2015-05-21T11:08:45.240 回答
0

可能上面的答案有效,但对我有用的是这个。

 {
    SupportPlaceAutocompleteFragment supportPlaceAutocompleteFragmentSearch;
    FragmentManager fragmentManagerSearch = getChildFragmentManager();
    supportPlaceAutocompleteFragmentSearch = 
                                            (SupportPlaceAutocompleteFragment)
                                            fragmentManagerSearch.findFragmentById(R.id.place_autocomplete_fragment_search);
    }
于 2016-05-12T06:07:35.447 回答
0

如果您使用 Kotlin开发代码,并希望将 MapFragment 添加到您需要使用的另一个 Fragment 中,SupportMapFragment但如果这样做,您将得到空异常,因此您应该使用 childFragmentManager 而不是 并且代码不supportFragmentManager 应该只是添加以下代码:onCreateViewonCreate

private lateinit var mapFragment : SupportMapFragment

 mapFragment = childFragmentManager.findFragmentById(R.id.mapview) as SupportMapFragment
    mapFragment.getMapAsync { googleMap ->
        this.googleMap = googleMap
        setGoogleMapSettings()
    }
于 2019-10-16T17:19:05.397 回答