【问题标题】:Google Maps API v2 not working谷歌地图 API v2 不工作
【发布时间】:2013-02-15 00:52:43
【问题描述】:

我正在尝试使用 Google Maps API v2 构建一个应用程序,但问题是,尽管我遵循了所有必需的说明,但该应用程序不断给我强制关闭。

这里是安卓部分

import android.support.v4.app.FragmentActivity;
import com.google.android.maps.GeoPoint;
import com.google.android.maps.MapController;
import com.google.android.maps.MapView;

public class GPSonMap extends FragmentActivity{

 MapView map;
 MapController controller;
 LocationManager locationManager;
 LocationListener listener;
 PendingIntent pendingIntent ;
 Criteria criteria;
 MapOverlay overlay;


@Override
public void onCreate(Bundle onsaved)
{
    super.onCreate(onsaved);
    setContentView(R.layout.map);
    Log.d("", "entered");


    map = (MapView)findViewById(R.id.mapView);
    }

这里是 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=".MapsActivity" >

<fragment

    class="com.google.android.gms.maps.SupportMapFragment"
    android:id="@+id/mapView"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
/>

</RelativeLayout>

这是我的清单

        <?xml version="1.0" encoding="utf-8"?>
    <manifest xmlns:android="http://schemas.android.com/apk/res/android"
        package="com.bachelor.bond"
        android:versionCode="1"
        android:versionName="1.0" >

        <uses-sdk
            android:minSdkVersion="14"
            android:targetSdkVersion="17" />
      <!--   <permission
              android:name="com.example.example.permission.MAPS_RECEIVE"
              android:protectionLevel="signature"/>
            <uses-permission android:name="com.bachelor.bond.permission.MAPS_RECEIVE"/>. -->

           <uses-permission android:name="android.permission.INTERNET" />
         <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/>
        <permission android:name="com.example.example.permission.C2D_MESSAGE" android:protectionLevel="signature" />
    <uses-permission android:name="com.example.example.permission.C2D_MESSAGE" /> 

    <!-- App receives GCM messages. -->
    <uses-permission android:name="com.google.android.c2dm.permission.RECEIVE" />
    <!-- GCM connects to Google Services. -->
    <uses-permission android:name="android.permission.INTERNET" />
    <uses-permission android:name="com.google.android.providers.gsf.permission.READ_GSERVICES"/>
        <uses-permission android:name="android.permission.READ_PHONE_STATE"/> 
    <!-- GCM requires a Google account. -->
    <uses-permission android:name="android.permission.GET_ACCOUNTS" />
    <!-- Keeps the processor from sleeping when a message is received. -->
    <uses-permission android:name="android.permission.WAKE_LOCK"
         />
    <uses-permission android:name="com.example.example.permission.MAPS_RECEIVE"/>
    <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
    <uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
      <uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />  
    <!-- Permission to vibrate -->
        <uses-permission android:name="android.permission.VIBRATE"/>
        <uses-permission android:name = "android.permission.ACCESS_FINE_LOCATION"/>

        <permission 
        android:name="com.example.example.permission.MAPS_RECEIVE" 
        android:protectionLevel="signature"></permission>

        <uses-feature 
        android:glEsVersion="0x00020000" 
        android:required="true"/>
        <application
            android:allowBackup="true"
            android:icon="@drawable/ic_launcher"
            android:label="@string/app_name">
            <uses-library android:name="com.google.android.maps" />
             <activity
                android:name=".GPSonMap"
                android:label="@string/app_name"
                 > <intent-filter>
                    <action android:name="android.intent.action.MAIN" />

                    <category android:name="android.intent.category.LAUNCHER" />
                </intent-filter>

            </activity>

             <uses-library android:name="com.google.android.maps" />
             <intent-filter>
        <action android:name="com.google.android.c2dm.intent.RECEIVE" />
        <action android:name="com.google.android.c2dm.intent.REGISTRATION" />
        <category android:name="com.example.example" />
      </intent-filter>
              <receiver android:name="com.google.android.gcm.GCMBroadcastReceiver" 
                 android:permission="com.google.android.c2dm.permission.SEND" >

    </receiver>
     <service android:name=".GCMIntentService" />
     <activity android:name="Login"></activity>

     <meta-data
        android:name="com.google.android.maps.v2.API_KEY"
        android:value="****"/>

