【问题标题】:Application crashes when using Google Maps v2 on most devices在大多数设备上使用 Google Maps v2 时应用程序崩溃
【发布时间】:2013-08-01 15:58:23
【问题描述】:

我正在尝试编写一些使用 Google Maps API 的应用程序。地图显示在主要活动中。

在某些手机(包括模拟器)上,应用程序在启动后会立即崩溃。它唯一可以使用的手机是我的 Galaxy S1,它运行 CM10.1 (4.2.2)。

它在我的 HTC DESIRE HD(4.2.2 以及)上崩溃

MainActivity.java:

import android.location.Location;
import android.location.LocationListener;
import android.location.LocationManager;
import android.net.Uri;
import android.os.Bundle;
import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.ImageView;
import android.widget.Toast;

import com.google.android.gms.maps.CameraUpdateFactory;
import com.google.android.gms.maps.GoogleMap;
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 MainActivity extends Activity {

    public void ToastLoadShout(String msg){Toast.makeText(this, msg, Toast.LENGTH_LONG).show();}

      static final LatLng HAMBURG = new LatLng(53.558, 9.927);
      static final LatLng KIEL = new LatLng(53.551, 9.993);
      static final LatLng gps = new LatLng(0, 0);
      static double lat=0.0;
      static double lon=0.0;
      private GoogleMap map;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        turnGPSOn();

        map = ((MapFragment) getFragmentManager().findFragmentById(R.id.map))
                .getMap();
           // Marker hamburg = map.addMarker(new MarkerOptions().position(HAMBURG)
           //    .title("Hamburg"));
            Marker kiel = map.addMarker(new MarkerOptions()
                .position(KIEL)
                .title("Free shyt")
                .snippet("Come and take this shit")
                .icon(BitmapDescriptorFactory
                    .fromResource(R.drawable.ic_launcher)));

            ImageView locate;
            locate = (ImageView) findViewById(R.id.locate);
            locate.setOnClickListener(new OnClickListener() {
                public void onClick(View v) {
                    ReNewCoordinates();

                    if (lat==0&&lon==0)
                        ToastLoadShout("Try again in a few seconds.");
                    else
                    {
                    map.moveCamera(CameraUpdateFactory.newLatLngZoom(new LatLng(lat,lon), 10));
                    ToastLoadShout("your location is: "+lat+" , "+lon);
                    map.animateCamera(CameraUpdateFactory.zoomTo(10), 2000, null);

                    Marker loc = map.addMarker(new MarkerOptions()
                    .position(new LatLng(lat,lon))
                    .icon(BitmapDescriptorFactory
                        .fromResource(R.drawable.loc)));
                    }
                }
            });

            map.moveCamera(CameraUpdateFactory.newLatLngZoom(new LatLng(50.909474,13.917618), 10000));
            map.animateCamera(CameraUpdateFactory.zoomTo(1), 20, null);

            // Move the camera instantly to hamburg with a zoom of 15.
            //map.moveCamera(CameraUpdateFactory.newLatLngZoom(new LatLng(lat,lon), 15));

            // Zoom in, animating the camera.
            //map.animateCamera(CameraUpdateFactory.zoomTo(10), 2000, null);
            //ToastLoadShout("your location is: "+lat+" , "+lon);
            ImageView add;
            add = (ImageView) findViewById(R.id.add);
            add.setOnClickListener(new OnClickListener() {
                public void onClick(View v) {
                    Intent intent = new Intent(MainActivity.this, Add.class);
                    startActivity(intent);
                    }
            });
    }

     private void turnGPSOn() {

            String provider = android.provider.Settings.Secure.getString(
                    getContentResolver(),
                    android.provider.Settings.Secure.LOCATION_PROVIDERS_ALLOWED);
            if (!provider.contains("gps")) { // if gps is disabled
                final Intent poke = new Intent();
                poke.setClassName("com.android.settings",
                        "com.android.settings.widget.SettingsAppWidgetProvider");
                poke.addCategory(Intent.CATEGORY_ALTERNATIVE);
                poke.setData(Uri.parse("3"));
                sendBroadcast(poke);
                ToastLoadShout("Turning GPS on..");
            }
        }

     public void ReNewCoordinates(){
            LocationManager locationManager = (LocationManager) this.getSystemService(Context.LOCATION_SERVICE);
            // Define a listener that responds to location updates
            LocationListener locationListener = new LocationListener() {
                public void onLocationChanged(Location location) {
                    // Called when a new location is found by the network location provider.
                    lat = (location.getLatitude());
                    lon = (location.getLongitude());
                }
                public void onProviderDisabled(String provider) {}
                public void onProviderEnabled(String provider) {}
                public void onStatusChanged(String provider, int status,Bundle extras) {}};
                locationManager.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, 0, 0, locationListener);
            }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.main, menu);
        return true;
    }

}

