0

Im having some trouble accessing Hello Mapview at the moment, http://developer.android.com/training/tutorials/views/hello-mapview.html, but i think ive done this correctly. I want to show a map in a separate activity.

Map.xml:

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

 <com.google.android.maps.MapView
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/mapview"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:clickable="true"
    android:apiKey="working key"
    />

</LinearLayout>

Button onclick event that should show my map

public void showMap(View v){

    Intent intent = new Intent(getBaseContext(), GoogleMapsActivity.class);
    startActivity(intent);
}

GoogleMapsActivity.java

public class GoogleMapsActivity extends MapActivity
{
MapView mapView;

@Override
public void onCreate(Bundle savedInstanceState) {

    super.onCreate(savedInstanceState);
    setContentView(R.layout.map);

    mapView = (MapView) findViewById(R.id.mapview);
    mapView.setBuiltInZoomControls(true);
}

The activity is added to my manifest, Google apis is included etc. Its working if i put the map in my main activity, but not in my GoogleMapsActivity. Please tell me what ive missed here.

Thanks

Hesham Saeed
  • 5,358
  • 7
  • 37
  • 57
Johan
  • 35,120
  • 54
  • 178
  • 293

2 Answers2

0

From https://developers.google.com/maps/documentation/android/reference/:

Only one MapActivity is supported per process. Multiple MapActivities running simultaneously are likely to interfere in unexpected and undesired ways.

Basically, you can only have 1 MapActivity, and MapViews can only be hosted in a MapActivity. What you want basically isn't possible.

EDIT: not sure it would work for you, but you might try marking your second activity with android:process=":remote" and see if that works, but I doubt it.

EDIT: there is evidently some confusion. Look at https://developers.google.com/maps/documentation/android/reference/com/google/android/maps/MapView. This states that:

A MapView can only be constructed (or inflated) by a MapActivity. This is because it depends on threads which access the network and filesystem in the background; these threads must be shepherded by the lifecycle management in MapActivity. Tiles are cached on the filesystem in your application's directory. The cache is auto-managed so you don't need to do anything with it, and can delete it at any time.

I'll re-iterate: You CAN'T display a MapView in any other activity.

Femi
  • 64,273
  • 8
  • 118
  • 148
  • 1
    He just wants to show the map in another activity, he doesn't need two `MapActivity` classes. – Ran Jun 24 '12 at 15:27
  • Thanks, but its just as Ran said. But I've located the problem though, misspelling of my activity in my manifest file :/ – Johan Jun 24 '12 at 15:30
  • Ah: you don't have ANY working MapActivity. I thought you had one working and couldn't get another one working. – Femi Jun 24 '12 at 15:32
  • "You CAN'T display a MapView in any other activity." Are you sure? Because ive just managed to do it in my `GoogleMapsActivity`, which is using a separate xml – Johan Jun 24 '12 at 15:33
  • If you update your answer with a line about the manifest file i can accept your answer :) – Johan Jun 24 '12 at 15:34
  • I could, but I didn't actually answer it :). You should answer it, then wait the day or 2 and then accept your own answer. I really can't lay claim to getting it right. – Femi Jun 24 '12 at 15:44
0

The problem was a misspelling in my manifest file,

<activity android:name=".GoogleMapActivity" /> //missing an "s"
Johan
  • 35,120
  • 54
  • 178
  • 293