谷歌地图Android的API V2的问题与Android 4.3地图、问题、Android、API

2023-09-04 07:00:09 作者:地势坤↗唯吾独尊

我是新手,在谷歌地图。只要遵循这个链接 HTTP://www.androidhive。信息/ 2013/08 / Android的工作与 - 谷歌 - 地图-V2 / 当我启动我的应用程序,follwing错误是来了:

I am novice in Google maps. Just followed this link http://www.androidhive.info/2013/08/android-working-with-google-maps-v2/ when I am launching my app, follwing error is coming up:

Unfotunately Googlemapsv2 has stopped

我已经经历了很多后对计算器的走了,但仍然没有得到哪里出了问题。

I have gone through lots of post on stackoverflow but still not getting where is the problem.

logcat的输出

01-14 18:05:22.301: E/AndroidRuntime(12239): FATAL EXCEPTION: main
01-14 18:05:22.301: E/AndroidRuntime(12239): java.lang.RuntimeException: Unable to start activity ComponentInfo{example.androidhive.googlemapsv2/example.androidhive.googlemapsv2.MainActivity}: android.view.InflateException: Binary XML file line #11: Error inflating class fragment
01-14 18:05:22.301: E/AndroidRuntime(12239):    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2343)
01-14 18:05:22.301: E/AndroidRuntime(12239):    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2395)
01-14 18:05:22.301: E/AndroidRuntime(12239):    at android.app.ActivityThread.access$600(ActivityThread.java:162)
01-14 18:05:22.301: E/AndroidRuntime(12239):    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1364)
01-14 18:05:22.301: E/AndroidRuntime(12239):    at android.os.Handler.dispatchMessage(Handler.java:107)
01-14 18:05:22.301: E/AndroidRuntime(12239):    at android.os.Looper.loop(Looper.java:194)
01-14 18:05:22.301: E/AndroidRuntime(12239):    at android.app.ActivityThread.main(ActivityThread.java:5371)
01-14 18:05:22.301: E/AndroidRuntime(12239):    at java.lang.reflect.Method.invokeNative(Native Method)
01-14 18:05:22.301: E/AndroidRuntime(12239):    at java.lang.reflect.Method.invoke(Method.java:525)
01-14 18:05:22.301: E/AndroidRuntime(12239):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:833)
01-14 18:05:22.301: E/AndroidRuntime(12239):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:600)
01-14 18:05:22.301: E/AndroidRuntime(12239):    at dalvik.system.NativeStart.main(Native Method)
01-14 18:05:22.301: E/AndroidRuntime(12239): Caused by: android.view.InflateException: Binary XML file line #11: Error inflating class fragment
01-14 18:05:22.301: E/AndroidRuntime(12239):    at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:704)
01-14 18:05:22.301: E/AndroidRuntime(12239):    at android.view.LayoutInflater.rInflate(LayoutInflater.java:746)
01-14 18:05:22.301: E/AndroidRuntime(12239):    at android.view.LayoutInflater.inflate(LayoutInflater.java:489)
01-14 18:05:22.301: E/AndroidRuntime(12239):    at android.view.LayoutInflater.inflate(LayoutInflater.java:396)
01-14 18:05:22.301: E/AndroidRuntime(12239):    at android.view.LayoutInflater.inflate(LayoutInflater.java:352)
01-14 18:05:22.301: E/AndroidRuntime(12239):    at com.android.internal.policy.impl.PhoneWindow.setContentView(PhoneWindow.java:281)
01-14 18:05:22.301: E/AndroidRuntime(12239):    at android.app.Activity.setContentView(Activity.java:1881)
01-14 18:05:22.301: E/AndroidRuntime(12239):    at example.androidhive.googlemapsv2.MainActivity.onCreate(MainActivity.java:18)
01-14 18:05:22.301: E/AndroidRuntime(12239):    at android.app.Activity.performCreate(Activity.java:5122)
01-14 18:05:22.301: E/AndroidRuntime(12239):    at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1081)
01-14 18:05:22.301: E/AndroidRuntime(12239):    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2307)
01-14 18:05:22.301: E/AndroidRuntime(12239):    ... 11 more
01-14 18:05:22.301: E/AndroidRuntime(12239): 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
01-14 18:05:22.301: E/AndroidRuntime(12239):    at maps.af.ay.a(Unknown Source)
01-14 18:05:22.301: E/AndroidRuntime(12239):    at maps.af.ay.a(Unknown Source)
01-14 18:05:22.301: E/AndroidRuntime(12239):    at maps.af.al.a(Unknown Source)
01-14 18:05:22.301: E/AndroidRuntime(12239):    at maps.af.be.a(Unknown Source)
01-14 18:05:22.301: E/AndroidRuntime(12239):    at maps.af.bd.a(Unknown Source)
01-14 18:05:22.301: E/AndroidRuntime(12239):    at cmj.onTransact(SourceFile:107)
01-14 18:05:22.301: E/AndroidRuntime(12239):    at android.os.Binder.transact(Binder.java:310)
01-14 18:05:22.301: E/AndroidRuntime(12239):    at com.google.android.gms.maps.internal.IMapFragmentDelegate$a$a.onCreateView(Unknown Source)
01-14 18:05:22.301: E/AndroidRuntime(12239):    at com.google.android.gms.maps.MapFragment$a.onCreateView(Unknown Source)
01-14 18:05:22.301: E/AndroidRuntime(12239):    at com.google.android.gms.dynamic.a$4.b(Unknown Source)
01-14 18:05:22.301: E/AndroidRuntime(12239):    at com.google.android.gms.dynamic.a.a(Unknown Source)
01-14 18:05:22.301: E/AndroidRuntime(12239):    at com.google.android.gms.dynamic.a.onCreateView(Unknown Source)
01-14 18:05:22.301: E/AndroidRuntime(12239):    at com.google.android.gms.maps.MapFragment.onCreateView(Unknown Source)

