问题描述,请先阅读此内容
我正在尝试获取我的简单应用程序,它将显示在从 Android 2.3 到 Android 4.0+ 设备的设备上运行的用户位置。地图不会显示在真正的 Android 2.3 设备上。
该应用程序目前可以在 Android 4.0+ 设备上按照我的需要运行和显示地图,没有任何问题。
因为它可以在Android 4.0+上毫无问题地显示地图,所以我认为问题应该不是谷歌地图V2的设置或权限有什么问题。
但是,当我在 Android 2.3 设备上尝试时,地图区域将是空白的,如下图所示:
该应用程序使用 ActionBarSherlock、Google Maps API v2 和 SupportMapFragment。针对 SDK 17 构建,最低 SDK 10。
代码片段
我粘贴并修剪了代码以显示唯一相关的部分,如果您需要其他任何内容,请发表评论。
活动布局.xml
<fragment
android:id="@+id/map"
android:name="com.google.android.gms.maps.SupportMapFragment"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_margin="@dimen/incident_padding_right" />
活动.java
import com.actionbarsherlock.app.SherlockFragmentActivity;
import com.google.android.gms.maps.GoogleMap;
import com.google.android.gms.maps.GoogleMap.OnMarkerDragListener;
import com.google.android.gms.maps.SupportMapFragment;
public class Activity extends SherlockFragmentActivity{
private static GoogleMap map;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
this.setContentView(R.layout.activity_layout);
............
//Set up map view
map = ((SupportMapFragment)getSupportFragmentManager().findFragmentById(R.id.map)).getMap();
............
OnMarkerDragListener markerDragListener = new OnMarkerDragListener() {
@Override
public void onMarkerDragStart(Marker marker) {
// Called when the marker drag is started
}
@Override
public void onMarkerDragEnd(Marker marker) {
// Called when the marker is dropped down.
coords[0] = marker.getPosition().latitude;
coords[1] = marker.getPosition().longitude;
RestoreUIwithSavedLocation(coords);
Log.d(TAG, "Pin Dropped at: " + coords[0] + ", " + coords[1]);
}
@Override
public void onMarkerDrag(Marker marker) {
}
};
map.setOnMarkerDragListener(markerDragListener);
}
在真正的 Android 2.3 设备上运行时的 Logcat:
01-04 21:16:52.020: D/dalvikvm(19074): VFY: dead code 0x0014-0018 in Lcom/google/android/gms/common/GooglePlayServicesUtil;.b (Landroid/content/res/Resources;)Z
01-04 21:16:52.150: W/dalvikvm(19074): Unable to resolve superclass of Lmaps/p/s; (427)
01-04 21:16:52.150: W/dalvikvm(19074): Link of class 'Lmaps/p/s;' failed
01-04 21:16:52.150: W/dalvikvm(19074): Unable to resolve superclass of Lmaps/y/bo; (3820)
01-04 21:16:52.150: W/dalvikvm(19074): Link of class 'Lmaps/y/bo;' failed
01-04 21:16:52.150: W/dalvikvm(19074): Unable to resolve superclass of Lmaps/i/k; (4208)
01-04 21:16:52.150: W/dalvikvm(19074): Link of class 'Lmaps/i/k;' failed
01-04 21:16:52.150: E/dalvikvm(19074): Could not find class 'maps.i.k', referenced from method maps.z.ag.a
01-04 21:16:52.150: W/dalvikvm(19074): VFY: unable to resolve new-instance 3540 (Lmaps/i/k;) in Lmaps/z/ag;
01-04 21:16:52.150: D/dalvikvm(19074): VFY: replacing opcode 0x22 at 0x006d
01-04 21:16:52.160: D/dalvikvm(19074): VFY: dead code 0x006f-007f in Lmaps/z/ag;.a (Landroid/view/LayoutInflater;Lcom/google/android/gms/maps/GoogleMapOptions;ZLjava/lang/String;)Lmaps/z/ag;
使用 Google API Lv.10 在模拟器上运行时的 logcat
03-25 13:00:47.974: W/GooglePlayServicesUtil(751): Google Play services is missing.
03-25 13:00:47.992: W/GooglePlayServicesUtil(751): Google Play services is missing.
03-25 13:00:47.992: W/GooglePlayServicesUtil(751): Google Play services is missing.
03-25 13:00:48.002: W/GooglePlayServicesUtil(751): Google Play services is missing.
03-25 13:00:48.002: W/GooglePlayServicesUtil(751): Google Play services is missing.
03-25 13:00:48.022: W/GooglePlayServicesUtil(751): Google Play services is missing.
03-25 13:00:48.072: D/AndroidRuntime(751): Shutting down VM
03-25 13:00:48.072: W/dalvikvm(751): threadid=1: thread exiting with uncaught exception (group=0x40015560)
03-25 13:00:48.133: E/AndroidRuntime(751): FATAL EXCEPTION: main
03-25 13:00:48.133: E/AndroidRuntime(751): java.lang.RuntimeException: Unable to start activity ComponentInfo{Activity}: java.lang.NullPointerException
03-25 13:00:48.133: E/AndroidRuntime(751): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1647)
03-25 13:00:48.133: E/AndroidRuntime(751): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1663)
03-25 13:00:48.133: E/AndroidRuntime(751): at android.app.ActivityThread.access$1500(ActivityThread.java:117)
03-25 13:00:48.133: E/AndroidRuntime(751): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:931)
03-25 13:00:48.133: E/AndroidRuntime(751): at android.os.Handler.dispatchMessage(Handler.java:99)
03-25 13:00:48.133: E/AndroidRuntime(751): at android.os.Looper.loop(Looper.java:130)
03-25 13:00:48.133: E/AndroidRuntime(751): at android.app.ActivityThread.main(ActivityThread.java:3683)
03-25 13:00:48.133: E/AndroidRuntime(751): at java.lang.reflect.Method.invokeNative(Native Method)
03-25 13:00:48.133: E/AndroidRuntime(751): at java.lang.reflect.Method.invoke(Method.java:507)
03-25 13:00:48.133: E/AndroidRuntime(751): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839)
03-25 13:00:48.133: E/AndroidRuntime(751): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597)
03-25 13:00:48.133: E/AndroidRuntime(751): at dalvik.system.NativeStart.main(Native Method)
03-25 13:00:48.133: E/AndroidRuntime(751): Caused by: java.lang.NullPointerException
03-25 13:00:48.133: E/AndroidRuntime(751): at Activity.onCreate(Activity.java:250)
03-25 13:00:48.133: E/AndroidRuntime(751): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
03-25 13:00:48.133: E/AndroidRuntime(751): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1611)
03-25 13:00:48.133: E/AndroidRuntime(751): ... 11 more
Activity.onCreate(Activity.java:250)
指向这一行:
map.setOnMarkerDragListener(markerDragListener);
我试过的
奇怪的是,如果我添加这一行
<uses-library android:name="com.google.android.maps" />
到AndroidManifest.xml
,logcat 将不显示任何内容(没有错误没有警告),并且地图仍然是空白的。但是有人发帖说这条线解决了她/他的空白地图问题:https ://stackoverflow.com/questions/15595357/android-supportmapfragment-google-play-services-issue
在我的项目Android Dependencies
文件夹中,我有这些*.jar
文件:
actionbar-sherlock-4.2-library.jar
google-play-services_lib.jar
android-support-v4.jar
actionbarsherlock-plugin-maps-4.2.0.jar
google-play-services.jar
从这篇文章的回答中,我知道谷歌地图可能无法在模拟器上运行良好。我所需要的只是让地图显示在我失败的真正的 Android 2.3 设备上。
此外,我也密切关注这些帖子并尝试了他们的方法(如果适用于我的情况),但仍然对我的问题不满意:
如何在 API 级别 10 中使用 Google Maps API v2?
Google MapFragment 为空白(白色),找不到类“maps.jk”
将 ActionBarSherlock 与新的 SupportMapFragment 一起使用
如果您需要更多信息,请在下面发表评论,我会尝试添加它们。