0

我是谷歌地图的新手,因此很多事情对我来说还不够清楚。所以我期待一些专家在这方面的指导。我阅读了本指南并做了它所说的一切。我使用谷歌地图 api v2 并得到一个地图键。然后我按照该链接创建一个示例项目并选择目标版本 android 2.2(在运行项目时)。但是在运行 eclipse android 模拟器中的示例后出现错误列表。我的logcat视图显示:

01-31 00:51:59.226: W/dalvikvm(329): threadid=1: thread exiting with uncaught exception (group=0x4001d800)
01-31 00:51:59.236: E/AndroidRuntime(329): FATAL EXCEPTION: main
01-31 00:51:59.236: E/AndroidRuntime(329): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.googleMapApp/com.googleMapApp.LocationProfileManagerActivity}: android.view.InflateException: Binary XML file line #7: Error inflating class fragment
01-31 00:51:59.236: E/AndroidRuntime(329):  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2663)
01-31 00:51:59.236: E/AndroidRuntime(329):  at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2679)
01-31 00:51:59.236: E/AndroidRuntime(329):  at android.app.ActivityThread.access$2300(ActivityThread.java:125)
01-31 00:51:59.236: E/AndroidRuntime(329):  at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2033)
01-31 00:51:59.236: E/AndroidRuntime(329):  at android.os.Handler.dispatchMessage(Handler.java:99)
01-31 00:51:59.236: E/AndroidRuntime(329):  at android.os.Looper.loop(Looper.java:123)
01-31 00:51:59.236: E/AndroidRuntime(329):  at android.app.ActivityThread.main(ActivityThread.java:4627)
01-31 00:51:59.236: E/AndroidRuntime(329):  at java.lang.reflect.Method.invokeNative(Native Method)
01-31 00:51:59.236: E/AndroidRuntime(329):  at java.lang.reflect.Method.invoke(Method.java:521)
01-31 00:51:59.236: E/AndroidRuntime(329):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868)
01-31 00:51:59.236: E/AndroidRuntime(329):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626)
01-31 00:51:59.236: E/AndroidRuntime(329):  at dalvik.system.NativeStart.main(Native Method)
01-31 00:51:59.236: E/AndroidRuntime(329): Caused by: android.view.InflateException: Binary XML file line #7: Error inflating class fragment
01-31 00:51:59.236: E/AndroidRuntime(329):  at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:576)
01-31 00:51:59.236: E/AndroidRuntime(329):  at android.view.LayoutInflater.rInflate(LayoutInflater.java:618)
01-31 00:51:59.236: E/AndroidRuntime(329):  at android.view.LayoutInflater.inflate(LayoutInflater.java:407)
01-31 00:51:59.236: E/AndroidRuntime(329):  at android.view.LayoutInflater.inflate(LayoutInflater.java:320)
01-31 00:51:59.236: E/AndroidRuntime(329):  at android.view.LayoutInflater.inflate(LayoutInflater.java:276)
01-31 00:51:59.236: E/AndroidRuntime(329):  at com.android.internal.policy.impl.PhoneWindow.setContentView(PhoneWindow.java:198)
01-31 00:51:59.236: E/AndroidRuntime(329):  at android.app.Activity.setContentView(Activity.java:1647)
01-31 00:51:59.236: E/AndroidRuntime(329):  at com.googleMapApp.LocationProfileManagerActivity.onCreate(LocationProfileManagerActivity.java:14)
01-31 00:51:59.236: E/AndroidRuntime(329):  at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
01-31 00:51:59.236: E/AndroidRuntime(329):  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2627)
01-31 00:51:59.236: E/AndroidRuntime(329):  ... 11 more
01-31 00:51:59.236: E/AndroidRuntime(329): Caused by: java.lang.ClassNotFoundException: android.view.fragment in loader dalvik.system.PathClassLoader[/data/app/com.googleMapApp-1.apk]
01-31 00:51:59.236: E/AndroidRuntime(329):  at dalvik.system.PathClassLoader.findClass(PathClassLoader.java:243)
01-31 00:51:59.236: E/AndroidRuntime(329):  at java.lang.ClassLoader.loadClass(ClassLoader.java:573)
01-31 00:51:59.236: E/AndroidRuntime(329):  at java.lang.ClassLoader.loadClass(ClassLoader.java:532)
01-31 00:51:59.236: E/AndroidRuntime(329):  at android.view.LayoutInflater.createView(LayoutInflater.java:466)
01-31 00:51:59.236: E/AndroidRuntime(329):  at android.view.LayoutInflater.onCreateView(LayoutInflater.java:544)
01-31 00:51:59.236: E/AndroidRuntime(329):  at com.android.internal.policy.impl.PhoneLayoutInflater.onCreateView(PhoneLayoutInflater.java:66)
01-31 00:51:59.236: E/AndroidRuntime(329):  at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:563)

我的 Manifest.xml是:

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

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

    <permission
    android:name="com.googleMapApp.permission.MAPS_RECEIVE"
    android:protectionLevel="signature"/>
    <uses-permission android:name="com.googleMapApp.permission.MAPS_RECEIVE"/>
    <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.WRITE_EXTERNAL_STORAGE"/>
    <uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION"/>
    <uses-permission android:name="android.permission.ACCESS_FINE_LOCATION"/>

<!-- Require OpenGL ES version 2 -->
     <uses-feature
      android:glEsVersion="0x00020000"
      android:required="true"/>

    <application
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name" >
        <activity
            android:name=".LocationProfileManagerActivity"
            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.maps.v2.API_KEY"
        android:value="myMapKey"/>
    </application>

</manifest>

我的main.xml是:

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

我的activity.java是:

package com.googleMapApp;

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


public class LocationProfileManagerActivity extends Activity {
    /** Called when the activity is first created. */
    //private GoogleMap map;
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        //map = ((MapFragment) getFragmentManager().findFragmentById(R.id.map)).getMap();

     }
}

很抱歉这个冗长的描述,但我想告诉你我做了什么。我认为作为一个新手,我在这种方式上犯了很多错误。所以我希望有人会指导我。

4

3 回答 3

3

您正在使用仅在以后的 API 中可用的功能,即Fragment. 如果要编译支持 2.2,则需要在项目中包含支持库。您还必须进行以下更改以使用支持片段而不是常规片段 -

在 main.xml 中,更改

class="com.google.android.gms.maps.MapFragment"/>

class="com.google.android.gms.maps.SupportMapFragment"/>

在你的activity.java中,改变

public class LocationProfileManagerActivity extends Activity {

public class LocationProfileManagerActivity extends FragmentActivity {
于 2013-01-30T18:30:57.207 回答
1

Maps API v2 无法在模拟器中运行,因为它依赖于捆绑在 Google Play 商店 .apk 中的 Google Play 服务。不过,它可以一起被破解,你可以在这里阅读关于“谷歌地图 v2 模拟器”的第一个谷歌结果。此外,您仍然可以在有限的时间内使用 V1,您可以从这里开始。

于 2013-01-30T18:29:39.410 回答
1

你需要做两个改变

由于您的 sdk 版本低于 12,因此需要进行以下更改

1) main.xml

代替

  class="com.google.android.gms.maps.MapFragment"/>

class="com.google.android.gms.maps.SupportMapFragment"/>

2)活动.java

需要扩展FragmentActivity而不是Activity

如果您想在模拟器中进行测试,请下载google_play_services_apk并将其安装在模拟器中。

于 2013-01-30T18:43:34.827 回答