Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Google Maps API v2 not working

I am trying to build an application using Google Maps API v2 but the thing is, the application keeps giving me force closes although i followed all the required instructions.

Here is the android part

import android.support.v4.app.FragmentActivity;
import com.google.android.maps.GeoPoint;
import com.google.android.maps.MapController;
import com.google.android.maps.MapView;

public class GPSonMap extends FragmentActivity{

 MapView map;
 MapController controller;
 LocationManager locationManager;
 LocationListener listener;
 PendingIntent pendingIntent ;
 Criteria criteria;
 MapOverlay overlay;


@Override
public void onCreate(Bundle onsaved)
{
    super.onCreate(onsaved);
    setContentView(R.layout.map);
    Log.d("", "entered");


    map = (MapView)findViewById(R.id.mapView);
    }

and here is the XML

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MapsActivity" >

<fragment

    class="com.google.android.gms.maps.SupportMapFragment"
    android:id="@+id/mapView"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
/>

</RelativeLayout>

and this is my Manifest

        <?xml version="1.0" encoding="utf-8"?>
    <manifest xmlns:android="http://schemas.android.com/apk/res/android"
        package="com.bachelor.bond"
        android:versionCode="1"
        android:versionName="1.0" >

        <uses-sdk
            android:minSdkVersion="14"
            android:targetSdkVersion="17" />
      <!--   <permission
              android:name="com.example.example.permission.MAPS_RECEIVE"
              android:protectionLevel="signature"/>
            <uses-permission android:name="com.bachelor.bond.permission.MAPS_RECEIVE"/>. -->

           <uses-permission android:name="android.permission.INTERNET" />
         <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/>
        <permission android:name="com.example.example.permission.C2D_MESSAGE" android:protectionLevel="signature" />
    <uses-permission android:name="com.example.example.permission.C2D_MESSAGE" /> 

    <!-- App receives GCM messages. -->
    <uses-permission android:name="com.google.android.c2dm.permission.RECEIVE" />
    <!-- GCM connects to Google Services. -->
    <uses-permission android:name="android.permission.INTERNET" />
    <uses-permission android:name="com.google.android.providers.gsf.permission.READ_GSERVICES"/>
        <uses-permission android:name="android.permission.READ_PHONE_STATE"/> 
    <!-- GCM requires a Google account. -->
    <uses-permission android:name="android.permission.GET_ACCOUNTS" />
    <!-- Keeps the processor from sleeping when a message is received. -->
    <uses-permission android:name="android.permission.WAKE_LOCK"
         />
    <uses-permission android:name="com.example.example.permission.MAPS_RECEIVE"/>
    <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
    <uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
      <uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />  
    <!-- Permission to vibrate -->
        <uses-permission android:name="android.permission.VIBRATE"/>
        <uses-permission android:name = "android.permission.ACCESS_FINE_LOCATION"/>

        <permission 
        android:name="com.example.example.permission.MAPS_RECEIVE" 
        android:protectionLevel="signature"></permission>

        <uses-feature 
        android:glEsVersion="0x00020000" 
        android:required="true"/>
        <application
            android:allowBackup="true"
            android:icon="@drawable/ic_launcher"
            android:label="@string/app_name">
            <uses-library android:name="com.google.android.maps" />
             <activity
                android:name=".GPSonMap"
                android:label="@string/app_name"
                 > <intent-filter>
                    <action android:name="android.intent.action.MAIN" />

                    <category android:name="android.intent.category.LAUNCHER" />
                </intent-filter>

            </activity>

             <uses-library android:name="com.google.android.maps" />
             <intent-filter>
        <action android:name="com.google.android.c2dm.intent.RECEIVE" />
        <action android:name="com.google.android.c2dm.intent.REGISTRATION" />
        <category android:name="com.example.example" />
      </intent-filter>
              <receiver android:name="com.google.android.gcm.GCMBroadcastReceiver" 
                 android:permission="com.google.android.c2dm.permission.SEND" >

    </receiver>
     <service android:name=".GCMIntentService" />
     <activity android:name="Login"></activity>

