2

Hello every one i am new in android and i trying to connect Google map V2 with my android application. For that i follow following this tutorial and this one also but when every i try to lunch my application is shows following error.

enter image description here

my MainActivity.java

package com.example.map;


import android.annotation.SuppressLint;
import android.annotation.TargetApi;
import android.app.Activity;
import android.app.FragmentManager;
import android.os.Build;
import android.os.Bundle;
import android.view.Menu;

import com.google.android.gms.maps.CameraUpdateFactory;
import com.google.android.gms.maps.GoogleMap;
import com.google.android.gms.maps.MapFragment;
import com.google.android.gms.maps.model.BitmapDescriptorFactory;
import com.google.android.gms.maps.model.LatLng;
import com.google.android.gms.maps.model.Marker;
import com.google.android.gms.maps.model.MarkerOptions;
import com.google.android.gms.maps.SupportMapFragment;

@SuppressLint("NewApi")
public class MainActivity extends Activity {
  static final LatLng HAMBURG = new LatLng(53.558, 9.927);
  static final LatLng KIEL = new LatLng(53.551, 9.993);
  private GoogleMap map;

  @TargetApi(Build.VERSION_CODES.HONEYCOMB)
@Override
  protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    FragmentManager fragmentManager = getFragmentManager();
    MapFragment mapFragment =  (MapFragment) fragmentManager.findFragmentById(R.id.map);
    map = mapFragment.getMap();
    //map = ((SupportMapFragment) getSupportFragmentManager().findFragmentById(R.id.map)).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);
  }

  @Override
  public boolean onCreateOptionsMenu(Menu menu) {
    getMenuInflater().inflate(R.menu.main, menu);
    return true;
  }

}

my activity_main.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=".MainActivity" >


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

</RelativeLayout>

my AndroidManifest.xml

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

    <uses-sdk
        android:minSdkVersion="8"
        android:targetSdkVersion="17" />

     <permission
        android:name="in.wptrafficanalyzer.locationingooglemapv2.permission.MAPS_RECEIVE"
        android:protectionLevel="signature"/>

    <uses-feature
        android:glEsVersion="0x00020000"
        android:required="true" />

    <uses-permission android:name="in.wptrafficanalyzer.locationingooglemapv2.permission.MAPS_RECEIVE"/>
    <uses-permission android:name="android.permission.INTERNET"/>
    <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
    <uses-permission android:name="com.google.android.providers.gsf.permission.READ_GSERVICES"/>
    <uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION"/>
    <uses-permission android:name="android.permission.ACCESS_FINE_LOCATION"/>

    <application
        android:allowBackup="true"
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/AppTheme" >


        <activity
            android:name="com.example.map.MainActivity"
            android:label="@string/app_name" >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
        <meta-data
            android:name="com.google.android.maps.v2.API_KEY"
            android:value="the key" />
    </application>

</manifest>

my locgcat

09-05 08:09:55.435: E/AndroidRuntime(490): FATAL EXCEPTION: main
09-05 08:09:55.435: E/AndroidRuntime(490): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.map/com.example.map.MainActivity}: android.view.InflateException: Binary XML file line #8: Error inflating class fragment
09-05 08:09:55.435: E/AndroidRuntime(490):  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2663)
09-05 08:09:55.435: E/AndroidRuntime(490):  at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2679)
09-05 08:09:55.435: E/AndroidRuntime(490):  at android.app.ActivityThread.access$2300(ActivityThread.java:125)
09-05 08:09:55.435: E/AndroidRuntime(490):  at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2033)
09-05 08:09:55.435: E/AndroidRuntime(490):  at android.os.Handler.dispatchMessage(Handler.java:99)
09-05 08:09:55.435: E/AndroidRuntime(490):  at android.os.Looper.loop(Looper.java:123)
09-05 08:09:55.435: E/AndroidRuntime(490):  at android.app.ActivityThread.main(ActivityThread.java:4627)
09-05 08:09:55.435: E/AndroidRuntime(490):  at java.lang.reflect.Method.invokeNative(Native Method)
09-05 08:09:55.435: E/AndroidRuntime(490):  at java.lang.reflect.Method.invoke(Method.java:521)
09-05 08:09:55.435: E/AndroidRuntime(490):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868)
09-05 08:09:55.435: E/AndroidRuntime(490):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626)
09-05 08:09:55.435: E/AndroidRuntime(490):  at dalvik.system.NativeStart.main(Native Method)
09-05 08:09:55.435: E/AndroidRuntime(490): Caused by: android.view.InflateException: Binary XML file line #8: Error inflating class fragment
09-05 08:09:55.435: E/AndroidRuntime(490):  at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:576)
09-05 08:09:55.435: E/AndroidRuntime(490):  at android.view.LayoutInflater.rInflate(LayoutInflater.java:618)
09-05 08:09:55.435: E/AndroidRuntime(490):  at android.view.LayoutInflater.inflate(LayoutInflater.java:407)
09-05 08:09:55.435: E/AndroidRuntime(490):  at android.view.LayoutInflater.inflate(LayoutInflater.java:320)
09-05 08:09:55.435: E/AndroidRuntime(490):  at android.view.LayoutInflater.inflate(LayoutInflater.java:276)
09-05 08:09:55.435: E/AndroidRuntime(490):  at com.android.internal.policy.impl.PhoneWindow.setContentView(PhoneWindow.java:198)
09-05 08:09:55.435: E/AndroidRuntime(490):  at android.app.Activity.setContentView(Activity.java:1647)
09-05 08:09:55.435: E/AndroidRuntime(490):  at com.example.map.MainActivity.onCreate(MainActivity.java:31)
09-05 08:09:55.435: E/AndroidRuntime(490):  at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
09-05 08:09:55.435: E/AndroidRuntime(490):  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2627)
09-05 08:09:55.435: E/AndroidRuntime(490):  ... 11 more
09-05 08:09:55.435: E/AndroidRuntime(490): Caused by: java.lang.ClassNotFoundException: android.view.fragment in loader dalvik.system.PathClassLoader[/data/app/com.example.map-2.apk]
09-05 08:09:55.435: E/AndroidRuntime(490):  at dalvik.system.PathClassLoader.findClass(PathClassLoader.java:243)
09-05 08:09:55.435: E/AndroidRuntime(490):  at java.lang.ClassLoader.loadClass(ClassLoader.java:573)
09-05 08:09:55.435: E/AndroidRuntime(490):  at java.lang.ClassLoader.loadClass(ClassLoader.java:532)
09-05 08:09:55.435: E/AndroidRuntime(490):  at android.view.LayoutInflater.createView(LayoutInflater.java:466)
09-05 08:09:55.435: E/AndroidRuntime(490):  at android.view.LayoutInflater.onCreateView(LayoutInflater.java:544)
09-05 08:09:55.435: E/AndroidRuntime(490):  at com.android.internal.policy.impl.PhoneLayoutInflater.onCreateView(PhoneLayoutInflater.java:66)
09-05 08:09:55.435: E/AndroidRuntime(490):  at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:563)
09-05 08:09:55.435: E/AndroidRuntime(490):  ... 20 more

what is wrong in my code ??

Simmant
  • 1,477
  • 25
  • 39
  • This is answered here: http://stackoverflow.com/questions/14122705/error-while-inflating-xml-in-google-maps-fragment – RedPixel Sep 05 '13 at 08:28
  • Map v2 will not display in emulator try on device because it requires Google play services installed. – Biraj Zalavadia Sep 05 '13 at 08:32
  • `Google map V2 with android is not working?` a quick answer to your question would be 'uh, yes it does'. – njzk2 Sep 05 '13 at 08:33
  • Refer this one http://wptrafficanalyzer.in/blog/showing-current-location-using-onmylocationchangelistener-in-google-map-android-api-v2/ – Aravin Sep 05 '13 at 08:40

5 Answers5

5

Your min sdk is 8

android:minSdkVersion="8"

Your class must extend FragmentActivty

Use SupportMapFragment instead of MapFragment

SupportMapFragment fm = (SupportMapFragment)  getSupportFragmentManager().findFragmentById(R.id.map);
GoogleMap mMap = fm.getMap(); 

Also import

import android.support.v4.app.FragmentActivity;  
import com.google.android.gms.maps.SupportMapFragment;

Edit:

your package name is

package="com.example.map"

So change it to

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

and

 <uses-permission android:name="com.example.map.permission.MAPS_RECEIVE"/>