我这里有 HTC 的 logcat:

08-01 18:46:29.929: I/Process(4007): Sending signal. PID: 4007 SIG: 9
08-01 18:46:32.031: D/skia(4024): new locale en-Latn-GB
08-01 18:46:32.141: W/GooglePlayServicesUtil(4024): Google Play services out of date.  Requires 3159100 but found 2012110
08-01 18:46:32.151: W/GooglePlayServicesUtil(4024): Google Play services out of date.  Requires 3159100 but found 2012110
08-01 18:46:32.151: W/GooglePlayServicesUtil(4024): Google Play services out of date.  Requires 3159100 but found 2012110
08-01 18:46:32.161: W/GooglePlayServicesUtil(4024): Google Play services out of date.  Requires 3159100 but found 2012110
08-01 18:46:32.171: W/GooglePlayServicesUtil(4024): Google Play services out of date.  Requires 3159100 but found 2012110
08-01 18:46:32.231: W/GooglePlayServicesUtil(4024): Google Play services out of date.  Requires 3159100 but found 2012110
08-01 18:46:32.231: D/AndroidRuntime(4024): Shutting down VM
08-01 18:46:32.231: W/dalvikvm(4024): threadid=1: thread exiting with uncaught exception (group=0x40b10930)
08-01 18:46:32.241: E/AndroidRuntime(4024): FATAL EXCEPTION: main
08-01 18:46:32.241: E/AndroidRuntime(4024): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.free/com.example.free.MainActivity}: java.lang.NullPointerException: IBitmapDescriptorFactory is not initialized
08-01 18:46:32.241: E/AndroidRuntime(4024):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2308)
08-01 18:46:32.241: E/AndroidRuntime(4024):     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2358)
08-01 18:46:32.241: E/AndroidRuntime(4024):     at android.app.ActivityThread.access$600(ActivityThread.java:153)
08-01 18:46:32.241: E/AndroidRuntime(4024):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1247)
08-01 18:46:32.241: E/AndroidRuntime(4024):     at android.os.Handler.dispatchMessage(Handler.java:99)
08-01 18:46:32.241: E/AndroidRuntime(4024):     at android.os.Looper.loop(Looper.java:137)
08-01 18:46:32.241: E/AndroidRuntime(4024):     at android.app.ActivityThread.main(ActivityThread.java:5227)
08-01 18:46:32.241: E/AndroidRuntime(4024):     at java.lang.reflect.Method.invokeNative(Native Method)
08-01 18:46:32.241: E/AndroidRuntime(4024):     at java.lang.reflect.Method.invoke(Method.java:511)
08-01 18:46:32.241: E/AndroidRuntime(4024):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:795)
08-01 18:46:32.241: E/AndroidRuntime(4024):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:562)
08-01 18:46:32.241: E/AndroidRuntime(4024):     at dalvik.system.NativeStart.main(Native Method)
08-01 18:46:32.241: E/AndroidRuntime(4024): Caused by: java.lang.NullPointerException: IBitmapDescriptorFactory is not initialized
08-01 18:46:32.241: E/AndroidRuntime(4024):     at com.google.android.gms.internal.x.b(Unknown Source)
08-01 18:46:32.241: E/AndroidRuntime(4024):     at com.google.android.gms.maps.model.BitmapDescriptorFactory.aX(Unknown Source)
08-01 18:46:32.241: E/AndroidRuntime(4024):     at com.google.android.gms.maps.model.BitmapDescriptorFactory.fromResource(Unknown Source)
08-01 18:46:32.241: E/AndroidRuntime(4024):     at com.example.free.MainActivity.onCreate(MainActivity.java:52)
08-01 18:46:32.241: E/AndroidRuntime(4024):     at android.app.Activity.performCreate(Activity.java:5104)
08-01 18:46:32.241: E/AndroidRuntime(4024):     at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1080)
08-01 18:46:32.241: E/AndroidRuntime(4024):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2262)
08-01 18:46:32.241: E/AndroidRuntime(4024):     ... 11 more

