Mapview在android api v2中崩溃了

时间:2014-04-17 09:33:06

标签: android

我尝试使用android api v2创建一个mapview。但是我的应用程序在设备中崩溃了。但是在模拟器中工作不会崩溃

这些是我得到的主要错误。我无法弄清楚问题是什么。请帮我。 我使用了androidhive的教程。

Android清单

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

    <uses-sdk
        android:minSdkVersion="12"
        android:targetSdkVersion="17" />

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

    <uses-feature
        android:glEsVersion="0x00020000"
        android:required="true" />

    <uses-permission android:name="com.example.samplemap.permission.MAPS_RECEIVE" />
    <uses-permission android:name="android.permission.INTERNET" />
    <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.example.samplemap.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.gms.version"
        android:value="@integer/google_play_services_version" />
         <meta-data
            android:name="com.google.android.maps.v2.API_KEY"
            android:value="AIzaSyD1F0YSlV2_WqlY9yhJpqfsPP2jrtod_TU" />
    </application>

</manifest>

Activity_main

&LT;

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"
    tools:context=".MainActivity" >

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

</RelativeLayout> 

MainActivity.java

package com.example.samplemap;


import com.google.android.gms.maps.GoogleMap;
import com.google.android.gms.maps.MapFragment;

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

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

}

点击模拟器上的播放服务(谷歌地图api v2无法在模拟器中工作),我收到以下错误:

04-17 16:47:47.711: E/AndroidRuntime(1685): FATAL EXCEPTION: main
04-17 16:47:47.711: E/AndroidRuntime(1685): android.content.ActivityNotFoundException: No Activity found to handle Intent { act=android.intent.action.VIEW dat=market://details?id=com.google.android.gms flg=0x80000 pkg=com.android.vending }
04-17 16:47:47.711: E/AndroidRuntime(1685):     at android.app.Instrumentation.checkStartActivityResult(Instrumentation.java:1545)
04-17 16:47:47.711: E/AndroidRuntime(1685):     at android.app.Instrumentation.execStartActivity(Instrumentation.java:1416)
04-17 16:47:47.711: E/AndroidRuntime(1685):     at android.app.Activity.startActivityForResult(Activity.java:3351)
04-17 16:47:47.711: E/AndroidRuntime(1685):     at android.app.Activity.startActivityForResult(Activity.java:3312)
04-17 16:47:47.711: E/AndroidRuntime(1685):     at android.app.Activity.startActivity(Activity.java:3522)
04-17 16:47:47.711: E/AndroidRuntime(1685):     at android.app.Activity.startActivity(Activity.java:3490)
04-17 16:47:47.711: E/AndroidRuntime(1685):     at com.google.android.gms.dynamic.a$5.onClick(Unknown Source)
04-17 16:47:47.711: E/AndroidRuntime(1685):     at android.view.View.performClick(View.java:4084)
04-17 16:47:47.711: E/AndroidRuntime(1685):     at android.view.View$PerformClick.run(View.java:16966)
04-17 16:47:47.711: E/AndroidRuntime(1685):     at android.os.Handler.handleCallback(Handler.java:615)
04-17 16:47:47.711: E/AndroidRuntime(1685):     at android.os.Handler.dispatchMessage(Handler.java:92)
04-17 16:47:47.711: E/AndroidRuntime(1685):     at android.os.Looper.loop(Looper.java:137)
04-17 16:47:47.711: E/AndroidRuntime(1685):     at android.app.ActivityThread.main(ActivityThread.java:4745)
04-17 16:47:47.711: E/AndroidRuntime(1685):     at java.lang.reflect.Method.invokeNative(Native Method)
04-17 16:47:47.711: E/AndroidRuntime(1685):     at java.lang.reflect.Method.invoke(Method.java:511)
04-17 16:47:47.711: E/AndroidRuntime(1685):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:786)
04-17 16:47:47.711: E/AndroidRuntime(1685):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
04-17 16:47:47.711: E/AndroidRuntime(1685):     at dalvik.system.NativeStart.main(Native Method)

1 个答案:

答案 0 :(得分:2)

你的logcat清楚地说了

  

引起:java.lang.IllegalStateException:您的元数据标记   应用程序的AndroidManifest.xml没有正确的值   引起:java.lang.IllegalStateException:应用程序的AndroidManifest.xml中的元数据标记没有正确的值。预期4323000但找到0.您必须在元素中包含以下声明:
        04-17 14:59:51.881:E / AndroidRuntime(1057):    预期4323000但找到0.您必须具备以下条件   元素内的声明:

尝试在<meta-data>文件中的<application>标记下添加manifest.xml代码,例如

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

让你的manifest.xml像这样

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

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

</application>