     <meta-data
        android:name="com.google.android.maps.v2.API_KEY"
        android:value="****"/>

        </application>

    </manifest>

And the Log Cat output

02-15 00:42:35.257: E/AndroidRuntime(2693): FATAL EXCEPTION: main
02-15 00:42:35.257: E/AndroidRuntime(2693): java.lang.RuntimeException: Unable to start         activity ComponentInfo{com.bachelor.bond/com.dxample.example.GPSonMap}:     android.view.InflateException: Binary XML file line #7: Error inflating class fragment
02-15 00:42:35.257: E/AndroidRuntime(2693):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2180)
02-15 00:42:35.257: E/AndroidRuntime(2693):     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2230)
02-15 00:42:35.257: E/AndroidRuntime(2693):     at android.app.ActivityThread.access$600(ActivityThread.java:141)
02-15 00:42:35.257: E/AndroidRuntime(2693):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1234)
02-15 00:42:35.257: E/AndroidRuntime(2693):     at android.os.Handler.dispatchMessage(Handler.java:99)
02-15 00:42:35.257: E/AndroidRuntime(2693):     at android.os.Looper.loop(Looper.java:137)
02-15 00:42:35.257: E/AndroidRuntime(2693):     at android.app.ActivityThread.main(ActivityThread.java:5039)
02-15 00:42:35.257: E/AndroidRuntime(2693):     at java.lang.reflect.Method.invokeNative(Native Method)
02-15 00:42:35.257: E/AndroidRuntime(2693):     at java.lang.reflect.Method.invoke(Method.java:511)
02-15 00:42:35.257: E/AndroidRuntime(2693):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
02-15 00:42:35.257: E/AndroidRuntime(2693):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)
02-15 00:42:35.257: E/AndroidRuntime(2693):     at dalvik.system.NativeStart.main(Native Method)
02-15 00:42:35.257: E/AndroidRuntime(2693): Caused by: android.view.InflateException: Binary XML file line #7: Error inflating class fragment
02-15 00:42:35.257: E/AndroidRuntime(2693):     at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:704)
02-15 00:42:35.257: E/AndroidRuntime(2693):     at android.view.LayoutInflater.rInflate(LayoutInflater.java:746)
02-15 00:42:35.257: E/AndroidRuntime(2693):     at android.view.LayoutInflater.inflate(LayoutInflater.java:489)
02-15 00:42:35.257: E/AndroidRuntime(2693):     at android.view.LayoutInflater.inflate(LayoutInflater.java:396)
02-15 00:42:35.257: E/AndroidRuntime(2693):     at android.view.LayoutInflater.inflate(LayoutInflater.java:352)
02-15 00:42:35.257: E/AndroidRuntime(2693):     at com.android.internal.policy.impl.PhoneWindow.setContentView(PhoneWindow.java:270)
02-15 00:42:35.257: E/AndroidRuntime(2693):     at android.app.Activity.setContentView(Activity.java:1881)
02-15 00:42:35.257: E/AndroidRuntime(2693):     at com.bachelor.bond.GPSonMap.onCreate(GPSonMap.java:54)
02-15 00:42:35.257: E/AndroidRuntime(2693):     at android.app.Activity.performCreate(Activity.java:5104)
02-15 00:42:35.257: E/AndroidRuntime(2693):     at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1080)
02-15 00:42:35.257: E/AndroidRuntime(2693):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2144)
02-15 00:42:35.257: E/AndroidRuntime(2693):     ... 11 more
02-15 00:42:35.257: E/AndroidRuntime(2693): Caused by:         android.support.v4.app.Fragment$InstantiationException: Unable to instantiate fragment     com.google.android.gms.maps.SupportMapFragment: make sure class name exists, is public, and     has an empty constructor that is public
02-15 00:42:35.257: E/AndroidRuntime(2693):     at android.support.v4.app.Fragment.instantiate(Fragment.java:401)
02-15 00:42:35.257: E/AndroidRuntime(2693):     at android.support.v4.app.Fragment.instantiate(Fragment.java:369)
02-15 00:42:35.257: E/AndroidRuntime(2693):     at android.support.v4.app.FragmentActivity.onCreateView(FragmentActivity.java:272)
02-15 00:42:35.257: E/AndroidRuntime(2693):     at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:676)
02-15 00:42:35.257: E/AndroidRuntime(2693):     ... 21 more
02-15 00:42:35.257: E/AndroidRuntime(2693): Caused by: java.lang.ClassNotFoundException:     Didn't find class "com.google.android.gms.maps.SupportMapFragment" on path:     /system/framework/com.google.android.maps.jar:/data/app/com.bachelor.bond-1.apk
02-15 00:42:35.257: E/AndroidRuntime(2693):     at dalvik.system.BaseDexClassLoader.findClass(BaseDexClassLoader.java:65)
02-15 00:42:35.257: E/AndroidRuntime(2693):     at java.lang.ClassLoader.loadClass(ClassLoader.java:501)
02-15 00:42:35.257: E/AndroidRuntime(2693):     at java.lang.ClassLoader.loadClass(ClassLoader.java:461)
02-15 00:42:35.257: E/AndroidRuntime(2693):     at android.support.v4.app.Fragment.instantiate(Fragment.java:391)
02-15 00:42:35.257: E/AndroidRuntime(2693):     ... 24 more

