简体   繁体   中英

Android Google Maps V2 - Project Has Stopped Working

I'm trying to use google android maps V2 on my samsung s3 mini (4.1.2) however I keep on getting the message "Application has stopped unexpectedly". I had included the google APIs [Android 4.1.2], Google play services library, android-support-v4.jar. Here are the codes: In my AndroidManifest.xml

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

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

    <uses-permission android:name="com.Test.projecttest.MAPS_RECEIVE" />
    <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" />
    <!--
     The following two permissions are not required to use
     Google Maps Android API v2, but are recommended.
    -->
    <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" />

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

    <application
        android:allowBackup="true"
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/AppTheme" >
        <meta-data
            android:name="com.google.android.maps.v2.API_KEY"
            android:value="AIzaSyBjT4hZc4gKo1lyrQ5fwLD_Fz5vWgUQmlA" />

        <activity
            android:name="com.Test.projecttest.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>

</manifest>

MainActivity.java in com.Test.projecttest

      package com.Test.projecttest;

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

public class MainActivity extends FragmentActivity {

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

main.xml located at res/layout

    <?xml version="1.0" encoding="utf-8"?>
<fragment 
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/map"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    class="com.google.android.gms.maps.SupportMapFragment" />

My logcat:

07-19 16:21:46.594: E/AndroidRuntime(5069): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.Test.projecttest/com.Test.projecttest.MainActivity}: android.view.InflateException: Binary XML file line #2: Error inflating class fragment
07-19 16:21:46.594: E/AndroidRuntime(5069): Caused by: android.view.InflateException: Binary XML file line #2: Error inflating class fragment
07-19 16:21:46.594: E/AndroidRuntime(5069): 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
07-19 16:21:46.594: E/AndroidRuntime(5069): Caused by: java.lang.ClassNotFoundException: com.google.android.gms.maps.SupportMapFragment

Your minsdk is 8

 <uses-sdk
    android:minSdkVersion="8"

Use SupportFragment .

Use the below in your layout xml

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

Your activity must extend FragmentActivity .

Also add this to manifest

    <permission
    android:name="com.Test.projecttest.permission.MAPS_RECEIVE"
    android:protectionLevel="signature"/>
    <uses-permission android:name="com.Test.projecttest.permission.MAPS_RECEIVE"/>

To initialize map object

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

Make sure you have added support library

Also make sure you imported the below

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

You need to add this in you manifest: + all that @Raghunandan said ;)

 <permission
    android:name="your.package.permission.MAPS_RECEIVE"
    android:protectionLevel="signature" />

<uses-permission android:name="your.package.permission.MAPS_RECEIVE" />

No need to write that in the xml.

just extend the activity to SupportMapFragment

then you will be able to use all google map features

and add this to manifest:

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

I displayed the map on a fragment and attached the fragment on the MainActivity . It makes it more flexible. i did it in the following way:

import com.google.android.gms.maps.CameraUpdate;
import com.google.android.gms.maps.CameraUpdateFactory;
import com.google.android.gms.maps.GoogleMap;
import com.google.android.gms.maps.GoogleMap.OnCameraChangeListener;
import com.google.android.gms.maps.GoogleMap.OnMapClickListener;
import com.google.android.gms.maps.SupportMapFragment;
import com.google.android.gms.maps.model.BitmapDescriptorFactory;
import com.google.android.gms.maps.model.CameraPosition;
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 MapFragment extends SupportMapFragment implements
        LocationListener, OnMapClickListener, OnCameraChangeListener {

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