7

在尝试了几乎所有事情之后,如果不提取空对象引用,我似乎无法获取地图。我正在尝试将 google mapfragment 膨胀到一个片段中,但是每次我这样做时,我总是保留一个 getmap null object 。

这是我的代码

在这一点上,我迫切需要任何帮助,我几乎尝试了所有方法。

错误

  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.gnumbu.errolgreen.importedapplication.ViewMapFragment.onViewCreated(ViewMapFragment.java:122)
            at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:971)
            at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1136)
            at android.support.v4.app.BackStackRecord.run(BackStackRecord.java:739)
            at android.support.v4.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:1499)
            at android.support.v4.app.FragmentManagerImpl.executePendingTransactions(FragmentManager.java:488)
            at android.support.v4.app.FragmentPagerAdapter.finishUpdate(FragmentPagerAdapter.java:141)
            at android.support.v4.view.ViewPager.populate(ViewPager.java:1073)
            at android.support.v4.view.ViewPager.populate(ViewPager.java:919)
            at android.support.v4.view.ViewPager.onMeasure(ViewPager.java:1441)
            at android.view.View.measure(View.java:17430)
            at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5463)
            at android.widget.FrameLayout.onMeasure(FrameLayout.java:430)
            at android.view.View.measure(View.java:17430)
            at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5463)
            at android.support.v7.internal.widget.ActionBarOverlayLayout.onMeasure(ActionBarOverlayLayout.java:453)
            at android.view.View.measure(View.java:17430)
            at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5463)
            at android.widget.FrameLayout.onMeasure(FrameLayout.java:430)
            at android.view.View.measure(View.java:17430)
            at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5463)
            at android.widget.LinearLayout.measureChildBeforeLayout(LinearLayout.java:1436)
            at android.widget.LinearLayout.measureVertical(LinearLayout.java:722)
            at android.widget.LinearLayout.onMeasure(LinearLayout.java:613)
            at android.view.View.measure(View.java:17430)
            at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5463)
            at android.widget.FrameLayout.onMeasure(FrameLayout.java:430)
            at com.android.internal.policy.impl.PhoneWindow$DecorView.onMeasure(PhoneWindow.java:2560)
            at android.view.View.measure(View.java:17430)
            at android.view.ViewRootImpl.performMeasure(ViewRootImpl.java:2001)
            at android.view.ViewRootImpl.measureHierarchy(ViewRootImpl.java:1166)
            at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:1372)
            at android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:1054)
            at android.view.ViewRootImpl$TraversalRunnable.run(ViewRootImpl.java:5779)
            at android.view.Choreographer$CallbackRecord.run(Choreographer.java:767)
            at android.view.Choreographer.doCallbacks(Choreographer.java:580)
            at android.view.Choreographer.doFrame(Choreographer.java:550)
            at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:753)
            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:5221)
            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:899)
            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:694)

片段java类

public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        if (container == null) {
            return null;
        }
        view = (RelativeLayout) inflater.inflate(R.layout.fragmentmenu_layout, container, false);
        // Passing harcoded values for latitude & longitude. Please change as per your need. This is just used to drop a Marker on the Map
        latitude = 26.78;
        longitude = 72.56;

      ; // For setting up the MapFragment

        return view;
    }

    /***** Sets up the map if it is possible to do so *****/
    public static void setUpMapIfNeeded() {
        // Do a null check to confirm that we have not already instantiated the map.
        if (mMap == null) {
            // Try to obtain the map from the SupportMapFragment.
            mMap = ((SupportMapFragment) SectionsPagerAdapter.fragmentManager
                    .findFragmentById(R.id.mapView)).getMap();
            // Check if we were successful in obtaining the map.
            if (mMap != null)
                setUpMap();
        }
    }

    /**
     * This is where we can add markers or lines, add listeners or move the
     * camera.
     * <p>
     * This should only be called once and when we are sure that {@link #mMap}
     * is not null.
     */
    private static void setUpMap() {
        // For showing a move to my loction button
        mMap.setMyLocationEnabled(true);
        // For dropping a marker at a point on the Map
        mMap.addMarker(new MarkerOptions().position(new LatLng(latitude, longitude)).title("My Home").snippet("Home Address"));
        // For zooming automatically to the Dropped PIN Location
        mMap.animateCamera(CameraUpdateFactory.newLatLngZoom(new LatLng(latitude,
                longitude), 12.0f));

    }

    @Override
    public void onViewCreated(View view, Bundle savedInstanceState) {
        // TODO Auto-generated method stub
        if (mMap != null)
            setUpMap();

        if (mMap == null) {
            // Try to obtain the map from the SupportMapFragment.
            mMap = ((SupportMapFragment) MainActivity.fragmentManager
                    .findFragmentById(R.id.mapView)).getMap(); // getMap is deprecated
            // Check if we were successful in obtaining the map.
            if (mMap != null)
                setUpMap();
            getAllFeaturedItems();
            captureMapScreen();
        }
    }

    @Override
    public void onStart() {
        super.onStart();
    }

    @Override
    public void onResume() {
        super.onResume();
        setUpMapIfNeeded();
    }

    @Override
    public void onPause() {
        super.onPause();
    }

    @Override
    public void onStop() {
        super.onStop();
    }

    @Override
    public void onLowMemory() {
        super.onLowMemory();
    }

    /**** The mapfragment's id must be removed from the FragmentManager
     **** or else if the same it is passed on the next time then
     **** app will crash ****/
    @Override
    public void onDestroyView() {
        super.onDestroyView();
        if (mMap != null) {
            MainActivity.fragmentManager.beginTransaction()
                    .remove(MainActivity.fragmentManager.findFragmentById(R.id.mapView)).commit();
            mMap = null;
        }
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
    }

