我正在尝试使用SupportFragmentManager在Android上使用谷歌地图。 它在我的智能手机4.3上运行良好,但是当我在具有google api(API 17,18,22)等目标的模拟器上运行时,它会崩溃并显示以下异常:
06-04 23:22:26.662: W/GooglePlayServicesUtil(2844): Google Play services
out of date. Requires 7571000 but found 6774470
06-04 23:22:26.676: D/AndroidRuntime(2844): Shutting down VM
06-04 23:22:26.676: E/AndroidRuntime(2844): FATAL EXCEPTION: main
06-04 23:22:26.676: E/AndroidRuntime(2844): Process: com.example.radaroc, PID: 2844
06-04 23:22:26.676: E/AndroidRuntime(2844): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.radaroc/com.example.radaroc.SupportMapActivity}: java.lang.NullPointerException: Attempt to invoke virtual method 'void com.google.android.gms.maps.GoogleMap.setOnMapClickListener(com.google.android.gms.maps.GoogleMap$OnMapClickListener)' on a null object reference
06-04 23:22:26.676: E/AndroidRuntime(2844): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2325)
06-04 23:22:26.676: E/AndroidRuntime(2844): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2390)
06-04 23:22:26.676: E/AndroidRuntime(2844): at android.app.ActivityThread.access$800(ActivityThread.java:151)
06-04 23:22:26.676: E/AndroidRuntime(2844): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1303)
06-04 23:22:26.676: E/AndroidRuntime(2844): at android.os.Handler.dispatchMessage(Handler.java:102)
06-04 23:22:26.676: E/AndroidRuntime(2844): at android.os.Looper.loop(Looper.java:135)
06-04 23:22:26.676: E/AndroidRuntime(2844): at android.app.ActivityThread.main(ActivityThread.java:5257)
06-04 23:22:26.676: E/AndroidRuntime(2844): at java.lang.reflect.Method.invoke(Native Method)
06-04 23:22:26.676: E/AndroidRuntime(2844): at java.lang.reflect.Method.invoke(Method.java:372)
06-04 23:22:26.676: E/AndroidRuntime(2844): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:903)
06-04 23:22:26.676: E/AndroidRuntime(2844): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:698)
06-04 23:22:26.676: E/AndroidRuntime(2844): Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'void com.google.android.gms.maps.GoogleMap.setOnMapClickListener(com.google.android.gms.maps.GoogleMap$OnMapClickListener)' on a null object reference
06-04 23:22:26.676: E/AndroidRuntime(2844): at com.example.radaroc.SupportMapActivity.onCreate(SupportMapActivity.java:66)
06-04 23:22:26.676: E/AndroidRuntime(2844): at android.app.Activity.performCreate(Activity.java:5990)
06-04 23:22:26.676: E/AndroidRuntime(2844): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1106)
06-04 23:22:26.676: E/AndroidRuntime(2844): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2278)
06-04 23:22:26.676: E/AndroidRuntime(2844): ... 10 more
我的oncreate方法中有这个:
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_support_map);
int status = GooglePlayServicesUtil.isGooglePlayServicesAvailable(getBaseContext());
// Showing status
if(status!=ConnectionResult.SUCCESS){ // Google Play Services are not available
int requestCode = 10;
Dialog dialog = GooglePlayServicesUtil.getErrorDialog(status, this, requestCode);
dialog.show();
}else { // Google Play Services are available
FragmentManager myFM = getSupportFragmentManager();
final SupportMapFragment fm = (SupportMapFragment) myFM
.findFragmentById(R.id.map);
//SupportMapFragment fm = (SupportMapFragment) getSupportFragmentManager().findFragmentById(R.id.map);
googleMap = fm.getMap();// fm.getMapAsync(this);
googleMap.setMyLocationEnabled(true);
getSupportLoaderManager().initLoader(0, null, this);
}
}
这是我的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"
tools:context="${relativePackage}.${activityClass}" >
<fragment
android:id="@+id/map"
android:layout_width="match_parent"
android:layout_height="match_parent"
class="com.google.android.gms.maps.SupportMapFragment" />
</RelativeLayout>
答案 0 :(得分:0)
您必须在模拟器中安装Google Play服务才能使地图正常工作
答案 1 :(得分:0)
它说Google Play services out of date. Requires 7571000
,我觉得你的emulator
有旧版Google Play服务。 (与您的SDK无关)
如果您可以在真实设备上运行它,那么您的程序没有任何问题。
但是如果您想在模拟器上测试它,有两种方法可以解决它。
1)转到SDK Manager并在您使用的任何API级别下更新Google APIs (x86 System Image)
并从中创建新的模拟器。
2)或者,您可以在线找到最新的Google Play服务并通过adb install path/to/newestGooglePlayServices.apk