Raghunandan
  • 132,755
  • 26
  • 225
  • 256
  • Thank you so much sir for answer my question sir . I apply following changes in my code and now the previous error is been solve but now i am getting null pointer exception. i dont why is there is also problem with my LatLng sir. – Simmant Sep 05 '13 at 08:37
  • @simmant post the stacktrace and post the line that causes NPE> – Raghunandan Sep 05 '13 at 08:38
  • Mind reading hat on: `map = mapFragment.getMap()` is getting called before the fragment has been laid out, so is null. [This answer](http://stackoverflow.com/a/14047363/153225) might help you. – Martin Sep 05 '13 at 08:59
  • @Raghunandan sir this line of code line 35 Marker hamburg = map.addMarker(new MarkerOptions().position(HAMBURG) .title("Hamburg")); – Simmant Sep 05 '13 at 09:13
  • @simmant looks like your map object is null. check it with a simple if condition and log some info. as marting suggested check the link provided by martin – Raghunandan Sep 05 '13 at 09:16
1

use a device to run your app instead emulator

swati srivastav
  • 635
  • 4
  • 15
  • thank you so much mam this code working fine on device realy mam thanks a lot . now realy happy – Simmant Sep 05 '13 at 09:43
  • mam but question is why this code not working on the emulator ?? – Simmant Sep 05 '13 at 09:44
  • the reason is with the version upgradation only the device is definately getting upgrade but emulators still supprt google map v1 only. But yes better if we ask this question to Google Developers as they have noty given this facility – swati srivastav Sep 05 '13 at 09:47
1
public class MainActivity extends FragmentActivity 
{
  static final LatLng HAMBURG = new LatLng(53.558, 9.927);
  static final LatLng KIEL = new LatLng(53.551, 9.993);
  private GoogleMap map;


  @Override
  protected void onCreate(Bundle savedInstanceState) 
  {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    android.support.v4.app.FragmentManager fragmentManager = getSupportFragmentManager();
    SupportMapFragment mapFragment = (SupportMapFragment) fragmentManager.findFragmentById(R.id.map);
    map = mapFragment.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);
}

}

change the lines in you manifest file

<permission
    android:name="in.wptrafficanalyzer.locationingooglemapv2.permission.MAPS_RECEIVE"
    android:protectionLevel="signature"/>


<uses-permission android:name="in.wptrafficanalyzer.locationingooglemapv2.permission.MAPS_RECEIVE"/>

instead of marked two line use

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


<uses-permission android:name="com.example.map.permission.MAPS_RECEIVE"/>
swati srivastav
  • 635
  • 4
  • 15
  • thank you so much mam for your answer ialso change above thing in my code but still getting the same error mam. – Simmant Sep 05 '13 at 09:31
  • u running it in emulator? – swati srivastav Sep 05 '13 at 09:33
  • yaa mam i am running it in emulator and i also installed play services in that . – Simmant Sep 05 '13 at 09:36
  • simmant , you can not test it in emulator as emulator does not support google map v2. – swati srivastav Sep 05 '13 at 09:41
  • http://stackoverflow.com/questions/14040185/running-google-maps-v2-on-android-emulator – swati srivastav Sep 05 '13 at 09:42
  • use any device honeycomb or above to test ur app – swati srivastav Sep 05 '13 at 09:43
  • @swatisrivastav you can test it on emulator. Quoting from docs if you want to test your app on the emulator, expand the directory for Android 4.2.2 (API 17) or a higher version, select Google APIs, and install it. Then create a new AVD with Google APIs as the platform target. Note: Only Android 4.2.2 and higher versions of the Google APIs platform include Google Play services. There is a workaround for previous versions also. But is better to test it on a device also check the availability of google play services before initializing map object – Raghunandan Sep 05 '13 at 10:02
  • @Raghunandan , tried in this way http://nemanjakovacevic.net/blog/english/2012/12/06/how-to-make-android-google-maps-v2-work-in-android-emulator/ but still not get the soution – swati srivastav Sep 05 '13 at 10:14
1

Try this ,i hope this will help you

package com.example.maptest;

import android.app.Activity;
import android.os.Bundle;
import android.view.Menu;

import com.google.android.gms.maps.CameraUpdateFactory;
import com.google.android.gms.maps.GoogleMap;
import com.google.android.gms.maps.MapFragment;
import com.google.android.gms.maps.model.BitmapDescriptorFactory;
import com.google.android.gms.maps.model.LatLng;
import com.google.android.gms.maps.model.Marker;
import com.google.android.gms.maps.model.MarkerOptions;

public class MainActivity extends Activity {
    static final LatLng HAMBURG = new LatLng(53.558, 9.927);
      static final LatLng KIEL = new LatLng(53.551, 9.993);
      private GoogleMap map;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
         map = ((MapFragment) getFragmentManager().findFragmentById(R.id.fragment1))
                    .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);
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.main, menu);
        return true;
    }

}
Ansar
  • 364
  • 3
  • 16
0
// xml
<fragment
    android:id="@+id/maps"
    android:name="pl.mg6.android.maps.extensions.SupportMapFragment"
    android:layout_width="match_parent"
    android:layout_height="0dp"
    android:layout_weight="1" />

// Activity
    FragmentManager fm = getSupportFragmentManager();
    SupportMapFragment f = (SupportMapFragment) fm.findFragmentById(R.id.maps);

    map = f.getExtendedMap();
Haresh Chhelana
  • 24,720
  • 5
  • 57
  • 67
  • A bit more context beyond some code snippets would be nice. I don't think OP *needs* a 3rd party map extension to solve their problem. – Martin Sep 05 '13 at 08:55