Android Google Maps API v2的问题(

时间:2013-11-18 21:23:46

标签: java android eclipse google-maps google-maps-api-2

我一直在尝试在Eclipse中创建一个Google Maps API v2应用,但我一直遇到一系列我不确定如何解决的错误。

几天前开始编写Android应用程序(大约两周前编码),所以这对我来说还是一个新手。

我获得了SHA和MD5,我的API v2密钥,在Google上注册了它等等。

我不确定我哪里出错!

这是尝试作为Android应用程序运行时的CatLog:

>11-18 16:02:19.756: E/Trace(16453): error opening trace file: No such file or directory (2)
>11-18 16:02:19.756: D/ActivityThread(16453): setTargetHeapUtilization:0.25

>11-18 16:02:19.756: D/ActivityThread(16453): setTargetHeapIdealFree:8388608

>11-18 16:02:19.756: D/ActivityThread(16453): setTargetHeapConcurrentStart:2097152

>11-18 16:02:19.816: W/dalvikvm(16453): threadid=1: thread exiting with uncaught exception (group=0x412b5438)

>11-18 16:02:19.826: E/AndroidRuntime(16453): FATAL EXCEPTION: main

>11-18 16:02:19.826: E/AndroidRuntime(16453): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.deliveryfor/com.deliveryfor.MainActivity}: android.view.InflateException: Binary XML file line #17: Error inflating class fragment

>11-18 16:02:19.826: E/AndroidRuntime(16453):   at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2081)

>11-18 16:02:19.826: E/AndroidRuntime(16453):   at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2106)

>11-18 16:02:19.826: E/AndroidRuntime(16453):   at 
android.app.ActivityThread.access$600(ActivityThread.java:138)

>11-18 16:02:19.826: E/AndroidRuntime(16453):   at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1204
)
>11-18 16:02:19.826: E/AndroidRuntime(16453):   at android.os.Handler.dispatchMessage(Handler.java:99)

>11-18 16:02:19.826: E/AndroidRuntime(16453):   at android.os.Looper.loop(Looper.java:137)

>11-18 16:02:19.826: E/AndroidRuntime(16453):   at android.app.ActivityThread.main(ActivityThread.java:4921)

>11-18 16:02:19.826: E/AndroidRuntime(16453):   at java.lang.reflect.Method.invokeNative(Native Method)

>11-18 16:02:19.826: E/AndroidRuntime(16453):   at java.lang.reflect.Method.invoke(Method.java:511)

>11-18 16:02:19.826: E/AndroidRuntime(16453):   at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:790)

>11-18 16:02:19.826: E/AndroidRuntime(16453):   at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:557)

>11-18 16:02:19.826: E/AndroidRuntime(16453):   at dalvik.system.NativeStart.main(Native Method)

>11-18 16:02:19.826: E/AndroidRuntime(16453): Caused by: android.view.InflateException: Binary XML file line #17: Error inflating class fragment

>11-18 16:02:19.826: E/AndroidRuntime(16453):   at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:704)

>11-18 16:02:19.826: E/AndroidRuntime(16453):   at android.view.LayoutInflater.rInflate(LayoutInflater.java:746)

>11-18 16:02:19.826: E/AndroidRuntime(16453):   at android.view.LayoutInflater.inflate(LayoutInflater.java:489)

>11-18 16:02:19.826: E/AndroidRuntime(16453):   at android.view.LayoutInflater.inflate(LayoutInflater.java:396)

>11-18 16:02:19.826: E/AndroidRuntime(16453):   at android.view.LayoutInflater.inflate(LayoutInflater.java:352)

>11-18 16:02:19.826: E/AndroidRuntime(16453):   at com.android.internal.policy.impl.PhoneWindow.setContentView(PhoneWindow.java:398)

>11-18 16:02:19.826: E/AndroidRuntime(16453):   at android.app.Activity.setContentView(Activity.java:2087)

>11-18 16:02:19.826: E/AndroidRuntime(16453):   at com.deliveryfor.MainActivity.onCreate(MainActivity.java:24)

>11-18 16:02:19.826: E/AndroidRuntime(16453):   at android.app.Activity.performCreate(Activity.java:5234)

>11-18 16:02:19.826: E/AndroidRuntime(16453):   at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1082)

>11-18 16:02:19.826: E/AndroidRuntime(16453):   at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2045)

>11-18 16:02:19.826: E/AndroidRuntime(16453):   ... 11 more

>11-18 16:02:19.826: E/AndroidRuntime(16453): Caused by: java.lang.IllegalStateException: The meta-data tag in your app's AndroidManifest.xml does not have the right value.  Expected 4030500 but found 0.  You must have the following declaration within the <application> element:     <meta-data android:name="com.google.android.gms.version" android:value="@integer/google_play_services_version" />

>11-18 16:02:19.826: E/AndroidRuntime(16453):   at com.google.android.gms.common.GooglePlayServicesUtil.n(Unknown Source)

>11-18 16:02:19.826: E/AndroidRuntime(16453):   at com.google.android.gms.common.GooglePlayServicesUtil.isGooglePlayServicesAvailable(Unknown Source)

>11-18 16:02:19.826: E/AndroidRuntime(16453):   at com.google.android.gms.maps.internal.q.v(Unknown Source)

>11-18 16:02:19.826: E/AndroidRuntime(16453):   at com.google.android.gms.maps.internal.q.u(Unknown Source)

