1

我正在处理片段以显示谷歌地图。但是我在显示地图时总是从应用程序中崩溃。我已经在库中有 android-support-v4 和 google-play-services。这是我的活动的代码:

package com.example.attijaribank2; 
import android.app.Activity;
import android.os.Bundle;
import android.support.v4.app.FragmentActivity;
import android.view.Menu;

import com.google.android.gms.maps.CameraUpdateFactory;
import com.google.android.gms.maps.GoogleMap;
import com.google.android.gms.maps.SupportMapFragment;
import com.google.android.gms.maps.MapFragment;
import com.google.android.gms.maps.model.BitmapDescriptorFactory;
import com.google.android.gms.maps.model.LatLng;
import com.google.android.gms.maps.model.Marker;
import com.google.android.gms.maps.model.MarkerOptions;

public class Votresolde extends FragmentActivity {
static final LatLng HAMBURG = new LatLng(53.558, 9.927);
static final LatLng KIEL = new LatLng(53.551, 9.993);
private GoogleMap map;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_solde);
map =         ((SupportMapFragment)getSupportFragmentManager().findFragmentById(R.id.map)).getMap();
Marker hamburg = map.addMarker(new MarkerOptions().position(HAMBURG)
        .title("Hamburg"));
    Marker kiel = map.addMarker(new MarkerOptions()
        .position(KIEL)
        .title("Kiel")
        .snippet("Kiel is cool")
        .icon(BitmapDescriptorFactory
            .fromResource(R.drawable.ic_launcher)));


    map.moveCamera(CameraUpdateFactory.newLatLngZoom(HAMBURG, 15));


    map.animateCamera(CameraUpdateFactory.zoomTo(10), 2000, null);
    map.moveCamera(CameraUpdateFactory.newLatLngZoom(HAMBURG, 15));


    map.animateCamera(CameraUpdateFactory.zoomTo(10), 2000, null);    
}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.activity_solde, menu);
return true;
}

}

我究竟做错了什么 ?还是关于 xml 文件?

这是 xml 文件:

<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"/>

和 logcat :

03-02 17:38:38.195: D/dalvikvm(372): DexOpt: couldn't find field     
Landroid/content/res/Configuration;.smallestScreenWidthDp
03-02 17:38:38.195: W/dalvikvm(372): VFY: unable to resolve instance field 23
03-02 17:38:38.195: D/dalvikvm(372): VFY: replacing opcode 0x52 at 0x0012
03-02 17:38:38.195: D/dalvikvm(372): VFY: dead code 0x0014-0018 in   
Lcom/google/android/gms/common/GooglePlayServicesUtil;.b   
(Landroid/content/res/Resources;)Z

W/GooglePlayServicesUtil(372): Google Play services is missing.
W/GooglePlayServicesUtil(372): Google Play services is missing.
03-02 17:38:38.295: D/AndroidRuntime(372): Shutting down VM
03-02 17:38:38.295: W/dalvikvm(372): threadid=1: thread exiting with uncaught exception   
(group=0x40015560)
03-02 17:38:38.305: E/AndroidRuntime(372): FATAL EXCEPTION: main
E/AndroidRuntime(372): java.lang.RuntimeException: Unable to start activity  
ComponentInfo{com.example.attijaribank2/com.example.attijaribank2.Votresolde}:   
java.lang.NullPointerException
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1647)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1663)
at android.app.ActivityThread.access$1500(ActivityThread.java:117)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:931)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:130)
at android.app.ActivityThread.main(ActivityThread.java:3683)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:507)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597)
at dalvik.system.NativeStart.main(Native Method)
Caused by: java.lang.NullPointerException
at com.example.attijaribank2.Votresolde.onCreate(Votresolde.java:26)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1611)

它说缺少谷歌播放服务,但我确实导入了它。

4

2 回答 2

2

缺少 Google Play 服务。

这意味着您要么在未安装 Google Play 的模拟器上进行测试

请查看官方链接以澄清您的疑问,其中显示:

Note: Google Play services is not supported on the Android emulator — to develop using the APIs, you need to provide a development device such as an Android phone or tablet. 

http://developer.android.com/google/play-services/setup.html

于 2013-03-03T05:12:17.450 回答
0

我找到了这个答案,解决了我的问题: https ://stackoverflow.com/a/13719616/1827048

.

特别是这部分对我有用:

a) 将目录 ANDROID_SDK_DIR/extras/google/google_play_services/libproject/google-play-services_lib 复制到项目的根目录

b) 将下一行添加到 YOUR_PROJECT/project.properties:

android.library.reference.1=google-play-services_lib

c) 将下一行添加到 YOUR_PROJECT/proguard-project.txt:

keep class * extends java.util.ListResourceBundle {
protected Object[][] getContents();
}

注意:如果您使用 min SDK = 8 创建应用程序,请使用 android 支持库 v4 + SupportMapFragment 而不是 MapFragment。

于 2013-04-08T12:15:33.377 回答