1

I am trying to show Google Maps on my application but it always crash. I have gotten the API from Google and already added the Google Play Service library to my project. The Map is called in DisplayResult.java

Here is my AndroidManifest.xml:

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

<uses-sdk
    android:minSdkVersion="11"
    android:targetSdkVersion="19" />

<permission
    android:name="com.vogella.android.locationapi.maps.permission.MAPS_RECEIVE"
    android:protectionLevel="signature" />

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

<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/>
<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" />

<uses-permission android:name="com.example.newmapview.permission.MAPS_RECEIVE" />

<application
    android:allowBackup="true"
    android:icon="@drawable/ic_launcher"
    android:label="@string/app_name"
    android:theme="@style/AppTheme" >
    <activity
        android:name="com.example.chicagobus.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>
    <activity
        android:name="com.example.chicagobus.DisplayMessageActivity"
        android:label="@string/title_activity_display_message"
        android:parentActivityName="com.example.chicagobus.MainActivity" >
        <meta-data
            android:name="android.support.PARENT_ACTIVITY"
            android:value="com.example.chicagobus.MainActivity" />
    </activity>
    <activity
        android:name="com.example.chicagobus.DisplayResultActivity"
        android:label="@string/title_activity_display_result"
        android:parentActivityName="com.example.chicagobus.MainActivity" >
        <meta-data
            android:name="android.support.PARENT_ACTIVITY"
            android:value="com.example.chicagobus.MainActivity" />
    </activity>
    <meta-data
        android:name="com.google.android.maps.v2.API_KEY"
        android:value="AIzaSyB8J8kUL9n..................." />
    <meta-data android:name="com.google.android.gms.version"
         android:value="@integer/google_play_services_version" />
</application>

And this is the layout of DisplayResult.java that calls the map:

<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=".DisplayResultActivity" >

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

</RelativeLayout>

And here is my Java code:

package com.example.chicagobus;

import android.os.Bundle;
import android.support.v4.app.FragmentActivity;

public class DisplayResultActivity extends FragmentActivity {

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

Any ideas about this?

nKn
  • 13,691
  • 9
  • 45
  • 62
bcbulz
  • 11
  • 1
  • 1
    Emulator doesn't support Goolge play services so try using real deceive after check your package name in manifest file – mohammed momn Feb 19 '14 at 21:10

1 Answers1

0

Change this:

<permission
    android:name="com.vogella.android.locationapi.maps.permission.MAPS_RECEIVE"
    android:protectionLevel="signature" />
// and
<uses-permission android:name="com.example.newmapview.permission.MAPS_RECEIVE" />

By the package name of your app as:

<permission
    android:name="com.example.chicagobus.permission.MAPS_RECEIVE"
    android:protectionLevel="signature" />
// and
<uses-permission android:name="com.example.chicagobus.permission.MAPS_RECEIVE" />  

Also, try to add this code to your onCreate method to initialize your fragment map:

myMap = ((SupportMapFragment) (getSupportFragmentManager().findFragmentById(R.id.map))).g‌​etMap();

Finally, as @mohammedmomn said: you cannot use Google Play Services on an emulator... I mean not always. To use it, see these tips:
1. Running google map application on Android Emulator
2. How to Run Google Map API V2 on Android Emulator.
And this question might help you: Running Google Maps v2 on Android Emulator

Let me know if that helps you.

Community
  • 1
  • 1
Blo
  • 11,903
  • 5
  • 45
  • 99
  • Fllo, thank you for the reply. I changed the permission as you stated and when I installed the API, I followed the instruction from those tips. If I put the code in onCreate, it will return a java.lang.nullpointer exception on the GoogleMap supportMap = supportmapfragment.getMap();. When I remove those code, it is still doesn't work with the original error message. Do you know what is the error message mean? – bcbulz Feb 20 '14 at 16:44
  • The NullPointerException is caused by your map which is not ready yet. Try to check if the GooglePlayServices is available, see this: http://stackoverflow.com/a/19542071/2668136 – Blo Feb 23 '14 at 10:23