【问题标题】:Google maps class not working谷歌地图类不工作
【发布时间】:2013-11-24 10:51:01
【问题描述】:

我的 android 应用程序中有 Google 地图,在我的大多数 android 设备上,我的 google 地图工作正常,但在一台设备上,我的 google 地图无法正常工作。 我的谷歌地图课程是:

public class Map extends  Activity {

    // Google Map
    private GoogleMap googleMap;
    Intent intent;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.map);

        try {
            // Loading map
            initilizeMap();


        } catch (Exception e) {
            e.printStackTrace();
        }

    }

    /**
     * function to load map. If map is not created it will create it for you
     * */
    private void initilizeMap() {
          double latitude = 0 ;
          double longitude = 0;

        if (googleMap == null) {
            googleMap = ((MapFragment) getFragmentManager().findFragmentById(
                    R.id.map)).getMap();


            intent=getIntent();

            String location=intent.getStringExtra("location");


      /*      GPS gps = new GPS(this);
            if(gps.canGetLocation()){ // gps enabled} // return boolean true/false

           latitude=gps.getLatitude(); // returns latitude
           longitude=gps.getLongitude(); // returns longitude
            }
            */

        //  Toast.makeText(getApplicationContext(), location, Toast.LENGTH_LONG).show();
            String[] arr = location.split(" ", 2); 
            latitude=Double.valueOf(arr[0]);
            longitude=Double.valueOf(arr[1]);



            //    googleMap.setMyLocationEnabled(true);
            // create marker
            MarkerOptions marker = new MarkerOptions().position(new LatLng(latitude, longitude));

            // adding marker
            googleMap.addMarker(marker);
            CameraUpdate center=
                    CameraUpdateFactory.newLatLng(new LatLng(latitude,
                                                            longitude));
                CameraUpdate zoom=CameraUpdateFactory.zoomTo(15);

                googleMap.moveCamera(center);
                googleMap.animateCamera(zoom);

            // check if map is created successfully or not

            if (googleMap == null) {
                Toast.makeText(getApplicationContext(),
                        "Sorry! unable to create maps", Toast.LENGTH_SHORT)
                        .show();
            }
        }
    }

    @Override
    protected void onResume() {
        super.onResume();
        initilizeMap();
    }

}

我的这个设备的 logcat 是:

