简体   繁体   中英

Google Maps Android API V2 crashes

I am trying to create an application with Google Maps API V2 by following this tutorial: http://www.androidhive.info/2013/08/android-working-with-google-maps-v2/

I followed this tutorial carefully but my application is crashing. Here's my ManifestFile(i have multiple activities)

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

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

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

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

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

<!-- Required to show current location -->
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />

<!-- Required OpenGL ES 2.0. for Maps V2 -->
<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" >

    <!-- DEVELOPER'S LOADING SCREEN -->
    <activity
        android:name=".DevelopersSplashScreen"
        android:label="@string/title_activity_developers_splash_screen"
        android:screenOrientation="landscape" >


    </activity>                                 <!-- END OF ACTIVITY -->



    <!-- MONSTER ISLAND PHIL LOADING SCREEN -->
    <activity
        android:name=".PhilimonSplashScreen"
        android:label="@string/title_activity_sample"
        android:screenOrientation="landscape" >
    </activity>                                                     <!-- END OF ACTIVITY -->


    <!-- MY HOME ACTIVITY SCREEN -->
    <activity
        android:name=".MainActivity"
        android:label="@string/app_name"
        android:screenOrientation="landscape" >
    </activity>                                                      <!-- END OF ACTIVITY -->




    <activity
        android:name=".Home"
        android:label="@string/title_activity_home" 
        android:screenOrientation="landscape">
         <intent-filter>
            <action android:name="android.intent.action.MAIN" />

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

    </activity>                                                          <!-- END OF ACTIVITY -->

     <!-- Google Maps API Key -->
    <meta-data
        android:name="com.google.android.maps.v2.API_KEY"
        android:value="AIzaSyCiwzjBd6Mx9Mp_tUXAezALDRnDG6kzIzo" />
</application>

my XMl layout:

<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">

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

my Java file:

package com.example.philimon;

import com.google.android.gms.maps.GoogleMap;
import com.google.android.gms.maps.MapFragment;
import android.annotation.SuppressLint;
import android.app.Activity;
import android.os.Bundle;
import android.support.v4.app.FragmentActivity;
import android.view.Menu;
import android.view.MenuItem;
import android.view.Window;
import android.view.WindowManager;
import android.widget.Toast;


@SuppressLint("NewApi")

public class Home extends Activity  {

// Google Map
private GoogleMap googleMap;

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

    try {
        // Loading map
        initilizeMap();

    } catch (Exception e) {
        e.printStackTrace();
    }


}

/**
 * function to load map. If map is not created it will create it for you
 * */
private void initilizeMap() {
    if (googleMap == null) {
        googleMap = ((MapFragment) getFragmentManager().findFragmentById(
                R.id.map)).getMap();

        // check if map is created successfully or not
        if (googleMap == null) {
            Toast.makeText(getApplicationContext(),"Sorry! unable to create maps", Toast.LENGTH_SHORT).show();
        }
    }
}

@Override
protected void onResume() {
    super.onResume();
    initilizeMap();
}


}

You didn't add the GMS version meta-data:

<application>
    <meta-data
            android:name="com.google.android.gms.version"
            android:value="@integer/google_play_services_version"/>
    ...
</application>

This is an example manifest file that uses google Google Maps Android API v2 on Android Wear

<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="your package name" >

<!-- Mark this app as an Android Wear app. -->
<uses-feature android:name="android.hardware.type.watch" />

<!-- Permissions and features required for the Android Maps API v2 -->
<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-feature
        android:glEsVersion="0x00020000"
        android:required="true"/>

<application
    android:allowBackup="true"
    android:icon="@mipmap/ic_launcher"
    android:label="@string/app_name"
    android:theme="@android:style/Theme.DeviceDefault" >

    <!-- API key for the Android Maps API v2. The value is defined as a string resource. -->
    <meta-data android:name="com.google.android.geo.API_KEY"
               android:value="@string/google_maps_key"/>

    <!-- Meta data required for Google Play Services -->
    <meta-data
            android:name="com.google.android.gms.version"
            android:value="@integer/google_play_services_version" />

    <activity
        android:name=".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>
</application>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM