Google地图未在Android原生应用中加载

时间:2014-01-05 14:59:09

标签: java android google-maps google-play-services

我尝试使用Android应用谷歌地图,但我的努力是徒劳的。我甚至在Stackoverflow中遇到了同样问题的许多问题,但没有一个答案解决了我的问题。所以我再次重新提出问题,希望能解决我的问题。

我按照本教程(http://www.androidhive.info/2013/08/android-working-with-google-maps-v2/)来设计我的应用程序。

代码: MainActivity.java

package com.example.hopemap;

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

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

public class MainActivity extends Activity {

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

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

}

的Manifest.xml

<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.hopemap"
android:versionCode="1"
android:versionName="1.0" >
<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="key"/>
<uses-sdk
    android:minSdkVersion="12"
    android:targetSdkVersion="19" />

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

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

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

Layout.xml

    <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"/>

</RelativeLayout>

LogCat的日志文件

01-05 19:44:03.792: D/AndroidRuntime(22917): Shutting down VM
01-05 19:44:03.792: W/dalvikvm(22917): threadid=1: thread exiting with uncaught exception (group=0x4150d930)
01-05 19:44:03.802: E/AndroidRuntime(22917): FATAL EXCEPTION: main
01-05 19:44:03.802: E/AndroidRuntime(22917): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.hopemap/com.example.hopemap.MainActivity}: android.view.InflateException: Binary XML file line #6: Error inflating class fragment
01-05 19:44:03.802: E/AndroidRuntime(22917):    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2245)
01-05 19:44:03.802: E/AndroidRuntime(22917):    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2299)
01-05 19:44:03.802: E/AndroidRuntime(22917):    at android.app.ActivityThread.access$700(ActivityThread.java:150)
01-05 19:44:03.802: E/AndroidRuntime(22917):    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1280)
01-05 19:44:03.802: E/AndroidRuntime(22917):    at android.os.Handler.dispatchMessage(Handler.java:99)
01-05 19:44:03.802: E/AndroidRuntime(22917):    at android.os.Looper.loop(Looper.java:137)
01-05 19:44:03.802: E/AndroidRuntime(22917):    at android.app.ActivityThread.main(ActivityThread.java:5283)
01-05 19:44:03.802: E/AndroidRuntime(22917):    at java.lang.reflect.Method.invokeNative(Native Method)
01-05 19:44:03.802: E/AndroidRuntime(22917):    at java.lang.reflect.Method.invoke(Method.java:511)
01-05 19:44:03.802: E/AndroidRuntime(22917):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1102)
01-05 19:44:03.802: E/AndroidRuntime(22917):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:869)
01-05 19:44:03.802: E/AndroidRuntime(22917):    at dalvik.system.NativeStart.main(Native Method)
01-05 19:44:03.802: E/AndroidRuntime(22917): Caused by: android.view.InflateException: Binary XML file line #6: Error inflating class fragment
01-05 19:44:03.802: E/AndroidRuntime(22917):    at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:710)
01-05 19:44:03.802: E/AndroidRuntime(22917):    at android.view.LayoutInflater.rInflate(LayoutInflater.java:752)
01-05 19:44:03.802: E/AndroidRuntime(22917):    at android.view.LayoutInflater.inflate(LayoutInflater.java:495)
01-05 19:44:03.802: E/AndroidRuntime(22917):    at android.view.LayoutInflater.inflate(LayoutInflater.java:397)
01-05 19:44:03.802: E/AndroidRuntime(22917):    at android.view.LayoutInflater.inflate(LayoutInflater.java:353)
01-05 19:44:03.802: E/AndroidRuntime(22917):    at com.android.internal.policy.impl.PhoneWindow.setContentView(PhoneWindow.java:364)
01-05 19:44:03.802: E/AndroidRuntime(22917):    at android.app.Activity.setContentView(Activity.java:1930)
01-05 19:44:03.802: E/AndroidRuntime(22917):    at com.example.hopemap.MainActivity.onCreate(MainActivity.java:17)
01-05 19:44:03.802: E/AndroidRuntime(22917):    at android.app.Activity.performCreate(Activity.java:5283)
01-05 19:44:03.802: E/AndroidRuntime(22917):    at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1097)
01-05 19:44:03.802: E/AndroidRuntime(22917):    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2209)
01-05 19:44:03.802: E/AndroidRuntime(22917):    ... 11 more
01-05 19:44:03.802: E/AndroidRuntime(22917): Caused by: java.lang.IllegalStateException: A required meta-data tag in your app's AndroidManifest.xml does not exist.  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" />
01-05 19:44:03.802: E/AndroidRuntime(22917):    at com.google.android.gms.common.GooglePlayServicesUtil.n(Unknown Source)
01-05 19:44:03.802: E/AndroidRuntime(22917):    at com.google.android.gms.common.GooglePlayServicesUtil.isGooglePlayServicesAvailable(Unknown Source)
01-05 19:44:03.802: E/AndroidRuntime(22917):    at com.google.android.gms.maps.internal.q.v(Unknown Source)
01-05 19:44:03.802: E/AndroidRuntime(22917):    at com.google.android.gms.maps.internal.q.u(Unknown Source)
01-05 19:44:03.802: E/AndroidRuntime(22917):    at com.google.android.gms.maps.MapsInitializer.initialize(Unknown Source)
01-05 19:44:03.802: E/AndroidRuntime(22917):    at com.google.android.gms.maps.MapFragment$b.cE(Unknown Source)
01-05 19:44:03.802: E/AndroidRuntime(22917):    at com.google.android.gms.maps.MapFragment$b.a(Unknown Source)
01-05 19:44:03.802: E/AndroidRuntime(22917):    at com.google.android.gms.dynamic.a.a(Unknown Source)
01-05 19:44:03.802: E/AndroidRuntime(22917):    at com.google.android.gms.dynamic.a.onInflate(Unknown Source)
01-05 19:44:03.802: E/AndroidRuntime(22917):    at com.google.android.gms.maps.MapFragment.onInflate(Unknown Source)
01-05 19:44:03.802: E/AndroidRuntime(22917):    at android.app.Activity.onCreateView(Activity.java:4871)
01-05 19:44:03.802: E/AndroidRuntime(22917):    at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:686)
01-05 19:44:03.802: E/AndroidRuntime(22917):    ... 21 more

在手机上运行应用程序时,屏幕显示为白色,并显示以下消息“很遗憾HopeTest已停止”。请帮帮我。

2 个答案:

答案 0 :(得分:2)

Application标记

中的manifest.xml中包含以下代码行
<meta-data android:name="com.google.android.gms.version" 
android:value="@integer/google_play_services_version" />

create an integer variable inside res/value folder with version of google play

例如:res / values / version.xml

<?xml version="1.0" encoding="utf-8"?>
<resources>
    <integer name="google_play_services_version">4030500</integer>
</resources>

答案 1 :(得分:0)

尝试从

复制version.xml文件
  google-play-services_lib/res/values/version.xml

  YourProjectName/res/values/version.xml