[英]map work on device but crash on emulator
我试图使用SupportFragmentManager在Android上使用Google地图。 它在我的智能手机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>
您必须在模拟器中安装Google Play服务才能使地图正常工作
它说Google Play services out of date. Requires 7571000
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
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.