清单文件:

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

    <uses-sdk
        android:minSdkVersion="14"
        android:targetSdkVersion="18" />
       <permission
        android:name="example.androidhive.googlemapsv2.permission.MAPS_RECEIVE"
        android:protectionLevel="signature" />

    <uses-permission android:name="example.androidhive.googlemapsv2.permission.MAPS_RECEIVE" />
    <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
    <uses-permission android:name="android.permission.INTERNET" />
    <uses-permission android:name="com.google.android.providers.gsf.permission.READ_GSERVICES" />
    <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />

    <!-- Required to show current location -->
    <uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
    <uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />

   <!-- Required OpenGL ES 2.0. for Maps V2 -->
    <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="example.androidhive.googlemapsv2.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" />
    </application>

</manifest>

MainActivity.java

MainActivity.java

package example.androidhive.googlemapsv2;

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

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

public class MainActivity extends Activity{
    private GoogleMap googleMap;
    private final String TAG ="GoogleMapsv2";
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        try {
            initializeMap();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    // Function to load map. If map is not created it will create it for you

    private void initializeMap() {
        Log.d(TAG,"IntializeMap");
        if (googleMap == null) {
            googleMap = ((MapFragment) getFragmentManager().findFragmentById(R.id.map)).getMap();
            // Check if map is created successfully
            if (googleMap == null) {
                Toast.makeText(getApplicationContext(), "Unable to create maps",Toast.LENGTH_SHORT).show();
            }
        }
    }


    @Override
    protected void onResume() {
        // TODO Auto-generated method stub
        super.onResume();
        initializeMap();
    }

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

}

activity_main.xml

activity_main.xml

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

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


</RelativeLayout>

编辑: 当我提出以下更改我的清单文件

Edit : when I make following change in my manifest file

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

另一个错误出现

Another error comes up

01-14 18:28:21.323: W/Bundle(13254): Key com.google.android.gms.version expected Integer but value was a java.lang.String.  The default value 0 was returned.
01-14 18:28:21.355: W/Bundle(13254): Attempt to cast generated internal exception:
01-14 18:28:21.355: W/Bundle(13254): java.lang.ClassCastException: java.lang.String cannot be cast to java.lang.Integer
01-14 18:28:21.355: W/Bundle(13254):    at android.os.Bundle.getInt(Bundle.java:945)
01-14 18:28:21.355: W/Bundle(13254):    at android.os.Bundle.getInt(Bundle.java:927)
01-14 18:28:21.355: W/Bundle(13254):    at com.google.android.gms.common.GooglePlayServicesUtil.n(Unknown Source)
01-14 18:28:21.355: W/Bundle(13254):    at com.google.android.gms.common.GooglePlayServicesUtil.isGooglePlayServicesAvailable(Unknown Source)
01-14 18:28:21.355: W/Bundle(13254):    at com.google.android.gms.maps.internal.q.v(Unknown Source)
01-14 18:28:21.355: W/Bundle(13254):    at com.google.android.gms.maps.internal.q.u(Unknown Source)
01-14 18:28:21.355: W/Bundle(13254):    at com.google.android.gms.maps.MapsInitializer.initialize(Unknown Source)
01-14 18:28:21.355: W/Bundle(13254):    at com.google.android.gms.maps.MapFragment$b.cE(Unknown Source)
01-14 18:28:21.355: W/Bundle(13254):    at com.google.android.gms.maps.MapFragment$b.a(Unknown Source)
01-14 18:28:21.355: W/Bundle(13254):    at com.google.android.gms.dynamic.a.a(Unknown Source)
01-14 18:28:21.355: W/Bundle(13254):    at com.google.android.gms.dynamic.a.onInflate(Unknown Source)
01-14 18:28:21.355: W/Bundle(13254):    at com.google.android.gms.maps.MapFragment.onInflate(Unknown Source)
01-14 18:28:21.355: W/Bundle(13254):    at android.app.Activity.onCreateView(Activity.java:4734)
01-14 18:28:21.355: W/Bundle(13254):    at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:680)
01-14 18:28:21.355: W/Bundle(13254):    at android.view.LayoutInflater.rInflate(LayoutInflater.java:746)
01-14 18:28:21.355: W/Bundle(13254):    at android.view.LayoutInflater.inflate(LayoutInflater.java:489)
01-14 18:28:21.355: W/Bundle(13254):    at android.view.LayoutInflater.inflate(LayoutInflater.java:396)
01-14 18:28:21.355: W/Bundle(13254):    at android.view.LayoutInflater.inflate(LayoutInflater.java:352)
01-14 18:28:21.355: W/Bundle(13254):    at com.android.internal.policy.impl.PhoneWindow.setContentView(PhoneWindow.java:281)
01-14 18:28:21.355: W/Bundle(13254):    at android.app.Activity.setContentView(Activity.java:1881)
01-14 18:28:21.355: W/Bundle(13254):    at example.androidhive.googlemapsv2.MainActivity.onCreate(MainActivity.java:18)
01-14 18:28:21.355: W/Bundle(13254):    at android.app.Activity.performCreate(Activity.java:5122)
01-14 18:28:21.355: W/Bundle(13254):    at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1081)
01-14 18:28:21.355: W/Bundle(13254):    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2307)
01-14 18:28:21.355: W/Bundle(13254):    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2395)
01-14 18:28:21.355: W/Bundle(13254):    at android.app.ActivityThread.access$600(ActivityThread.java:162)
01-14 18:28:21.355: W/Bundle(13254):    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1364)
01-14 18:28:21.355: W/Bundle(13254):    at android.os.Handler.dispatchMessage(Handler.java:107)
01-14 18:28:21.355: W/Bundle(13254):    at android.os.Looper.loop(Looper.java:194)
01-14 18:28:21.355: W/Bundle(13254):    at android.app.ActivityThread.main(ActivityThread.java:5371)
01-14 18:28:21.355: W/Bundle(13254):    at java.lang.reflect.Method.invokeNative(Native Method)
01-14 18:28:21.355: W/Bundle(13254):    at java.lang.reflect.Method.invoke(Method.java:525)
01-14 18:28:21.355: W/Bundle(13254):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:833)
01-14 18:28:21.355: W/Bundle(13254):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:600)
01-14 18:28:21.355: W/Bundle(13254):    at dalvik.system.NativeStart.main(Native Method)
01-14 18:28:21.374: D/AndroidRuntime(13254): Shutting down VM
01-14 18:28:21.374: W/dalvikvm(13254): threadid=1: thread exiting with uncaught exception (group=0x414929a8)
01-14 18:28:21.378: E/AndroidRuntime(13254): FATAL EXCEPTION: main
01-14 18:28:21.378: E/AndroidRuntime(13254): java.lang.RuntimeException: Unable to start activity ComponentInfo{example.androidhive.googlemapsv2/example.androidhive.googlemapsv2.MainActivity}: android.view.InflateException: Binary XML file line #11: Error inflating class fragment
01-14 18:28:21.378: E/AndroidRuntime(13254):    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2343)
01-14 18:28:21.378: E/AndroidRuntime(13254):    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2395)
01-14 18:28:21.378: E/AndroidRuntime(13254):    at android.app.ActivityThread.access$600(ActivityThread.java:162)
01-14 18:28:21.378: E/AndroidRuntime(13254):    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1364)
01-14 18:28:21.378: E/AndroidRuntime(13254):    at android.os.Handler.dispatchMessage(Handler.java:107)
01-14 18:28:21.378: E/AndroidRuntime(13254):    at android.os.Looper.loop(Looper.java:194)
01-14 18:28:21.378: E/AndroidRuntime(13254):    at android.app.ActivityThread.main(ActivityThread.java:5371)
01-14 18:28:21.378: E/AndroidRuntime(13254):    at java.lang.reflect.Method.invokeNative(Native Method)
01-14 18:28:21.378: E/AndroidRuntime(13254):    at java.lang.reflect.Method.invoke(Method.java:525)
01-14 18:28:21.378: E/AndroidRuntime(13254):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:833)
01-14 18:28:21.378: E/AndroidRuntime(13254):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:600)
01-14 18:28:21.378: E/AndroidRuntime(13254):    at dalvik.system.NativeStart.main(Native Method)
01-14 18:28:21.378: E/AndroidRuntime(13254): Caused by: android.view.InflateException: Binary XML file line #11: Error inflating class fragment
01-14 18:28:21.378: E/AndroidRuntime(13254):    at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:704)
01-14 18:28:21.378: E/AndroidRuntime(13254):    at android.view.LayoutInflater.rInflate(LayoutInflater.java:746)
01-14 18:28:21.378: E/AndroidRuntime(13254):    at android.view.LayoutInflater.inflate(LayoutInflater.java:489)
01-14 18:28:21.378: E/AndroidRuntime(13254):    at android.view.LayoutInflater.inflate(LayoutInflater.java:396)
01-14 18:28:21.378: E/AndroidRuntime(13254):    at android.view.LayoutInflater.inflate(LayoutInflater.java:352)
01-14 18:28:21.378: E/AndroidRuntime(13254):    at com.android.internal.policy.impl.PhoneWindow.setContentView(PhoneWindow.java:281)
01-14 18:28:21.378: E/AndroidRuntime(13254):    at android.app.Activity.setContentView(Activity.java:1881)
01-14 18:28:21.378: E/AndroidRuntime(13254):    at example.androidhive.googlemapsv2.MainActivity.onCreate(MainActivity.java:18)
01-14 18:28:21.378: E/AndroidRuntime(13254):    at android.app.Activity.performCreate(Activity.java:5122)
01-14 18:28:21.378: E/AndroidRuntime(13254):    at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1081)
01-14 18:28:21.378: E/AndroidRuntime(13254):    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2307)
01-14 18:28:21.378: E/AndroidRuntime(13254):    ... 11 more
01-14 18:28:21.378: E/AndroidRuntime(13254): 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" />

请帮我找出解决办法。

推荐答案

当你评论@gokhan的答案,你应该做的。

As you commented on @gokhan answer, you should do

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

不过,你仍然需要做的

However, you still need to do

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

其中, APP_KEY

我建议你创建一个 ids.xml 文件中的文件夹中,有这样的事情:

I suggest you to create an ids.xml file inside your values folder and have something like:

<resources>
    <string name="app_id" translatable="false">1234567890</string>
    <string name="app_key" translatable="false">QWERTTYruyfgREYT</string>
    <string name="facebookAppId" translatable="false"></string>