错误Api密钥未找到

时间:2014-08-05 12:15:47

标签: android google-maps android-fragments

我正在尝试在我的应用中使用谷歌地图,我在应用程序中使用谷歌播放服务

MapViewActivity:

public class MapsViewActivity extends FragmentActivity {
// Google Map
private GoogleMap googleMap;

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

    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();
        }
    }
}

XML:

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

并收到以下错误:

08-05 16:41:54.384: E/dalvikvm(24979): Could not find class 'gpq', referenced from   method gpr.a
08-05 16:41:54.384: W/dalvikvm(24979): VFY: unable to resolve new-instance 4090 (Lgpq;) in Lgpr;
08-05 16:41:54.384: D/dalvikvm(24979): VFY: replacing opcode 0x22 at 0x0000
08-05 16:41:54.404: W/dalvikvm(24979): VFY: unable to resolve static field 20875 (t) in Lyo;
08-05 16:41:54.404: D/dalvikvm(24979): VFY: replacing opcode 0x62 at 0x000e
08-05 16:41:54.404: W/dalvikvm(24979): VFY: unable to resolve static field 20875 (t) in Lyo;
08-05 16:41:54.484: E/AndroidRuntime(24979): FATAL EXCEPTION: main
08-05 16:41:54.484: E/AndroidRuntime(24979): java.lang.RuntimeException: Unable to start  activity ComponentInfo{com.example.careandcure/com.example.careandcure.MapsViewActivity}: android.view.InflateException: Binary XML file line #6: Error inflating class fragment
08-05 16:41:54.484: E/AndroidRuntime(24979):    at com.example.careandcure.MapsViewActivity.onCreate(MapsViewActivity.java:16)
08-05 16:41:54.484: E/AndroidRuntime(24979):    at android.app.Activity.performCreate(Activity.java:4538)
08-05 16:41:54.484: E/AndroidRuntime(24979):    at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1071)
08-05 16:41:54.484: E/AndroidRuntime(24979):    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2161)
08-05 16:41:54.484: E/AndroidRuntime(24979): Caused by: java.lang.RuntimeException: API key not found.  Check that <meta-data android:name="com.google.android.maps.v2.API_KEY" android:value="your API key"/> is in the <application> element of AndroidManifest.xml
08-05 16:41:54.484: E/AndroidRuntime(24979):    at com.google.android.gms.maps.internal.IMapFragmentDelegate$a$a.onCreateView(Unknown Source)

清单:

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

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

<permission
    android:name="info.androidhive.googlemapsv2.permission.MAPS_RECEIVE"
    android:protectionLevel="signature" />

<uses-permission android:name="info.androidhive.googlemapsv2.permission.MAPS_RECEIVE" />
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
<uses-permission android:name="android.permission.ACCESS_MOCK_LOCATION" />
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
<uses-permission android:name="com.google.android.providers.gsf.permission.READ_GSERVICES" />
<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" />

<!-- Required OpenGL ES 2.0. for Maps V2 -->
<uses-feature
    android:glEsVersion="0x00020000"
    android:required="true" />

<application
    android:allowBackup="true"
    android:icon="@drawable/logo"
    android:label="@string/app_name"
    android:theme="@style/AppTheme" >
    <activity
        android:name="com.example.careandcure.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.careandcure.MapsViewActivity" >
    </activity>

        <!-- Goolge Maps API Key -->
        <meta-data
            android:name="com.google.android.gms.version"
            android:value="@integer/google_play_services_version" />
        <meta-data
            android:name="com.google.android.maps.v2.API_KEY"
            android:value="api_key" />

</application>

</manifest>

我没有得到我错了或遗失任何东西的地方 如何删除请帮我解决这个问题。

3 个答案:

答案 0 :(得分:0)

显然,从您的logcat开始,您只需复制粘贴的manifest.xml文件。您必须输入密钥,而不是您的API密钥

从Google控制台生成密钥。并在manifest.xml中使用它

googlemap版本2需要两个元数据

一个使用 api密钥,另一个使用 google服务

android:value="@integer/google_play_services_version"

所以你应该简单地复制粘贴线。而且只能稍后试着。 : - )

<强>编辑:

将包名更改为您的(不是androidhive :-))更改权限

中的行
<permission
    android:name="info.androidhive.googlemapsv2.permission.MAPS_RECEIVE"
    android:protectionLevel="signature" />

要:

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

另外,为什么在uses-permission中使用 info.androidhive.googlemapsv2 的包名称。将其更改为您自己的包名称,即 com.example.careandcure 。希望它最终成为你的解决方案。

答案 1 :(得分:0)

日志说你:

 Caused by: java.lang.RuntimeException: API key not found.  Check that <meta-data android:name="com.google.android.maps.v2.API_KEY" android:value="your API key"/> is in the <application> element of AndroidManifest.xml

所以请确保您在AndroidManifest.xml中添加它,就像在guide中一样:

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

答案 2 :(得分:0)

在您的清单文件错误导致此行

<!-- Goolge Maps API Key -->
    <meta-data
        android:name="com.google.android.maps.v2.API_KEY"
        android:value="my api key" />

在API密钥的值中,您必须添加自己的API密钥。

请参阅此link以获取生成API密钥