还有模拟器的logcat:

08-01 15:27:48.565: E/Trace(981): error opening trace file: No such file or directory (2)
08-01 15:27:49.305: W/GooglePlayServicesUtil(981): Google Play services out of date.  Requires 3159100 but found 1
08-01 15:27:49.326: W/GooglePlayServicesUtil(981): Google Play services out of date.  Requires 3159100 but found 1
08-01 15:27:49.345: W/GooglePlayServicesUtil(981): Google Play services out of date.  Requires 3159100 but found 1
08-01 15:27:49.366: W/GooglePlayServicesUtil(981): Google Play services out of date.  Requires 3159100 but found 1
08-01 15:27:49.385: W/GooglePlayServicesUtil(981): Google Play services out of date.  Requires 3159100 but found 1
08-01 15:27:49.565: D/dalvikvm(981): GC_CONCURRENT freed 200K, 4% free 8223K/8519K, paused 36ms+5ms, total 163ms
08-01 15:27:49.565: D/dalvikvm(981): WAIT_FOR_CONCURRENT_GC blocked 88ms
08-01 15:27:49.745: W/GooglePlayServicesUtil(981): Google Play services out of date.  Requires 3159100 but found 1
08-01 15:27:49.745: D/AndroidRuntime(981): Shutting down VM
08-01 15:27:49.755: W/dalvikvm(981): threadid=1: thread exiting with uncaught exception (group=0x40a13300)
08-01 15:27:49.765: E/AndroidRuntime(981): FATAL EXCEPTION: main
08-01 15:27:49.765: E/AndroidRuntime(981): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.free/com.example.free.MainActivity}: java.lang.NullPointerException: IBitmapDescriptorFactory is not initialized
08-01 15:27:49.765: E/AndroidRuntime(981):  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2059)
08-01 15:27:49.765: E/AndroidRuntime(981):  at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2084)
08-01 15:27:49.765: E/AndroidRuntime(981):  at android.app.ActivityThread.access$600(ActivityThread.java:130)
08-01 15:27:49.765: E/AndroidRuntime(981):  at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1195)
08-01 15:27:49.765: E/AndroidRuntime(981):  at android.os.Handler.dispatchMessage(Handler.java:99)
08-01 15:27:49.765: E/AndroidRuntime(981):  at android.os.Looper.loop(Looper.java:137)
08-01 15:27:49.765: E/AndroidRuntime(981):  at android.app.ActivityThread.main(ActivityThread.java:4745)
08-01 15:27:49.765: E/AndroidRuntime(981):  at java.lang.reflect.Method.invokeNative(Native Method)
08-01 15:27:49.765: E/AndroidRuntime(981):  at java.lang.reflect.Method.invoke(Method.java:511)
08-01 15:27:49.765: E/AndroidRuntime(981):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:786)
08-01 15:27:49.765: E/AndroidRuntime(981):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
08-01 15:27:49.765: E/AndroidRuntime(981):  at dalvik.system.NativeStart.main(Native Method)
08-01 15:27:49.765: E/AndroidRuntime(981): Caused by: java.lang.NullPointerException: IBitmapDescriptorFactory is not initialized
08-01 15:27:49.765: E/AndroidRuntime(981):  at com.google.android.gms.internal.x.b(Unknown Source)
08-01 15:27:49.765: E/AndroidRuntime(981):  at com.google.android.gms.maps.model.BitmapDescriptorFactory.aX(Unknown Source)
08-01 15:27:49.765: E/AndroidRuntime(981):  at com.google.android.gms.maps.model.BitmapDescriptorFactory.fromResource(Unknown Source)
08-01 15:27:49.765: E/AndroidRuntime(981):  at com.example.free.MainActivity.onCreate(MainActivity.java:52)
08-01 15:27:49.765: E/AndroidRuntime(981):  at android.app.Activity.performCreate(Activity.java:5008)
08-01 15:27:49.765: E/AndroidRuntime(981):  at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1079)
08-01 15:27:49.765: E/AndroidRuntime(981):  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2023)
08-01 15:27:49.765: E/AndroidRuntime(981):  ... 11 more
08-01 15:28:19.685: I/Process(981): Sending signal. PID: 981 SIG: 9
08-01 15:52:15.355: E/Trace(1058): error opening trace file: No such file or directory (2)
08-01 15:52:16.015: W/GooglePlayServicesUtil(1058): Google Play services out of date.  Requires 3159100 but found 1
08-01 15:52:16.025: W/GooglePlayServicesUtil(1058): Google Play services out of date.  Requires 3159100 but found 1
08-01 15:52:16.055: W/GooglePlayServicesUtil(1058): Google Play services out of date.  Requires 3159100 but found 1
08-01 15:52:16.075: W/GooglePlayServicesUtil(1058): Google Play services out of date.  Requires 3159100 but found 1
08-01 15:52:16.095: W/GooglePlayServicesUtil(1058): Google Play services out of date.  Requires 3159100 but found 1
08-01 15:52:16.265: D/dalvikvm(1058): GC_CONCURRENT freed 206K, 4% free 8223K/8519K, paused 76ms+5ms, total 157ms
08-01 15:52:16.265: D/dalvikvm(1058): WAIT_FOR_CONCURRENT_GC blocked 31ms
08-01 15:52:16.326: W/GooglePlayServicesUtil(1058): Google Play services out of date.  Requires 3159100 but found 1
08-01 15:52:16.335: D/AndroidRuntime(1058): Shutting down VM
08-01 15:52:16.335: W/dalvikvm(1058): threadid=1: thread exiting with uncaught exception (group=0x40a13300)
08-01 15:52:16.355: E/AndroidRuntime(1058): FATAL EXCEPTION: main
08-01 15:52:16.355: E/AndroidRuntime(1058): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.free/com.example.free.MainActivity}: java.lang.NullPointerException: IBitmapDescriptorFactory is not initialized
08-01 15:52:16.355: E/AndroidRuntime(1058):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2059)
08-01 15:52:16.355: E/AndroidRuntime(1058):     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2084)
08-01 15:52:16.355: E/AndroidRuntime(1058):     at android.app.ActivityThread.access$600(ActivityThread.java:130)
08-01 15:52:16.355: E/AndroidRuntime(1058):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1195)
08-01 15:52:16.355: E/AndroidRuntime(1058):     at android.os.Handler.dispatchMessage(Handler.java:99)
08-01 15:52:16.355: E/AndroidRuntime(1058):     at android.os.Looper.loop(Looper.java:137)
08-01 15:52:16.355: E/AndroidRuntime(1058):     at android.app.ActivityThread.main(ActivityThread.java:4745)
08-01 15:52:16.355: E/AndroidRuntime(1058):     at java.lang.reflect.Method.invokeNative(Native Method)
08-01 15:52:16.355: E/AndroidRuntime(1058):     at java.lang.reflect.Method.invoke(Method.java:511)
08-01 15:52:16.355: E/AndroidRuntime(1058):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:786)
08-01 15:52:16.355: E/AndroidRuntime(1058):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
08-01 15:52:16.355: E/AndroidRuntime(1058):     at dalvik.system.NativeStart.main(Native Method)
08-01 15:52:16.355: E/AndroidRuntime(1058): Caused by: java.lang.NullPointerException: IBitmapDescriptorFactory is not initialized
08-01 15:52:16.355: E/AndroidRuntime(1058):     at com.google.android.gms.internal.x.b(Unknown Source)
08-01 15:52:16.355: E/AndroidRuntime(1058):     at com.google.android.gms.maps.model.BitmapDescriptorFactory.aX(Unknown Source)
08-01 15:52:16.355: E/AndroidRuntime(1058):     at com.google.android.gms.maps.model.BitmapDescriptorFactory.fromResource(Unknown Source)
08-01 15:52:16.355: E/AndroidRuntime(1058):     at com.example.free.MainActivity.onCreate(MainActivity.java:52)
08-01 15:52:16.355: E/AndroidRuntime(1058):     at android.app.Activity.performCreate(Activity.java:5008)
08-01 15:52:16.355: E/AndroidRuntime(1058):     at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1079)
08-01 15:52:16.355: E/AndroidRuntime(1058):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2023)
08-01 15:52:16.355: E/AndroidRuntime(1058):     ... 11 more