        </application>

    </manifest>

还有 Log Cat 输出

02-15 00:42:35.257: E/AndroidRuntime(2693): FATAL EXCEPTION: main
02-15 00:42:35.257: E/AndroidRuntime(2693): java.lang.RuntimeException: Unable to start         activity ComponentInfo{com.bachelor.bond/com.dxample.example.GPSonMap}:     android.view.InflateException: Binary XML file line #7: Error inflating class fragment
02-15 00:42:35.257: E/AndroidRuntime(2693):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2180)
02-15 00:42:35.257: E/AndroidRuntime(2693):     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2230)
02-15 00:42:35.257: E/AndroidRuntime(2693):     at android.app.ActivityThread.access$600(ActivityThread.java:141)
02-15 00:42:35.257: E/AndroidRuntime(2693):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1234)
02-15 00:42:35.257: E/AndroidRuntime(2693):     at android.os.Handler.dispatchMessage(Handler.java:99)
02-15 00:42:35.257: E/AndroidRuntime(2693):     at android.os.Looper.loop(Looper.java:137)
02-15 00:42:35.257: E/AndroidRuntime(2693):     at android.app.ActivityThread.main(ActivityThread.java:5039)
02-15 00:42:35.257: E/AndroidRuntime(2693):     at java.lang.reflect.Method.invokeNative(Native Method)
02-15 00:42:35.257: E/AndroidRuntime(2693):     at java.lang.reflect.Method.invoke(Method.java:511)
02-15 00:42:35.257: E/AndroidRuntime(2693):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
02-15 00:42:35.257: E/AndroidRuntime(2693):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)
02-15 00:42:35.257: E/AndroidRuntime(2693):     at dalvik.system.NativeStart.main(Native Method)
02-15 00:42:35.257: E/AndroidRuntime(2693): Caused by: android.view.InflateException: Binary XML file line #7: Error inflating class fragment
02-15 00:42:35.257: E/AndroidRuntime(2693):     at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:704)
02-15 00:42:35.257: E/AndroidRuntime(2693):     at android.view.LayoutInflater.rInflate(LayoutInflater.java:746)
02-15 00:42:35.257: E/AndroidRuntime(2693):     at android.view.LayoutInflater.inflate(LayoutInflater.java:489)
02-15 00:42:35.257: E/AndroidRuntime(2693):     at android.view.LayoutInflater.inflate(LayoutInflater.java:396)
02-15 00:42:35.257: E/AndroidRuntime(2693):     at android.view.LayoutInflater.inflate(LayoutInflater.java:352)
02-15 00:42:35.257: E/AndroidRuntime(2693):     at com.android.internal.policy.impl.PhoneWindow.setContentView(PhoneWindow.java:270)
02-15 00:42:35.257: E/AndroidRuntime(2693):     at android.app.Activity.setContentView(Activity.java:1881)
02-15 00:42:35.257: E/AndroidRuntime(2693):     at com.bachelor.bond.GPSonMap.onCreate(GPSonMap.java:54)
02-15 00:42:35.257: E/AndroidRuntime(2693):     at android.app.Activity.performCreate(Activity.java:5104)
02-15 00:42:35.257: E/AndroidRuntime(2693):     at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1080)
02-15 00:42:35.257: E/AndroidRuntime(2693):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2144)
02-15 00:42:35.257: E/AndroidRuntime(2693):     ... 11 more
02-15 00:42:35.257: E/AndroidRuntime(2693): Caused by:         android.support.v4.app.Fragment$InstantiationException: Unable to instantiate fragment     com.google.android.gms.maps.SupportMapFragment: make sure class name exists, is public, and     has an empty constructor that is public
02-15 00:42:35.257: E/AndroidRuntime(2693):     at android.support.v4.app.Fragment.instantiate(Fragment.java:401)
02-15 00:42:35.257: E/AndroidRuntime(2693):     at android.support.v4.app.Fragment.instantiate(Fragment.java:369)
02-15 00:42:35.257: E/AndroidRuntime(2693):     at android.support.v4.app.FragmentActivity.onCreateView(FragmentActivity.java:272)
02-15 00:42:35.257: E/AndroidRuntime(2693):     at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:676)
02-15 00:42:35.257: E/AndroidRuntime(2693):     ... 21 more
02-15 00:42:35.257: E/AndroidRuntime(2693): Caused by: java.lang.ClassNotFoundException:     Didn't find class "com.google.android.gms.maps.SupportMapFragment" on path:     /system/framework/com.google.android.maps.jar:/data/app/com.bachelor.bond-1.apk
02-15 00:42:35.257: E/AndroidRuntime(2693):     at dalvik.system.BaseDexClassLoader.findClass(BaseDexClassLoader.java:65)
02-15 00:42:35.257: E/AndroidRuntime(2693):     at java.lang.ClassLoader.loadClass(ClassLoader.java:501)
02-15 00:42:35.257: E/AndroidRuntime(2693):     at java.lang.ClassLoader.loadClass(ClassLoader.java:461)
02-15 00:42:35.257: E/AndroidRuntime(2693):     at android.support.v4.app.Fragment.instantiate(Fragment.java:391)
02-15 00:42:35.257: E/AndroidRuntime(2693):     ... 24 more

