当我尝试在片段上显示地图时,它返回空指针异常。
我已将所有权限添加到清单中。
我将片段文件附加为其XML文件和log cat

Chatffragment.java

  public class ChatFragment extends Fragment{
        // Google Map
        private GoogleMap googleMap;

        public ChatFragment() {
            // Required empty public constructor
        }

        @Override
        public void onCreate(Bundle savedInstanceState) {

            super.onCreate(savedInstanceState);
        }

        @Override
        public View onCreateView(LayoutInflater inflater, ViewGroup container,
                                 Bundle savedInstanceState)
        {

            View chatfrag = inflater.inflate(R.layout.chatfragment, container, false);


            try {
                // Loading map
                initilizeMap();

            } catch (Exception e) {
                e.printStackTrace();
            }
            return chatfrag;
        }
            /**
             * function to load map. If map is not created it will create it for you
             * */

        private void initilizeMap()
        {
            if (googleMap == null) {
                SupportMapFragment mapFrag = (SupportMapFragment) getActivity().getSupportFragmentManager().findFragmentById(R.id.map);

                googleMap = mapFrag.getMap();
                googleMap.setMyLocationEnabled(true);

                // check if map is created successfully or not
                if (googleMap == null) {
                    Toast.makeText(getActivity(),
                            "Sorry! unable to create maps", Toast.LENGTH_SHORT)
                            .show();
                }
            }
        }

        @Override
        public void onResume() {
            super.onResume();
            initilizeMap();


        }




    }


其XML

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_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>


LOGCAT

04-05 09:12:22.075 26987-26987/com.example.vikrant.safeshelter E/AndroidRuntime: FATAL EXCEPTION: main
                                                                                 Process: com.example.vikrant.safeshelter, PID: 26987


java.lang.NullPointerException: Attempt to invoke virtual method 'com.google.android.gms.maps.GoogleMap com.google.android.gms.maps.SupportMapFragment.getMap()' on a null object reference
                                                                                 at com.example.vikrant.safeshelter.ChatFragment.initilizeMap(ChatFragment.java:55)
                                                                                 at com.example.vikrant.safeshelter.ChatFragment.onResume(ChatFragment.java:70)
                                                                                 at android.support.v4.app.Fragment.performResume(Fragment.java:2020)
                                                                                 at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1107)
                                                                                 at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1252)
                                                                                 at android.support.v4.app.BackStackRecord.run(BackStackRecord.java:738)
                                                                                 at android.support.v4.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:1617)
                                                                                 at android.support.v4.app.FragmentManagerImpl.executePendingTransactions(FragmentManager.java:570)
                                                                                 at android.support.v4.app.FragmentStatePagerAdapter.finishUpdate(FragmentStatePagerAdapter.java:164)
                                                                                 at android.support.v4.view.ViewPager.populate(ViewPager.java:1177)
                                                                                 at android.support.v4.view.ViewPager.setCurrentItemInternal(ViewPager.java:608)
                                                                                 at android.support.v4.view.ViewPager.setCurrentItemInternal(ViewPager.java:570)
                                                                                 at android.support.v4.view.ViewPager.setCurrentItem(ViewPager.java:551)
                                                                                 at android.support.design.widget.TabLayout$ViewPagerOnTabSelectedListener.onTabSelected(TabLayout.java:1627)
                                                                                 at android.support.design.widget.TabLayout.selectTab(TabLayout.java:837)
                                                                                 at android.support.design.widget.TabLayout.selectTab(TabLayout.java:809)
                                                                                 at android.support.design.widget.TabLayout$Tab.select(TabLayout.java:1077)
                                                                                 at android.support.design.widget.TabLayout$1.onClick(TabLayout.java:643)
                                                                                 at android.view.View.performClick(View.java:4759)
                                                                                 at android.view.View$PerformClick.run(View.java:19770)
                                                                                 at android.os.Handler.handleCallback(Handler.java:739)
                                                                                 at android.os.Handler.dispatchMessage(Handler.java:95)
                                                                                 at android.os.Looper.loop(Looper.java:135)
                                                                                 at android.app.ActivityThread.main(ActivityThread.java:5234)
                                                                                 at java.lang.reflect.Method.invoke(Native Method)
                                                                                 at java.lang.reflect.Method.invoke(Method.java:372)
                                                                                 at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:909)
                                                                                 at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:704)


55和70行是
googleMap = mapFrag.getMap();
initilizeMap();

最佳答案

您必须等到地图加载完毕。

public class ChatFragment extends Fragment implements OnMapReadyCallback {

    private void initilizeMap() {
      //SupportMapFragment mapFragment = (SupportMapFragment) fragmentManager.findFragmentById(R.id.map);

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

    @Override
    public void onMapReady(final GoogleMap map) {
        googleMap = map;
        //Do other stuff..........
    }

}


编辑

SupportMapFragment mapFrag = (SupportMapFragment) getChildFragmentManager().findFragmentById(R.id.map);


检查编辑代码,使用ChildFragmentManager。

10-08 17:14