google maps are returning null pointer exception

痞子三分冷 提交于 2019-12-04 06:06:19

问题


when I am trying to show the maps on my fragment it is returning null pointer exception. I have added everything to the manifest each and every permission. I am attaching the fragment file its XML file and the 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();


        }




    }

Its 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)

line 55 and 70 are googleMap = mapFrag.getMap(); initilizeMap();


回答1:


You have to wait until map getting loaded....

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..........
    }

}

Edit

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

Check the edit code, use ChildFragmentManager.




回答2:


Replace this code

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

with

googleMap = ((SupportMapFragment) getChildFragmentManager().findFragmentById(R.id.map)).getMap();

Check it!




回答3:


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
  1. You can use getChildFragmentManager() instead of getActivity().getSupportFragmentManager()
  2. Remove initilizeMap(); from your onResume method .

Reference

SupportMapFragment.getMap() on a null object reference



来源:https://stackoverflow.com/questions/36424991/google-maps-are-returning-null-pointer-exception

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!