0

我正在使用 android API v2 处理地图视图,我正在关注本教程,我已经按照他在教程中解释的内容进行操作,但我仍然遇到致命异常。这是我的带有日志猫的 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:layout_width="match_parent"
        android:layout_height="match_parent"
        class="com.google.android.gms.maps.MapFragment" />

</RelativeLayout>

日志猫:

05-08 10:38:22.121: E/AndroidRuntime(954): FATAL EXCEPTION: main
05-08 10:38:22.121: E/AndroidRuntime(954): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.map/com.example.map.MainActivity}: android.view.InflateException: Binary XML file line #11: Error inflating class fragment
05-08 10:38:22.121: E/AndroidRuntime(954):  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2180)
05-08 10:38:22.121: E/AndroidRuntime(954):  at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2230)
05-08 10:38:22.121: E/AndroidRuntime(954):  at android.app.ActivityThread.access$600(ActivityThread.java:141)
05-08 10:38:22.121: E/AndroidRuntime(954):  at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1234)
05-08 10:38:22.121: E/AndroidRuntime(954):  at android.os.Handler.dispatchMessage(Handler.java:99)
05-08 10:38:22.121: E/AndroidRuntime(954):  at android.os.Looper.loop(Looper.java:137)
05-08 10:38:22.121: E/AndroidRuntime(954):  at android.app.ActivityThread.main(ActivityThread.java:5041)
05-08 10:38:22.121: E/AndroidRuntime(954):  at java.lang.reflect.Method.invokeNative(Native Method)
05-08 10:38:22.121: E/AndroidRuntime(954):  at java.lang.reflect.Method.invoke(Method.java:511)
05-08 10:38:22.121: E/AndroidRuntime(954):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
05-08 10:38:22.121: E/AndroidRuntime(954):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)
05-08 10:38:22.121: E/AndroidRuntime(954):  at dalvik.system.NativeStart.main(Native Method)
05-08 10:38:22.121: E/AndroidRuntime(954): Caused by: android.view.InflateException: Binary XML file line #11: Error inflating class fragment
05-08 10:38:22.121: E/AndroidRuntime(954):  at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:704)
05-08 10:38:22.121: E/AndroidRuntime(954):  at android.view.LayoutInflater.rInflate(LayoutInflater.java:746)
05-08 10:38:22.121: E/AndroidRuntime(954):  at android.view.LayoutInflater.inflate(LayoutInflater.java:489)
05-08 10:38:22.121: E/AndroidRuntime(954):  at android.view.LayoutInflater.inflate(LayoutInflater.java:396)
05-08 10:38:22.121: E/AndroidRuntime(954):  at android.view.LayoutInflater.inflate(LayoutInflater.java:352)
05-08 10:38:22.121: E/AndroidRuntime(954):  at com.android.internal.policy.impl.PhoneWindow.setContentView(PhoneWindow.java:270)
05-08 10:38:22.121: E/AndroidRuntime(954):  at android.app.Activity.setContentView(Activity.java:1881)
05-08 10:38:22.121: E/AndroidRuntime(954):  at com.example.map.MainActivity.onCreate(MainActivity.java:12)
05-08 10:38:22.121: E/AndroidRuntime(954):  at android.app.Activity.performCreate(Activity.java:5104)
05-08 10:38:22.121: E/AndroidRuntime(954):  at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1080)
05-08 10:38:22.121: E/AndroidRuntime(954):  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2144)
05-08 10:38:22.121: E/AndroidRuntime(954):  ... 11 more
05-08 10:38:22.121: E/AndroidRuntime(954): Caused by: android.app.Fragment$InstantiationException: Unable to instantiate fragment com.google.android.gms.maps.MapFragment: make sure class name exists, is public, and has an empty constructor that is public
05-08 10:38:22.121: E/AndroidRuntime(954):  at android.app.Fragment.instantiate(Fragment.java:592)
05-08 10:38:22.121: E/AndroidRuntime(954):  at android.app.Fragment.instantiate(Fragment.java:560)
05-08 10:38:22.121: E/AndroidRuntime(954):  at android.app.Activity.onCreateView(Activity.java:4709)
05-08 10:38:22.121: E/AndroidRuntime(954):  at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:680)
05-08 10:38:22.121: E/AndroidRuntime(954):  ... 21 more
05-08 10:38:22.121: E/AndroidRuntime(954): Caused by: java.lang.ClassNotFoundException: Didn't find class "com.google.android.gms.maps.MapFragment" on path: /data/app/com.example.map-2.apk
05-08 10:38:22.121: E/AndroidRuntime(954):  at dalvik.system.BaseDexClassLoader.findClass(BaseDexClassLoader.java:65)
05-08 10:38:22.121: E/AndroidRuntime(954):  at java.lang.ClassLoader.loadClass(ClassLoader.java:501)
05-08 10:38:22.121: E/AndroidRuntime(954):  at java.lang.ClassLoader.loadClass(ClassLoader.java:461)
05-08 10:38:22.121: E/AndroidRuntime(954):  at android.app.Fragment.instantiate(Fragment.java:582)
05-08 10:38:22.121: E/AndroidRuntime(954):  ... 24 more

我的清单看起来像这样。

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

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

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

    <uses-feature
        android:glEsVersion="0x00020000"
        android:required="true" />
    <uses-permission android:name="com.example.map.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.map.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.maps.v2.API_KEY"
            android:value="AIzaSyDSW0CfwDN0LyrF8PBeihEjrAkj25TDhUQ" />
    </application>

</manifest>
4

6 回答 6

1

You are trying to import existing project into library. You should be importing the library using following method.

  1. Right click package explorer. Import -> Expand Android -> Existing Android Code Into Workspace.
  2. Select the google-play-services library. And check the copy to workspace checkbox.

Once this is done. Reference the library project

Right click on the project. Select Android in the left pane. In the right pane bottom you would see a frame with label Library. Click on add and add the imported library.

To add support library to your project: Right click on the project Androids Tools -> Add Support library.

If you decide to use support libraries you will have to replace the code

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

with

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

also in your java file you will have to use SupportMapFragment.

mMap = ((SupportMapFragment) getSupportFragmentManager()
                    .findFragmentById(R.id.map)).getMap();
于 2013-05-08T14:46:56.600 回答
1

确保您在项目构建目标中使用 Google API。转到项目属性> Android 进行检查。

于 2013-05-08T10:53:25.753 回答
0
""Caused by: java.lang.ClassNotFoundException: Didn't find class"com.google.android.gms.maps.MapFragment" on path: /data/app/com.example.map-2.apk""

你检查过这个吗?

我认为你应该在你的 AndroidManifest.XML 中添加这个 (com.google.android.gms.maps.MapFragment)ACTIVITY。

尝试一下 LogCat 中的三个“由...引起”的错误。

于 2013-05-08T11:11:23.367 回答
0

您是否在 Eclipse 中使用了正确的 API(GOOGLE API)?如果您想在 android 中使用谷歌地图,请务必使用 GOOGLE API!

于 2013-05-08T10:50:34.920 回答
0

您可能遇到了导入问题。只需右键单击project -> properties -> Java build path您已导入库即可。为了清楚起见,请参阅此链接

于 2013-05-08T10:55:14.733 回答
0

此错误通常源于对google-play-services库的不正确引用。

要正确执行此操作,请按照我在您的应用程序中集成 Google Maps API V2 所写的这篇博文指南的前 3 个步骤操作:

谷歌地图 API V2

于 2013-05-08T10:58:38.073 回答