简体   繁体   中英

Starting with Android Google Maps API V2

I am newbie on Android Google Maps API and I was trying to develop a small app using the API. I use as a reference the Google documentation form: https://developers.google.com/maps/documentation/android/ and I follow all the step with caution. When I build it and install it on to my device (Android 2.3.6) the app crash. I follow all the instructions to sign the app and get the API key for this specific app, tried all the step of the documentation several times without any good results. Please, can someone help me with this? I there something missing? Thank you!

Manifest.xml

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

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

<permission
    android:name="com.application.uprm_map.permission.MAPS_RECEIVE"
    android:protectionLevel="signature"/>
<uses-permission android:name="com.application.uprm_map.permission.MAPS_RECEIVE"/>
<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"/>

<application
    android:allowBackup="true"
    android:icon="@drawable/ic_launcher"
    android:label="@string/app_name"
    android:theme="@style/AppTheme" >
    <activity
        android:name="com.application.uprm_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="<my own key>"/>

</application>
</manifest>

activity_main.xml:

   <?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"
      android:name="com.google.android.gms.maps.MapFragment"/>

MainActivity.java

  package com.application.uprm_map;

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

public class MainActivity extends Activity {

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

Exeptions:

03-05 02:29:27.645: D/dalvikvm(32056): GC_EXTERNAL_ALLOC freed 42K, 49% free        2748K/5379K, external 0K/0K, paused 21ms
03-05 02:29:27.650: D/AndroidRuntime(32056): Shutting down VM
03-05 02:29:27.650: W/dalvikvm(32056): threadid=1: thread exiting with uncaught exception (group=0x4001e578)
03-05 02:29:27.655: E/AndroidRuntime(32056): FATAL EXCEPTION: main
03-05 02:29:27.655: E/AndroidRuntime(32056): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.application.uprm_map/com.application.uprm_map.MainActivity}: android.view.InflateException: Binary XML file line #2: Error inflating class fragment
03-05 02:29:27.655: E/AndroidRuntime(32056):    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1651)
03-05 02:29:27.655: E/AndroidRuntime(32056):    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1667)
03-05 02:29:27.655: E/AndroidRuntime(32056):    at android.app.ActivityThread.access$1500(ActivityThread.java:117)
03-05 02:29:27.655: E/AndroidRuntime(32056):    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:935)
03-05 02:29:27.655: E/AndroidRuntime(32056):    at android.os.Handler.dispatchMessage(Handler.java:99)
03-05 02:29:27.655: E/AndroidRuntime(32056):    at android.os.Looper.loop(Looper.java:130)
03-05 02:29:27.655: E/AndroidRuntime(32056):    at android.app.ActivityThread.main(ActivityThread.java:3691)
03-05 02:29:27.655: E/AndroidRuntime(32056):    at java.lang.reflect.Method.invokeNative(Native Method)
03-05 02:29:27.655: E/AndroidRuntime(32056):    at java.lang.reflect.Method.invoke(Method.java:507)
03-05 02:29:27.655: E/AndroidRuntime(32056):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:912)
03-05 02:29:27.655: E/AndroidRuntime(32056):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:670)
03-05 02:29:27.655: E/AndroidRuntime(32056):    at dalvik.system.NativeStart.main(Native Method)
03-05 02:29:27.655: E/AndroidRuntime(32056): Caused by: android.view.InflateException: Binary XML file line #2: Error inflating class fragment
03-05 02:29:27.655: E/AndroidRuntime(32056):    at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:581)
03-05 02:29:27.655: E/AndroidRuntime(32056):    at android.view.LayoutInflater.inflate(LayoutInflater.java:386)
03-05 02:29:27.655: E/AndroidRuntime(32056):    at android.view.LayoutInflater.inflate(LayoutInflater.java:320)
03-05 02:29:27.655: E/AndroidRuntime(32056):    at android.view.LayoutInflater.inflate(LayoutInflater.java:276)
03-05 02:29:27.655: E/AndroidRuntime(32056):    at com.android.internal.policy.impl.PhoneWindow.setContentView(PhoneWindow.java:234)
03-05 02:29:27.655: E/AndroidRuntime(32056):    at android.app.Activity.setContentView(Activity.java:1679)
03-05 02:29:27.655: E/AndroidRuntime(32056):    at com.application.uprm_map.MainActivity.onCreate(MainActivity.java:11)
03-05 02:29:27.655: E/AndroidRuntime(32056):    at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1050)
03-05 02:29:27.655: E/AndroidRuntime(32056):    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1615)
03-05 02:29:27.655: E/AndroidRuntime(32056):    ... 11 more
03-05 02:29:27.655: E/AndroidRuntime(32056): Caused by: java.lang.ClassNotFoundException: android.view.fragment in loader dalvik.system.PathClassLoader[/data/app/com.application.uprm_map-1.apk]
03-05 02:29:27.655: E/AndroidRuntime(32056):    at dalvik.system.PathClassLoader.findClass(PathClassLoader.java:240)
03-05 02:29:27.655: E/AndroidRuntime(32056):    at java.lang.ClassLoader.loadClass(ClassLoader.java:551)
03-05 02:29:27.655: E/AndroidRuntime(32056):    at java.lang.ClassLoader.loadClass(ClassLoader.java:511)
03-05 02:29:27.655: E/AndroidRuntime(32056):    at android.view.LayoutInflater.createView(LayoutInflater.java:471)
03-05 02:29:27.655: E/AndroidRuntime(32056):    at android.view.LayoutInflater.onCreateView(LayoutInflater.java:549)
03-05 02:29:27.655: E/AndroidRuntime(32056):    at com.android.internal.policy.impl.PhoneLayoutInflater.onCreateView(PhoneLayoutInflater.java:66)
03-05 02:29:27.655: E/AndroidRuntime(32056):    at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:568)
03-05 02:29:27.655: E/AndroidRuntime(32056):    ... 19 more
03-05 02:29:29.660: I/dalvikvm(32056): threadid=4: reacting to signal 3
03-05 02:29:29.665: I/dalvikvm(32056): Wrote stack traces to '/data/anr/traces.txt'

First thing as you are having the API level 8 or below so you have to use the SupportMapFragment instead of MapFragment as below in your layout file.

  <?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" android:name="com.google.android.gms.maps.SupportMapFragment"/> 

Second thing you have to extend the FragmentActivity instead of Activity :

  public class MainActivity extends FragmentActivity { 

Do the above changes and then run your 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