0

フラグメントにマップを表示しようとすると、null ポインター例外が返されます。すべての権限をマニフェストにすべて追加しました。フラグメント ファイル、その XML ファイル、およびログの cat を添付しています。

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

ログキャット

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(); です。初期化マップ();

4

3 に答える 3

2

マップがロードされるまで待つ必要があります....

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 を使用してください。

于 2016-04-05T11:31:47.010 に答える
0

このコードを置き換えます

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

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

それをチェックしてください!

于 2016-04-05T11:34:11.910 に答える