2

我是 Google Maps API 的新手,我正在尝试在我的应用程序中显示地图。每次尝试都失败了,我不明白为什么。

这里是logcat。

<!-- language: c# --> 04-02 02:03:21.259: W/dalvikvm(24325): VFY: unable to resolve virtual method 3693: Lcom/kurtis/myapp/Mapexample;.getFragmentManager ()Landroid/app/FragmentManager;
04-02 02:03:21.269: W/dalvikvm(24325): VFY: unable to resolve virtual method 3693: Lcom/kurtis/myapp/Mapexample;.getFragmentManager ()Landroid/app/FragmentManager;
04-02 02:03:21.279: W/dalvikvm(24325): VFY: unable to resolve instance field 23
04-02 02:03:21.419: W/dalvikvm(24325): Unable to resolve superclass of Lmaps/p/s; (425)
04-02 02:03:21.429: W/dalvikvm(24325): Link of class 'Lmaps/p/s;' failed
04-02 02:03:21.429: W/dalvikvm(24325): Unable to resolve superclass of Lmaps/y/bo; (3818)
04-02 02:03:21.429: W/dalvikvm(24325): Link of class 'Lmaps/y/bo;' failed
04-02 02:03:21.429: W/dalvikvm(24325): Unable to resolve superclass of Lmaps/i/k; (4206)
04-02 02:03:21.429: W/dalvikvm(24325): Link of class 'Lmaps/i/k;' failed
04-02 02:03:21.429: E/dalvikvm(24325): Could not find class 'maps.i.k', referenced from method maps.z.ag.a
04-02 02:03:21.429: W/dalvikvm(24325): VFY: unable to resolve new-instance 3538 (Lmaps/i/k;) in Lmaps/z/ag;
04-02 02:03:21.979: W/dalvikvm(24325): threadid=1: thread exiting with uncaught exception (group=0x40174560)
04-02 02:03:21.979: E/AndroidRuntime(24325): FATAL EXCEPTION: main
04-02 02:03:21.979: E/AndroidRuntime(24325): java.lang.NoSuchMethodError: com.kurtis.myapp.Mapexample.getFragmentManager
04-02 02:03:21.979: E/AndroidRuntime(24325):    at com.kurtis.myapp.Mapexample.onCreate(Mapexample.java:26)
04-02 02:03:21.979: E/AndroidRuntime(24325):    at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
04-02 02:03:21.979: E/AndroidRuntime(24325):    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1623)
04-02 02:03:21.979: E/AndroidRuntime(24325):    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1675)
04-02 02:03:21.979: E/AndroidRuntime(24325):    at android.app.ActivityThread.access$1500(ActivityThread.java:121)
04-02 02:03:21.979: E/AndroidRuntime(24325):    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:943)
04-02 02:03:21.979: E/AndroidRuntime(24325):    at android.os.Handler.dispatchMessage(Handler.java:99)
04-02 02:03:21.979: E/AndroidRuntime(24325):    at android.os.Looper.loop(Looper.java:130)
04-02 02:03:21.979: E/AndroidRuntime(24325):    at android.app.ActivityThread.main(ActivityThread.java:3768)
04-02 02:03:21.979: E/AndroidRuntime(24325):    at java.lang.reflect.Method.invokeNative(Native Method)
04-02 02:03:21.979: E/AndroidRuntime(24325):    at java.lang.reflect.Method.invoke(Method.java:507)
04-02 02:03:21.979: E/AndroidRuntime(24325):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:878)
04-02 02:03:21.979: E/AndroidRuntime(24325):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:636)
04-02 02:03:21.979: E/AndroidRuntime(24325):    at dalvik.system.NativeStart.main(Native Method)

显现:

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

    <uses-sdk android:minSdkVersion="9"
                android:targetSdkVersion="16" />
  <uses-permission android:name="android.permission.INTERNET" />
  <uses-permission android:name="android.permission.CAMERA" />
  <uses-feature android:name="android.hardware.camera" 
                android:required="false"/>
  <uses-feature android:name="android.hardware.camera.any"
                android:required="false" />

  <permission android:name="com.kurtis.myapp.permission.MAPS_RECEIVE" android:protectionLevel="signature"/>
  <uses-permission android:name="com.kurtis.myapp.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:icon="@drawable/icona"
        android:label="@string/app_name" >
        <activity
            android:name="com.kurtis.myapp.Home"
            android:label="@string/app_name"
            android:screenOrientation="portrait" >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />
                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>

        <activity
            android:name="com.kurtis.myapp.Mapexample"
            android:theme="@android:style/Theme.NoTitleBar.Fullscreen"
            android:screenOrientation="portrait" >
        </activity>

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

</manifest>

活动:

package com.kurtis.myapp;

import com.example.android.R;
import com.google.android.gms.maps.GoogleMap;
import com.google.android.gms.maps.MapFragment;
import com.google.android.gms.maps.SupportMapFragment;

import android.annotation.SuppressLint;
import android.annotation.TargetApi;
import android.os.Build;
import android.os.Bundle;
import android.support.v4.app.FragmentActivity;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;

@SuppressLint("NewApi")
@TargetApi(Build.VERSION_CODES.HONEYCOMB)
public class Mapexample extends FragmentActivity {
    private GoogleMap mMap;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.mapexample);

        mMap = ((MapFragment) getFragmentManager().findFragmentById(R.id.map)).getMap();
    }


    private void setUpMapIfNeeded() {
        // Do a null check to confirm that we have not already instantiated the map.
        if (mMap == null) {
            mMap = ((MapFragment) getFragmentManager().findFragmentById(R.id.map)).getMap();
            // Check if we were successful in obtaining the map.
            if (mMap != null) {
                // The Map is verified. It is now safe to manipulate the map.

            }
        }
    }
}

...和布局:

<?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.SupportMapFragment"/>

你有解决这个问题的想法吗?

PS对不起我的英语不好...

4

4 回答 4

4

因为您编写了面向 SDK 9 的应用程序,并且SupportMapFragment在您的 XML 文件中使用(应该如此)。然后你必须使用getSupportFragmentManagerSupportMapFragment方法。

像那样:

map = ((SupportMapFragment) getSupportFragmentManager().findFragmentById(R.id.map)).getMap();

而不是这个:

mMap = ((MapFragment) getFragmentManager().findFragmentById(R.id.map)).getMap();
于 2013-04-02T07:06:30.217 回答
0

看起来它无法找到地图 API。您是否下载并配置了 Google Play 服务 SDK:https ://developers.google.com/maps/documentation/android/start#the_google_maps_api_key ?

于 2013-04-02T03:38:17.213 回答
0

使用getSupportFragmentManager()而不是getFragmentManager(). 后者仅在 Honecomb 之后定义(并且您的 minsdk 是 GingerBread),而前者在支持库中定义(我看到您在扩展时使用它FragmentActivity)。

这就是为什么您必须添加@TargetApi@SupressLint... ADT 警告您并且您将其静音... =) 您应该删除它们。

于 2013-04-02T06:21:04.220 回答
0

在 xml 文件中使用 android:name="com.google.android.gms.maps.SupportMapFragment" 而不是 class="com.google.android.gms.maps.SupportMapFragment" 让我知道它正在工作

于 2014-06-11T11:06:59.300 回答