Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8935071
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T09:54:52+00:00 2026-06-15T09:54:52+00:00

am using the android support v4- google maps by pete doyle and am trying

  • 0

am using the android support v4- google maps by pete doyle and am trying to delete a mapview and then recreate it again, when i go away from and return to it, but i keep getting the usual Java.lang.IllegalStateException: You are only allowed to have a single MapView in a MapActivity. anyone know ho to solve this correctly. I have this code in my fragment.

Fragment:

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
    super.onCreateView(inflater, container, savedInstanceState);
    Log.d(TAG, "onCreate View called here in location fragment!!");

    if(view == null){
      view = inflater.inflate(R.layout.map, container, false);  

    frame = (FrameLayout)view.findViewById(R.id.mapContainer);
    //mapview = (MapView)view.findViewById(R.id.mapView);
    }

    if(mapview == null){

        mapview = new MapView(getActivity(), getActivity().getString(R.string.map_api_key));  // keep getting the error on this line
        mapview_is_active = true;
    }

    mapview.setClickable(true);
    mapview.setBuiltInZoomControls(true);
    mapview.setSatellite(true); // Satellite View
    mapview.setStreetView(true); // Street View
    mapview.setTraffic(true); // Traffic View

    frame.addView(mapview,0);



    //frame = (FrameLayout)view.findViewById(R.id.mapContainer);   
   return view;
}


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

       if(mapview == null){
           Log.d(TAG, "mapview is null here");
           mapview = new MapView(getActivity(), getActivity().getString(R.string.map_api_key));
       }

       if(frame.getChildAt(0) == null){
           Log.d(TAG, "mapview is null here in container");
           frame.addView(mapview);
       }

        MapController controller = mapview.getController();

        double lat = Double.parseDouble(longitude);
        double lon = Double.parseDouble(latitude);

        GeoPoint geopoint = new GeoPoint((int)(lat * 1E6), (int)(lon * 1E6));

        mapview.invalidate();

        List<Overlay> mapOverlays = mapview.getOverlays();
        Drawable drawable = this.getResources().getDrawable(R.drawable.map_point);
        AddMapOverlay add_map_overlay = new AddMapOverlay(drawable, getActivity());

        OverlayItem overlayitem = new OverlayItem(geopoint, name, address);

        add_map_overlay.addOverlay(overlayitem);

        mapOverlays.add(add_map_overlay);

        controller.animateTo(geopoint);
        controller.setZoom(15);
    }

and in my onStop():

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

        if(frame != null){
            frame.removeView(mapview); // i remove the mapview here
        }   
    }

here is my map.xml:

<FrameLayout  xmlns:android="http://schemas.android.com/apk/res/android"
   android:id="@+id/mapContainer"
   android:layout_width="fill_parent"
   android:layout_height="fill_parent"
   android:layout_weight="1">

</FrameLayout>

any help will be greatly appreciated. Thanks

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-15T09:54:54+00:00Added an answer on June 15, 2026 at 9:54 am

    I did this with the below code. I have a MapView instance variable in my FragmentActivity, and then added and removed it in the Fragment itself below (just included the pertinent parts):

    @Override
    public void onActivityCreated(Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
    
        mLayoutMap = (FrameLayout) getActivity().findViewById(R.id.layoutMap);
    
        // create the map view if it's not already there
        if (((MyActivity)getActivity()).mMapView == null)
        {
            ((MyActivity)getActivity()).mMapView = 
                        new MapView(getActivity(), getActivity().getString(R.string.map_api_key));
            ((MyActivity)getActivity()).mMapView.setClickable(true);
        }
    
        // add the map view to the frame layout, at the lowest z-index
        mLayoutMap.addView(((MyActivity)getActivity()).mMapView, 0);
    
    }
    
    @Override
     public void onStop()
     {
         super.onStop();
    
         if (mLayoutMap.getChildCount() > 0 && mLayoutMap.getChildAt(0) instanceof MapView)
             mLayoutMap.removeViewAt(0);
     }
    

    mLayoutMap is just a FrameLayout in my XML.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm using Google API 8 (Android 2.2) with support package v4. It doesn't give
I recently downloaded the Android Support Package and have been using it trying to
I'm using Android Support Library (v4) and ActionBarSherlock. I'm trying to close a progress
Seems the Google-recommended way (using the android support library) for a fragment or activity
Is there any ways to test free Android applications (available from Google Play) using
I'm using Android-support-v4 I have a PagerAdapter that displays a list fragment in each
I am using Android's support.v4 package to develop a ViewPager containing multiple Fragment s.
I have created following thing in android using android compatibility support package Basically i
Using this page, http://developer.android.com/sdk/compatibility-library.html , I have installed the Android Support Package, added a
Using Android opengl I need to move an object from point A to point

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.