我是谷歌地图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"/>
你有任何想法解决这个问题吗?
P.S。抱歉我的英语不好......
答案 0 :(得分:4)
因为您编写了面向SDK 9的应用程序,并且在XML文件中使用了SupportMapFragment
(如您所愿)。然后,您必须使用getSupportFragmentManager
和SupportMapFragment
方法。
就像那样:
map = ((SupportMapFragment) getSupportFragmentManager().findFragmentById(R.id.map)).getMap();
而不是:
mMap = ((MapFragment) getFragmentManager().findFragmentById(R.id.map)).getMap();
答案 1 :(得分:0)
看起来它无法找到地图API。您是否下载并配置了Google Play服务SDK:https://developers.google.com/maps/documentation/android/start#the_google_maps_api_key ?
答案 2 :(得分:0)
使用getSupportFragmentManager()
代替getFragmentManager()
。后者仅在Honecomb(和你的minsdk是GingerBread)之后定义,而前者在支持库中定义(我看到你在扩展FragmentActivity
时使用)。
这就是为什么你必须添加@TargetApi
和@SupressLint
... ADT警告你并且你让它沉默...... =)你应该删除它们。
答案 3 :(得分:0)
在xml文件中使用android:name =&#34; com.google.android.gms.maps.SupportMapFragment&#34;代替 类=&#34; com.google.android.gms.maps.SupportMapFragment&#34; 让我知道它的工作