One other problem I face, is that when i try to
import com.google.android.gms.maps.MapFragment; it gives an error that it doesnt recognise a package name to be com.google.android.gms so i cant even use the GoogleMapor MapFragment for example.

So does anyone have any solution to those 2 problems?

like image 498
OmAr Hesham Avatar asked Feb 15 '13 00:02

OmAr Hesham


People also ask

Why is my Google map API not working?

There are a several reasons why your google maps may not be working, the most common issue being no Google Map API key set or set incorrectly. To use the Google Maps JavaScript API, you must register your app project on the Google Cloud Platform Console and get a Google API key which you can add to your app.

How do I enable Google Maps API v2?

Click on the “APIs & auth” menu on the left, and from the submenu select APIs. From the list of APIs that appear, scroll down and ensure that Google Maps Android API v2 is set to “On”.

Why is my API key not working?

API keys have multiple dependencies that can result in errors like: "The user is not authorized for this operation based on …". Please make sure you are using the correct credentials and endpoint with the correct API key.

Is Google Maps API no longer free?

You won't be charged until your usage exceeds $200 in a month. Note that the Maps Embed API, Maps SDK for Android, and Maps SDK for iOS currently have no usage limits and are at no charge (usage of the API or SDKs is not applied against your $200 monthly credit).


2 Answers

I found you missed several things in your code.

Answer 1: Your GPSonMAP.java must be modified like below;

public class GPSonMap extends android.support.v4.app.FragmentActivity{
 GoogleMap googlemap;  // "MapView map;" is wrong
 //MapController controller;  ==> it was depreciated.
 LocationManager locationManager;
 LocationListener listener;
 PendingIntent pendingIntent ;
 Criteria criteria;
 //MapOverlay overlay; ==> it was depreciated.
 SupportMapFragment fm; // add this


@Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.map);
        Log.d("", "entered");


    //map = (MapView)findViewById(R.id.mapView); ==> delete it.

     fm = (SupportMapFragment) getSupportFragmentManager().findFragmentById(R.id.mapView);
     googleMap = fm.getMap();
    }


}

After modifying the above, press "Ctrl + Shft + (alphabet) O " and some required classes will be imported.

Answer 2: Your xml file in layout must be modified like below;

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".GPSonMap" >

Your MainActivity is not MapsActivity, but GPSonMap.

Answer 3: Your Manifest file does not match your package name with the described package name in the manifest file. Please match the package name with the same one.

Modify "com.example.example" to your package name.

After copying and pasting some codes, you MUST check the package name and class name at least.

like image 119
BBonDoo Avatar answered Nov 08 '22 03:11

BBonDoo


Presumably, you have not added a reference to the Google Play Services library project to your app project. See the Google Play Services SDK documentation, specifically the section entitled "To set up a project to use the Google Play services SDK".

like image 22
CommonsWare Avatar answered Nov 08 '22 01:11

CommonsWare