10-24 03:46:02.535: D/ActivityThread(3501): ACT-AM_ON_PAUSE_CALLED ActivityRecord{418378f0 token=android.os.BinderProxy@41837140 {soft.b.peopleassist/soft.b.peopleassist.Respond}}
10-24 03:46:02.546: D/ActivityThread(3501): ACT-PAUSE_ACTIVITY handled : 1 / android.os.BinderProxy@41837140
10-24 03:46:02.551: D/ThemeManager(3501): packageName=====soft.b.peopleassist
10-24 03:46:02.551: D/ThemeManager(3501): packageName=soft.b.peopleassist
10-24 03:46:02.551: D/RRR(3501): path=other/bottom/bottom7.png
10-24 03:46:02.553: D/ThemeManager(3501): sIconWidth=64----sIconHeight=64
10-24 03:46:02.555: D/ThemeManagerHH(3501): infoDrawable=72
10-24 03:46:02.589: W/GooglePlayServicesUtil(3501): Google Play services is missing.
10-24 03:46:02.590: W/GooglePlayServicesUtil(3501): Google Play services is missing.
10-24 03:46:02.593: W/GooglePlayServicesUtil(3501): Google Play services is missing.
10-24 03:46:02.596: W/GooglePlayServicesUtil(3501): Google Play services is missing.
10-24 03:46:02.598: W/GooglePlayServicesUtil(3501): Google Play services is missing.
10-24 03:46:02.603: W/GooglePlayServicesUtil(3501): Google Play services is missing.
10-24 03:46:02.605: W/System.err(3501): java.lang.NullPointerException
10-24 03:46:02.606: W/System.err(3501):     at soft.b.peopleassist.Map.initilizeMap(Map.java:76)
10-24 03:46:02.607: W/System.err(3501):     at soft.b.peopleassist.Map.onCreate(Map.java:30)
10-24 03:46:02.607: W/System.err(3501):     at android.app.Activity.performCreate(Activity.java:4510)
10-24 03:46:02.608: W/System.err(3501):     at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1050)
10-24 03:46:02.609: W/System.err(3501):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2048)
10-24 03:46:02.609: W/System.err(3501):     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2111)
10-24 03:46:02.610: W/System.err(3501):     at android.app.ActivityThread.access$600(ActivityThread.java:134)
10-24 03:46:02.610: W/System.err(3501):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1251)
10-24 03:46:02.610: W/System.err(3501):     at android.os.Handler.dispatchMessage(Handler.java:99)
10-24 03:46:02.611: W/System.err(3501):     at android.os.Looper.loop(Looper.java:137)
10-24 03:46:02.612: W/System.err(3501):     at android.app.ActivityThread.main(ActivityThread.java:4666)
10-24 03:46:02.612: W/System.err(3501):     at java.lang.reflect.Method.invokeNative(Native Method)
10-24 03:46:02.613: W/System.err(3501):     at java.lang.reflect.Method.invoke(Method.java:511)
10-24 03:46:02.614: W/System.err(3501):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:809)
10-24 03:46:02.614: W/System.err(3501):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:576)
10-24 03:46:02.615: W/System.err(3501):     at dalvik.system.NativeStart.main(Native Method)
10-24 03:46:02.617: W/GooglePlayServicesUtil(3501): Google Play services is missing.
10-24 03:46:02.618: D/AndroidRuntime(3501): Shutting down VM
10-24 03:46:02.619: W/dalvikvm(3501): threadid=1: thread exiting with uncaught exception (group=0x40e61258)
10-24 03:46:02.624: E/AndroidRuntime(3501): FATAL EXCEPTION: main
10-24 03:46:02.624: E/AndroidRuntime(3501): java.lang.RuntimeException: Unable to resume activity {soft.b.peopleassist/soft.b.peopleassist.Map}: java.lang.NullPointerException
10-24 03:46:02.624: E/AndroidRuntime(3501):     at android.app.ActivityThread.performResumeActivity(ActivityThread.java:2602)
10-24 03:46:02.624: E/AndroidRuntime(3501):     at android.app.ActivityThread.handleResumeActivity(ActivityThread.java:2630)
10-24 03:46:02.624: E/AndroidRuntime(3501):     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2116)
10-24 03:46:02.624: E/AndroidRuntime(3501):     at android.app.ActivityThread.access$600(ActivityThread.java:134)
10-24 03:46:02.624: E/AndroidRuntime(3501):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1251)
10-24 03:46:02.624: E/AndroidRuntime(3501):     at android.os.Handler.dispatchMessage(Handler.java:99)
10-24 03:46:02.624: E/AndroidRuntime(3501):     at android.os.Looper.loop(Looper.java:137)
10-24 03:46:02.624: E/AndroidRuntime(3501):     at android.app.ActivityThread.main(ActivityThread.java:4666)
10-24 03:46:02.624: E/AndroidRuntime(3501):     at java.lang.reflect.Method.invokeNative(Native Method)
10-24 03:46:02.624: E/AndroidRuntime(3501):     at java.lang.reflect.Method.invoke(Method.java:511)
10-24 03:46:02.624: E/AndroidRuntime(3501):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:809)
10-24 03:46:02.624: E/AndroidRuntime(3501):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:576)
10-24 03:46:02.624: E/AndroidRuntime(3501):     at dalvik.system.NativeStart.main(Native Method)
10-24 03:46:02.624: E/AndroidRuntime(3501): Caused by: java.lang.NullPointerException
10-24 03:46:02.624: E/AndroidRuntime(3501):     at soft.b.peopleassist.Map.initilizeMap(Map.java:76)
10-24 03:46:02.624: E/AndroidRuntime(3501):     at soft.b.peopleassist.Map.onResume(Map.java:98)
10-24 03:46:02.624: E/AndroidRuntime(3501):     at android.app.Instrumentation.callActivityOnResume(Instrumentation.java:1159)
10-24 03:46:02.624: E/AndroidRuntime(3501):     at android.app.Activity.performResume(Activity.java:4584)
10-24 03:46:02.624: E/AndroidRuntime(3501):     at android.app.ActivityThread.performResumeActivity(ActivityThread.java:2588)
10-24 03:46:02.624: E/AndroidRuntime(3501):     ... 12 more

【问题讨论】:

    标签: android google-maps android-intent nullpointerexception google-play-services


    【解决方案1】:

    从 logcat 中可以清楚地看出,您的设备没有 google play services,因为要使用地图,您需要 google play services,这就是它崩溃的原因。

    W/GooglePlayServicesUtil(3501): Google Play services is missing.
    

    根据this 的说法,许多 Gingerbread 手机不附带 Google Play 服务是一个众所周知的问题,因此您需要在应用启动时检查安装,并安全地告诉用户他们的设备不支持或阻止这些设备下载市场上的应用程序。

    希望这对您有所帮助,并为您提供了如何做的线索。

    【讨论】:

    • 检查post no. #2,他解释了问题所在以及如何解决它。我从未尝试过,但它似乎是合理的。
    • 或者如果您在检测到用户设备并检查它是否没有google play services 之后想要您,您可以将他们引导到他们可以手动下载google play services 的页面。 check this one out
    【解决方案2】:

    在使用 google maps API 之前,您需要检查设备上是否有 google play 服务。

      public static boolean isGooglePlayServicesAvailable(SplashScreenActivity context) {
            int status = GooglePlayServicesUtil.isGooglePlayServicesAvailable(context);
            if (ConnectionResult.SUCCESS == status) {
                return true;
            } else {
                //GooglePlayServicesUtil.getErrorDialog(status, context, 0).show();
                return false;
            }
        }
    

    如果 google play 服务不可用,您可以将用户导航到 playstore 以安装 google play 服务。

    startActivity(new Intent(Intent.ACTION_VIEW, Uri.parse("market://details?id="+ GoogleApiAvailability.GOOGLE_PLAY_SERVICES_PACKAGE)));
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2015-06-25
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-04-27
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多