>11-18 16:02:19.826: E/AndroidRuntime(16453):   at com.google.android.gms.maps.MapsInitializer.initialize(Unknown Source)

>11-18 16:02:19.826: E/AndroidRuntime(16453):   at com.google.android.gms.maps.MapFragment$b.cE(Unknown Source)

>11-18 16:02:19.826: E/AndroidRuntime(16453):   at com.google.android.gms.maps.MapFragment$b.a(Unknown Source)

>11-18 16:02:19.826: E/AndroidRuntime(16453):   at com.google.android.gms.dynamic.a.a(Unknown Source)

>11-18 16:02:19.826: E/AndroidRuntime(16453):   at com.google.android.gms.dynamic.a.onInflate(Unknown Source)

>11-18 16:02:19.826: E/AndroidRuntime(16453):   at com.google.android.gms.maps.MapFragment.onInflate(Unknown Source)

>11-18 16:02:19.826: E/AndroidRuntime(16453):   at android.app.Activity.onCreateView(Activity.java:4889)

>11-18 16:02:19.826: E/AndroidRuntime(16453):   at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:680)
11-18 16:02:19.826: E/AndroidRuntime(16453):    ... 21 more

说实话,我不确定如何从那块巨大的红色中推断出任何解决方案。以下是我项目的要素:

MainActivity:

import android.app.Activity;
import android.os.Bundle;
import android.view.Menu;
import android.view.View;
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.MapFragment;
import com.google.android.gms.maps.model.LatLng;
import com.google.android.gms.maps.model.MarkerOptions;

public class MainActivity extends Activity {
    private final LatLng LOCATION_STATE = new LatLng(49.27645, -122.917587);
    private final LatLng LOCATION_CITY = new LatLng(49.187500, -122.849000);
    private final LatLng LOCATION_YOU = new LatLng(49.187500, -122.849000);

    private GoogleMap map;

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

        map  = ((MapFragment) getFragmentManager().findFragmentById(R.id.map)).getMap();

        map.addMarker(new MarkerOptions().position(LOCATION_YOU).title("This is you!"));
    }

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

    public void onClick_State(View v) {
//      CameraUpdate update = CameraUpdateFactory.newLatLng(LOCATION_BURNABY);
        map.setMapType(GoogleMap.MAP_TYPE_SATELLITE);
        CameraUpdate update = CameraUpdateFactory.newLatLngZoom(LOCATION_YOU, 9);
        map.animateCamera(update);
    }
    public void onClick_City(View v) {
        map.setMapType(GoogleMap.MAP_TYPE_TERRAIN);
        CameraUpdate update = CameraUpdateFactory.newLatLngZoom(LOCATION_CITY, 14);
        map.animateCamera(update);

    }
    public void onClick_You(View v) {
        map.setMapType(GoogleMap.MAP_TYPE_NORMAL);
        CameraUpdate update = CameraUpdateFactory.newLatLngZoom(LOCATION_STATE, 20);
        map.animateCamera(update);

    }

}

activity_main.java

<?xml version="1.0" encoding="utf-8"?>
<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" >

    <TextView
        android:id="@+id/header"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"/>

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

    <Button 
        android:layout_height="wrap_content" 
        android:layout_width="wrap_content" 
        android:text="You" 
        android:onClick="onClick_You" 
        android:layout_alignParentTop="true" 
        android:layout_alignParentRight="true" 
        android:id="@+id/btnYou"/> 

    <Button 
        android:layout_height="wrap_content" 
        android:layout_width="wrap_content" 
        android:text="City" 
        android:onClick="onClick_City" 
        android:layout_alignParentTop="true" 
        android:id="@+id/btnCity" 
        android:layout_centerHorizontal="true"/> 

    <Button 
        android:layout_height="wrap_content" 
        android:layout_width="wrap_content" 
        android:text="State" 
        android:onClick="onClick_State" 
        android:layout_alignParentTop="true"  
        android:id="@+id/btnState" 
        android:layout_alignParentLeft="true"/>



</RelativeLayout>

AndroidManifest:

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

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

    <uses-permission android:name="com.deliveryfor.permission.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"/> 
    <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" 
        android:debuggable="true">

    <meta-data
        android:name="com.google.android.maps.v2.API_KEY"
        android:value="AIzaSyBK2qJjm02TrBMNd9eza7PuyRGxWNpxng0"/>

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

2 个答案:

答案 0 :(得分:0)

要测试您的应用程序,您还必须设置Google Play服务SDK,请参阅此link并将此标记添加到您的清单文件中:

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

您的logcat指向:

>11-18 16:02:19.826: E/AndroidRuntime(16453): Caused by: java.lang.IllegalStateException: The meta-data tag in your app's AndroidManifest.xml does not have the right value.  
Expected 4030500 but found 0.  
You must have the following declaration within the <application> element:     
<meta-data android:name="com.google.android.gms.version" android:value="@integer/google_play_services_version" />

答案 1 :(得分:0)

从您的日志中

java.lang.IllegalStateException: The meta-data tag in your app's AndroidManifest.xml does not have the right value.  Expected 4030500 but found 0.  You must have the following declaration within the <application> element:     <meta-data android:name="com.google.android.gms.version" android:value="@integer/google_play_services_version" />

在结束标记应用程序之前移动元数据标记。