我面临的另一个问题是,当我尝试
import com.google.android.gms.maps.MapFragment; 它给出了一个错误,它无法识别包名称为com.google.android.gms,所以我什至不能使用GoogleMapMapFragment

那么有人有解决这两个问题的方法吗?

【问题讨论】:

    标签: android google-maps google-maps-api-2 google-maps-android-api-2


    【解决方案1】:

    如何在 Android 上通过 Google Maps API v2 显示 GOOGLE MAP。

    注意这两种情况都是设置 Google Play 服务和清单文件所必需的,包括多个权限和 API 密钥。

    1.在(支持)Fragment 中显示 Google 地图。

    ma​​in.xml

    <?xml version="1.0" encoding="utf-8"?>
    
    <!-- This can go anywhere in your layout (see other demos for some examples). -->
    <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"/>
    

    MainActivity.java

    /**
     * This shows how to create a simple activity with a map and a marker on the map.
     * <p>
     * Notice how we deal with the possibility that the Google Play services APK is not
     * installed/enabled/updated on a user's device.
     */
    public class BasicMapActivity extends android.support.v4.app.FragmentActivity {
        /**
         * Note that this may be null if the Google Play services APK is not available.
         */
        private GoogleMap mMap;
    
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.main);
            setUpMapIfNeeded();
        }
    
        @Override
        protected void onResume() {
            super.onResume();
            setUpMapIfNeeded();
        }
    
        /**
         * Sets up the map if it is possible to do so (i.e., the Google Play services APK is correctly
         * installed) and the map has not already been instantiated.. This will ensure that we only ever
         * call {@link #setUpMap()} once when {@link #mMap} is not null.
         * <p>
         * If it isn't installed {@link SupportMapFragment} (and
         * {@link com.google.android.gms.maps.MapView
         * MapView}) will show a prompt for the user to install/update the Google Play services APK on
         * their device.
         * <p>
         * A user can return to this Activity after following the prompt and correctly
         * installing/updating/enabling the Google Play services. Since the Activity may not have been
         * completely destroyed during this process (it is likely that it would only be stopped or
         * paused), {@link #onCreate(Bundle)} may not be called again so we should call this method in
         * {@link #onResume()} to guarantee that it will be called.
         */
        private void setUpMapIfNeeded() {
            // Do a null check to confirm that we have not already instantiated the map.
            if (mMap == null) {
                // Try to obtain the map from the SupportMapFragment.
                mMap = ((SupportMapFragment) getSupportFragmentManager().findFragmentById(R.id.map))
                        .getMap();
                // Check if we were successful in obtaining the map.
                if (mMap != null) {
                    setUpMap();
                }
            }
        }
    
        /**
         * This is where we can add markers or lines, add listeners or move the camera. In this case, we
         * just add a marker near Africa.
         * <p>
         * This should only be called once and when we are sure that {@link #mMap} is not null.
         */
        private void setUpMap() {
            mMap.addMarker(new MarkerOptions().position(new LatLng(0, 0)).title("Marker"));
        }
    }
    

    2。在(支持)Fragment 中显示 MapView。

    ma​​in.xml

    <?xml version="1.0" encoding="utf-8"?>
    
    <!-- This can go anywhere in your layout. -->
    <com.google.android.gms.maps.MapView xmlns:android="http://schemas.android.com/apk/res/android"
      android:id="@+id/map"
      android:layout_width="match_parent"
      android:layout_height="match_parent"/>
    

    MainActivity.java

    /**
     * This shows how to create a simple activity with a raw MapView and add a marker to it. This
     * requires forwarding all the important lifecycle methods onto MapView.
     */
    public class RawMapViewDemoActivity extends android.support.v4.app.FragmentActivity {
        private MapView mMapView;
    
    
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.main);
    
            mMapView = (MapView) findViewById(R.id.map);
            mMapView.onCreate(savedInstanceState);
    
    
        }
    
        @Override
        protected void onResume() {
            super.onResume();
            mMapView.onResume();
    
    
        }
    
    
    
        @Override
        protected void onPause() {
            mMapView.onPause();
            super.onPause();
        }
    
        @Override
        protected void onDestroy() {
            mMapView.onDestroy();
            super.onDestroy();
        }
    
        @Override
        public void onLowMemory() {
            super.onLowMemory();
            mMapView.onLowMemory();
        }
    
    
    }
    

    【讨论】:

    • 请问,您能解释一下我必须在 Java 构建路径中检查哪些依赖项吗?
    【解决方案2】:

    大概是您没有在您的应用项目中添加对 Google Play 服务库项目的引用。请参阅the Google Play Services SDK documentation,特别是标题为“设置项目以使用 Google Play 服务 SDK”的部分。

    【讨论】:

    • 这可能很愚蠢,但我真的不知道如何进行。我将源代码放入文件夹 google-play-services_lib 但是当我从属性 => android 导入这个库时,没有什么可以导入,当我运行应用程序时再过一段时间,同样的问题依然存在。
    • @omar:“我将源代码放入文件夹 google-play-services_lib”——我不知道你在这里指的是什么。将 google-play-services_lib 项目导入 Eclipse,然后从 Project > Properties > Android > Libraries 添加对它的引用。
    • 是的,这很好,但是当我要添加对它的引用时,当我点击“添加”按钮时,会出现一个空列表。
    • @OmArHesham:检查您导入的google-play-services_lib 项目的项目> 属性> Android,并确保选中“是库”复选框。
    • @CommonsWare...我认为在这种情况下“是库”复选框未选中。
    【解决方案3】:

    我发现您在代码中遗漏了几处。

    答案 1:你的 GPSonMAP.java 必须像下面这样修改;

    public class GPSonMap extends android.support.v4.app.FragmentActivity{
     GoogleMap googlemap;  // "MapView map;" is wrong
     //MapController controller;  ==> it was depreciated.
     LocationManager locationManager;
     LocationListener listener;
     PendingIntent pendingIntent ;
     Criteria criteria;
     //MapOverlay overlay; ==> it was depreciated.
     SupportMapFragment fm; // add this
    
    
    @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.map);
            Log.d("", "entered");
    
    
        //map = (MapView)findViewById(R.id.mapView); ==> delete it.
    
         fm = (SupportMapFragment) getSupportFragmentManager().findFragmentById(R.id.mapView);
         googleMap = fm.getMap();
        }
    
    
    }
    

    以上修改后,按“Ctrl + Shft +(字母)O”,会导入一些需要的类。

    答案2:您的布局中的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=".GPSonMap" >
    

    您的 MainActivity 不是 MapsActivity,而是 GPSonMap。

    答案 3:您的 Manifest 文件与您的包名称与清单文件中描述的包名称不匹配。请与包名一致。

    将“com.example.example”修改为你的包名。

    复制粘贴一些代码后,您必须至少检查包名和类名。

    【讨论】:

      【解决方案4】:

      我遇到了这个问题并且在浪费时间(超过 1.5 天的头发拉挫感)。然后,我观看了一段 youtube 视频,该视频对将 Google Play 服务添加到应用程序的说明进行了无聊的演练。好吧,在从 sdk 管理器添加 SDK 后的视频中,他们使用了 Eclipse“检查更新”。那个小魔法就出来了,下载了一个 SDK 管理器不做的 jar 文件!他们确实需要将此添加到说明中,以免人们感到沮丧。我很想从我的应用程序中删除谷歌地图。我用的是 1.0,我需要更新到 2.0 或沟地图。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2013-04-16
        • 2014-01-15
        • 1970-01-01
        • 2011-03-25
        • 1970-01-01
        相关资源
        最近更新 更多