0

I am trying to implement google maps. But it always gets error.

My Activity

package com.alex.googlemaps;

import android.os.Build;
import android.os.Bundle;
import com.google.android.gms.maps.GoogleMap;
import com.google.android.gms.maps.MapFragment;
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 android.annotation.TargetApi;
import android.app.Activity;
import android.view.Menu;

@TargetApi(Build.VERSION_CODES.HONEYCOMB)
public class MainActivity extends Activity {

    private GoogleMap mMap;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        mMap = ((MapFragment) getFragmentManager().findFragmentById(R.id.map)).getMap();
        mMap.setMapType(GoogleMap.MAP_TYPE_SATELLITE);
        final LatLng CIU = new LatLng(35.21843892856462, 33.41662287712097);
        Marker ciu = mMap.addMarker(new MarkerOptions()
                                  .position(CIU).title("My Office"));
    }

    @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;
    }

}

My XML file

<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"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context=".MainActivity" >



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

</RelativeLayout>

Manifest File

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

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

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

    <uses-permission android:name="com.alex.googlemaps.permission.MAPS_RECEIVE" />
    <uses-permission android:name="com.google.android.providers.gsf.permission.READ_GSERVICES" />
    <uses-permission android:name="android.permission.INTERNET" />
    <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" />

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

    <application
        android:allowBackup="true"
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/AppTheme" >
        <activity
            android:name="com.alex.googlemaps.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="AIzaSyD-zPbxq6kH0cjw-Axfey-WZ3cmlXqf2LE" />
    </application>

</manifest>

And the logCat shows as

04-05 05:44:02.598: E/Trace(1057): error opening trace file: No such file or directory (2)
04-05 05:44:03.038: W/GooglePlayServicesUtil(1057): Google Play services out of date.  Requires 3025100 but found 2012110
04-05 05:44:03.058: W/GooglePlayServicesUtil(1057): Google Play services out of date.  Requires 3025100 but found 2012110
04-05 05:44:03.148: W/GooglePlayServicesUtil(1057): Google Play services out of date.  Requires 3025100 but found 2012110
04-05 05:44:03.188: W/GooglePlayServicesUtil(1057): Google Play services out of date.  Requires 3025100 but found 2012110
04-05 05:44:03.198: W/GooglePlayServicesUtil(1057): Google Play services out of date.  Requires 3025100 but found 2012110
04-05 05:44:03.258: W/GooglePlayServicesUtil(1057): Google Play services out of date.  Requires 3025100 but found 2012110
04-05 05:44:03.258: D/AndroidRuntime(1057): Shutting down VM
04-05 05:44:03.268: W/dalvikvm(1057): threadid=1: thread exiting with uncaught exception (group=0x40a71930)
04-05 05:44:03.278: E/AndroidRuntime(1057): FATAL EXCEPTION: main
04-05 05:44:03.278: E/AndroidRuntime(1057): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.alex.googlemaps/com.alex.googlemaps.MainActivity}: java.lang.NullPointerException
04-05 05:44:03.278: E/AndroidRuntime(1057):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2180)
04-05 05:44:03.278: E/AndroidRuntime(1057):     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2230)
04-05 05:44:03.278: E/AndroidRuntime(1057):     at android.app.ActivityThread.access$600(ActivityThread.java:141)
04-05 05:44:03.278: E/AndroidRuntime(1057):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1234)
04-05 05:44:03.278: E/AndroidRuntime(1057):     at android.os.Handler.dispatchMessage(Handler.java:99)
04-05 05:44:03.278: E/AndroidRuntime(1057):     at android.os.Looper.loop(Looper.java:137)
04-05 05:44:03.278: E/AndroidRuntime(1057):     at android.app.ActivityThread.main(ActivityThread.java:5041)
04-05 05:44:03.278: E/AndroidRuntime(1057):     at java.lang.reflect.Method.invokeNative(Native Method)
04-05 05:44:03.278: E/AndroidRuntime(1057):     at java.lang.reflect.Method.invoke(Method.java:511)
04-05 05:44:03.278: E/AndroidRuntime(1057):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
04-05 05:44:03.278: E/AndroidRuntime(1057):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)
04-05 05:44:03.278: E/AndroidRuntime(1057):     at dalvik.system.NativeStart.main(Native Method)
04-05 05:44:03.278: E/AndroidRuntime(1057): Caused by: java.lang.NullPointerException
04-05 05:44:03.278: E/AndroidRuntime(1057):     at com.alex.googlemaps.MainActivity.onCreate(MainActivity.java:24)
04-05 05:44:03.278: E/AndroidRuntime(1057):     at android.app.Activity.performCreate(Activity.java:5104)
04-05 05:44:03.278: E/AndroidRuntime(1057):     at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1080)
04-05 05:44:03.278: E/AndroidRuntime(1057):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2144)
04-05 05:44:03.278: E/AndroidRuntime(1057):     ... 11 more
04-05 05:44:03.378: D/dalvikvm(1057): GC_CONCURRENT freed 257K, 14% free 2545K/2948K, paused 16ms+3ms, total 114ms
04-05 05:44:06.338: I/Process(1057): Sending signal. PID: 1057 SIG: 9

I googled a lot but all results are giving error at last. Anybody please helps me to resolve this issue.

0

3 Answers 3

2

After finding the view by Id try getting the map instance from Map fragment as follows:

@Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        MapFragment mpFragment= ((MapFragment) getFragmentManager().findFragmentById(R.id.map));
        mMap=mpFragment.getMap();
        mMap.setMapType(GoogleMap.MAP_TYPE_SATELLITE);
        final LatLng CIU = new LatLng(35.21843892856462, 33.41662287712097);
        Marker ciu = mMap.addMarker(new MarkerOptions()
                                  .position(CIU).title("My Office"));
    }
Sign up to request clarification or add additional context in comments.

4 Comments

This doesn't look any different to the code posted above (other than getting the fragment into a separate variable. Am I missing something?
No, it didnt fix the problem.
Are you trying to run on emulator or phone?
According to the API docs for the new Google V2 library it would require the play store apk installed on your device..It will not run on emulator. Google Play Store won't display the application on devices that don't have OpenGL ES version 2.
1

I suspect your problems is the Google Play APK. In your logcat it states it's out of date and here: http://developer.android.com/reference/com/google/android/gms/maps/MapFragment.html it states that if google play is unavailable MapFragment.getMap() will return null, which is consistent with the error you're getting.

Hope this helps!

5 Comments

please give me an example.
I'm afraid I can't - if you update your google play services, it'll either fix the problem or it won't - there doesn't look to be anything wrong with your code per se. You can find out about how to update google play services here: developer.android.com/google/play-services/setup.html
I already have the latest version. Anyway thanks for find out the problem.
UPDATE: The problem is only when working with the emulator, on the real device it works perfectly...:p
Interesting... another example as to why it's preferable to develop against a real device! (OpenGLES being another of my favourites)
0

Even though this has been marked as solved, I am still willing to post some info for the solution to my case.

Please refer to the answer How to check Google Play services version?

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.