片段菜单布局.xml

 <?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical" android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:translationZ="5dp"
>


    <ListView
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:id="@+id/listViewMenu"
        android:layout_gravity="center_horizontal"
        android:dividerHeight="1sp"
        android:translationZ="5dp"
        android:divider="@color/background_default"
        android:background="@color/background_white"
        android:clickable="true"
        android:layout_alignParentLeft="true"
        android:layout_alignParentStart="true"
        android:layout_below="@+id/screenShotMapView" />

    <ImageView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/screenShotMapView"
        android:layout_alignParentTop="true"
        android:maxWidth="50dp"
        android:layout_alignParentLeft="true"
        android:layout_alignParentStart="true"
        android:layout_alignParentRight="true"
        android:layout_alignParentEnd="true"
        android:adjustViewBounds="true"
        android:translationZ="5dp"
    />



        <fragment
            class="com.google.android.gms.maps.MapFragment"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:id="@+id/mapView"
            android:layout_alignParentTop="true"
            android:layout_alignParentRight="true"
            android:layout_alignParentEnd="true"
            android:layout_alignParentLeft="true"
            android:layout_alignParentStart="true"
            android:adjustViewBounds="true"
            android:longClickable="false"
            android:visibility="visible"
            android:maxHeight="50dp"
            android:maxWidth="50dp"
         />






</RelativeLayout>

MainActivity.java

public static FragmentManager fragmentManager;



    @Override
    protected void onCreate(Bundle savedInstanceState) {
       // setTheme(R.style.ActionBarTheme);
        super.onCreate(savedInstanceState);

        setContentView(R.layout.activity_main);

        // initialising the object of the FragmentManager. Here I'm passing getSupportFragmentManager(). You can pass getFragmentManager() if you are coding for Android 3.0 or above.


    //    final ActionBar actionBar = getSupportActionBar();
        buildGoogleApiClient();

 //       actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);
 //       actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);

        mImageBitmap = null;

        // Create the adapter that will return a fragment for each section/page
        mSectionsPagerAdapter = new SectionsPagerAdapter(
                this.getBaseContext(), getSupportFragmentManager());
        fragmentManager = getSupportFragmentManager();
        // Set up the ViewPager with the sections adapter.
        mViewPager = (ViewPager) findViewById(R.id.pager);

        mViewPager.setAdapter(mSectionsPagerAdapter);

        mViewPager.setOnPageChangeListener(new ViewPager.SimpleOnPageChangeListener() {
            @Override
            public void onPageSelected(int position) {
      //          actionBar.setSelectedNavigationItem(position);
            }
        });

        // For each of the sections in the app, add a tab to the action bar.
        /*
        for (int i = 0; i < mSectionsPagerAdapter.getCount(); i++) {
            // Create a tab with text corresponding to the page title defined by
            // the adapter. Also specify this Activity object, which implements
            // the TabListener interface, as the callback (listener) for when
            // this tab is selected.

                    if (i == 0) {
                        actionBar.addTab(
                                actionBar.newTab()

                                        .setIcon(R.drawable.ic_action_view_as_list)
                                       // .setText(mSectionsPagerAdapter.getPageTitle(i))
                                        .setTabListener(this)
                        );
                    }
                    if (i == 1) {
                        actionBar.addTab(
                                  actionBar.newTab()

                                .setIcon(R.drawable.ic_action_web_site_dark)
                               // .setText(mSectionsPagerAdapter.getPageTitle(i))
                                .setTabListener(this)
                );
            }
        }
        */
        mAlbumStorageDirFactory = new BaseAlbumDirFactory();


        Intent intent = new Intent(this, SplashScreen.class);
        startActivity(intent);

}
4

4 回答 4

25

不太清楚为什么我在这方面被否决了这么多,但是我已经解决了这个问题。

这条线

mMap = ((SupportMapFragment) MainActivity.fragmentManager
                    .findFragmentById(R.id.mapView)).getMap();

需要改为

  mMap = ((SupportMapFragment) getChildFragmentManager()
                .findFragmentById(R.id.mapView)).getMap();
于 2015-04-27T10:19:34.840 回答
6

android:name= 应更改为“com.google.android.gms.maps.SupportMapFragment”,即

<fragment android:name="com.google.android.gms.maps.MapFragment"
 android:layout_width="match_parent"      
 android:layout_height="match_parent"/>

<fragment android:name="com.google.android.gms.maps.SupportMapFragment"
 android:layout_width="match_parent" 
 android:layout_height="match_parent"/>
于 2016-01-12T10:44:27.810 回答
4

问题是您正在尝试从中获取地图,SupportMapFragment但您使用的MapFragment是在 xml 中清楚地看到的不同类型的地图

class="com.google.android.gms.maps.MapFragment"

它应该是

class="com.google.android.gms.maps.SupportMapFragment"

使您能够SupportMapFragment从代码中初始化类型

于 2015-04-26T02:18:02.160 回答
1

我改为getMap()并且getMapAsync(OnMapReadyCallback)它起作用了。

来源: SupportMapFragment

于 2015-09-16T18:22:12.367 回答