【问题讨论】:

  • 您是否启用了 GL 模式? GoogleMap2 库是如何包含在您的项目中的?
  • 我刚刚导入了谷歌服务:像这样:导入 → Android → 现有 Android 代码到工作区。
  • 如何启用该 GL 模式?没听说过。。
  • 要启用 GL 模式,请将此代码放入 AndroidManifest 为了与 google 服务良好配合,您必须导入整个 google-play -services_lib 到您的项目中,如库。

标签: android google-maps android-mapview


【解决方案1】:

只需执行以下操作即可检查 google play 服务是否可用:

int status = GooglePlayServicesUtil.isGooglePlayServicesAvailable(getApplicationContext());
    if(status == ConnectionResult.SUCCESS) {
        //Success! Do what you want

    }else{
        GooglePlayServicesUtil.getErrorDialog(status, this, status);
    }

它将检查 google play 服务,如果不可用,它将提示用户从 google play 下载它们

【讨论】:

    【解决方案2】:

    听起来您正在测试的手机没有安装最新的 Google Play 服务 pre-req。

    如果您查看here,您会发现一个部分讨论如何确保设备安装了 google play 服务 APK,如果没有,请指导用户安装它。

    另请注意,运行低于 Andorid 4.2.2 的 API 平台的模拟器不支持该模拟器(在第一部分中引用 here

    【讨论】:

    • 我如何实现这些方法?我必须添加一个库还是什么?因为它告诉我没有“isGooglePlayServicesAvailable();”这样的东西例如。
    • 有没有其他方法可以使用地图并避免要求用户下载sdk?
    • "isGooglePlayServicesAvailable()" 位于 com.google.android.gms.common 中,如下所示:developer.android.com/reference/com/google/android/gms/common/… 如果您将它添加到您的项目。如果没有,可以在 {SDK Dir}\extras\google 中找到
    • 至于另一种方式,不是真的 - 至少不是正式的。您也无法在 Play Market 中搜索它,因为它是隐藏的。这不是 SDK,而是用户使用最新的 Google Maps v2 实现所需的 Google Play Services APK,因此大多数用户应该已经拥有它
    • @NirPes 要调用GooglePlayServicesUtil.isGooglePlayServicesAvailable(context),您必须使用import com.google.android.gms.common.GooglePlayServicesUtil;
    【解决方案3】:

    我的应用也有同样的异常:

    java.lang.NullPointerException: IBitmapDescriptorFactory is not initialized
    

    就我而言,我必须在初始化 Google Map 对象后调用 BitmapDescriptorFactory 静态方法。

    阅读cmets:

    // This line initilizes the map asynchronously. It requires implementation
    // of OnMapReadyCallback interface.
    // And when it is inizilized, it calls onMapReady(GoogleMap googleMap)
    mapFragment.getMapAsync(this);
    
    
    // The following method is implemented in my activity
    @Override
    public void onMapReady(GoogleMap googleMap) {
        ... some code ...
        currentLocationIcon = BitmapDescriptorFactory.fromResource(R.drawable.report_location_current)
        ... some code ...
    }
    

    【讨论】:

      【解决方案4】:
      GoogleApiAvailability googleApiAvailability=GoogleApiAvailability.getInstance();
      
      int status=googleApiAvailability.isGooglePlayServicesAvailable(getActivity());
      
              if (status != ConnectionResult.SUCCESS) {
                  int requestCode = 10;
                  Dialog dialog = googleApiAvailability.getErrorDialog(getActivity(),status,requestCode);
                  dialog.show();
              }else{}
      

      【讨论】:

        【解决方案5】:

        请参考here 以检查您的手机上是否有可用的 Google Play 服务。如果您遇到错误,则会显示从 Google Play 服务返回的错误对话框。我在三星 Galaxy S3 上使用 Google Maps v2 开发了我的应用程序,并且运行良好。

        【讨论】:

          猜你喜欢
          • 2013-10-03
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2013-02-17
          • 2013-08-29
          • 1970-01-01
          • 2011-04-08
          相关资源
          最近更新 更多