0

I use Eclipse ,Android SDK and i have a class to do not put every code in main activity :

blablabla
import blablabla
..
..
public class Mhelper {
    static final LatLng HAMBURG = new LatLng(53.558, 9.927);
      static final LatLng KIEL = new LatLng(53.551, 9.993);
      private GoogleMap map;
      Context mContext;

    public Mhelper(Context ctx) {
          this.mContext= ctx;
    }

    public void pinpointlocation(Context context){

         map = ((SupportMapFragment) ((FragmentActivity) mContext).getSupportFragmentManager().findFragmentById(R.id.mapview))
                .getMap();
                Marker hamburg = map.addMarker(new MarkerOptions().position(HAMBURG)
                    .title("Hamburg"));
                Marker kiel = map.addMarker(new MarkerOptions()
                    .position(KIEL)
                    .title("Kiel")
                    .snippet("Kiel is cool")
                    .icon(BitmapDescriptorFactory
                        .fromResource(R.drawable.ic_launcher)));

                // Move the camera instantly to hamburg with a zoom of 15.
                map.moveCamera(CameraUpdateFactory.newLatLngZoom(HAMBURG, 15));

                // Zoom in, animating the camera.
                map.animateCamera(CameraUpdateFactory.zoomTo(10), 2000, null);
    }
....
....
....

and i put a viewmap in xml :

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"

...
...
...
...


<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="KEY"
/>

<fragment
    android:id="@+id/map"
    android:name="com.google.android.gms.maps.MapFragment"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:layout_below="@+id/button2" />

</RelativeLayout>

And the program compile and install without any error but when i run app in emulator i get:

blablabla
04-23 11:36:32.408: E/AndroidRuntime(2515): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.hach/com.example.hach.MainActivity}: android.view.InflateException: Binary XML file line #48: Error inflating class com.google.android.maps.MapView
blablabla
04-23 11:36:32.408: E/AndroidRuntime(2515): Caused by: android.view.InflateException: Binary XML file line #48: Error inflating class com.google.android.maps.MapView
blablabla
04-23 11:36:32.408: E/AndroidRuntime(2515): Caused by: java.lang.IllegalArgumentException: MapViews can only be created inside instances of MapActivity.

And i search in stack and find this ,this and this.

First one just confess he shouldn't use viewmap and second one told to read this refrence which is good one but i read it and can't find any solution which i can apply in my project.third one said Google Maps Android API v2 is good one.

And i found this link which is good example but i don't want to use tabs,should i? besides the line

import com.google.android.maps.MapActivity;

is the problem when i create new class and i want to extend MapActivity,the IDE don't know MapActivity.

Is there any thing that i can do to my current project without any complete change of app, to show the map and point a location as i code? should i extend MapActivity and add it to the project?Is there any simple solution like mapview?

Community
  • 1
  • 1
Shombol-shagol
  • 83
  • 1
  • 10
  • well like the error says you cannot do that. why not just create the map object in your activity then pass it to your helper class – tyczj Apr 23 '14 at 20:22
  • do not use `com.google.android.maps.MapActivity` that is the deprecated map activity from v1 – tyczj Apr 23 '14 at 20:29
  • Yea,i did that , but same error but this time from mainactivity. – Shombol-shagol Apr 23 '14 at 20:38
  • @tyczj i deleted **com.google.android.maps.MapActivity**,just testing. – Shombol-shagol Apr 23 '14 at 20:39
  • 1
    You appear to be mixing and matching Maps V1 and Maps V2, which will not work. Since Maps V1 is no longer available to new apps, focus on [Maps V2 and its documentation](https://developers.google.com/maps/documentation/android/). – CommonsWare Apr 23 '14 at 21:10
  • @CommonsWare I read that documents,which part of my app is V1 and which is V2. – Shombol-shagol Apr 24 '14 at 06:30

1 Answers1

0

I figer it out.I first delete map view (CommonsWare was right it was in V1) then i add

<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/>
<uses-permission android:name="com.google.android.providers.gsf.permission.READ_GSERVICES"/>

to manifest.Then it works.This problem caused by some unqualified web site which try to put a tutorials without testing them.

Shombol-shagol
  • 83
  • 1
  • 10