0

我正在关注这个https://developers.google.com/maps/documentation/android/start#installing_the_google_maps_android_v2_api来学习使用 Google Map API。

这是我的清单:

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.dude.uniplaces"
    android:versionCode="1"
    android:versionName="1.0" >
    <uses-feature
        android:glEsVersion="0x00020000"
        android:required="true"/>


    <uses-sdk
        android:minSdkVersion="8"
        android:targetSdkVersion="17" />

    <permission
        android:name="com.dude.uniplaces.permission.C2D_MESSAGE"
        android:protectionLevel="signature" />

    <uses-permission android:name="com.dude.uniplaces.permission.C2D_MESSAGE" />
    <uses-permission android:name="com.google.android.c2dm.permission.RECEIVE" />
    <uses-permission android:name="android.permission.GET_ACCOUNTS" />
    <uses-permission android:name="android.permission.WAKE_LOCK" />
    <uses-permission android:name="android.permission.VIBRATE" />
    <uses-permission android:name="android.permission.INTERNET" />
    <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
    <uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
    <uses-permission android:name="android.permission.ACCESS_MOCK_LOCATION" />
    <uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />

<uses-permission android:name="com.google.android.providers.gsf.permission.READ_GSERVICES"/>

<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>

    <permission
        android:name="com.dude.uniplaces.permission.MAPS_RECEIVE"
        android:protectionLevel="signature"/>
<uses-permission android:name="com.dude.uniplaces.permission.MAPS_RECEIVE"/>
    <application
        android:allowBackup="true"
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/AppTheme" >
        <uses-library android:name="com.google.android.maps" />          
        <receiver
            android:name="com.google.android.gcm.GCMBroadcastReceiver"
            android:permission="com.google.android.c2dm.permission.SEND" >
            <intent-filter>
                <action android:name="com.google.android.c2dm.intent.RECEIVE" />
                <action android:name="com.google.android.c2dm.intent.REGISTRATION" />

                <category android:name="com.dude.uniplaces" />
            </intent-filter>
        </receiver>

        <service android:name="com.dude.uniplaces.GCMIntentService" />

        <activity
            android:name="com.dude.uniplaces.Index"
            android:label="@string/app_name" >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
        <activity
            android:name="com.dude.uniplaces.Dude"
            android:label="dude" >
        </activity>
        <activity
            android:name="com.dude.uniplaces.SendMess"
            android:label="sendmess" >
        </activity>
        <meta-data
    android:name="com.google.android.maps.v2.API_KEY"
    android:value="AIzaSyBUO0v3_pHsRXfnGyJ68AeZkCUtHINw6OA"/>

    </application>

</manifest>

这是我的主要活动的 xml 文件:

<?xml version="1.0" encoding="utf-8"?>

<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"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context=".Index" >  


    <Button
     android:layout_height="wrap_content"
     android:layout_width="wrap_content"
     android:text="Invia Messaggio"
     android:onClick="sendMex" />
    <fragment 
          android:id="@+id/map"
          android:layout_width="match_parent"
          android:layout_height="match_parent"
          android:name="com.google.android.gms.maps.MapFragment"/>



</RelativeLayout>

这是我的主要活动:

package com.dude.uniplaces;
import static com.dude.uniplaces.CommonUtilities.DISPLAY_MESSAGE_ACTION;
import static com.dude.uniplaces.CommonUtilities.EXTRA_MESSAGE;
import static com.dude.uniplaces.CommonUtilities.SENDER_ID;

import java.util.HashMap;
import java.util.Map;

import android.app.Activity;
import android.app.ProgressDialog;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.location.Criteria;
import android.location.Location;
import android.location.LocationManager;
import android.os.AsyncTask;
import android.os.Bundle;
import android.provider.Settings;
import android.view.View;
import android.widget.Toast;

import com.google.android.gcm.GCMRegistrar;


public class Index extends Activity {
    ProgressDialog progressBar;
    AsyncTask<Void, Void, Void> mRegisterTask;
    @Override
    public void onCreate(Bundle savedInstanceState){
        super.onCreate(savedInstanceState);
        setContentView(R.layout.index);
        // Phone is ready
        GCMRegistrar.checkDevice(this);
        // Checking Manifest
        GCMRegistrar.checkManifest(this);
        registerReceiver(mHandleMessageReceiver, new IntentFilter(DISPLAY_MESSAGE_ACTION));
        // Ottieni il Registration ID
        final String regId = GCMRegistrar.getRegistrationId(this);
        // Controllo se sono registrato
        if (regId.equals("")) {
            // Mi registro
            GCMRegistrar.register(this, SENDER_ID);
        } else {
            // Sono registrato
            if (!GCMRegistrar.isRegisteredOnServer(this)) {
                // Provo a registrarmi ancora

                final Context context = this;
                mRegisterTask = new AsyncTask<Void, Void, Void>() {
                    @Override
                    protected Void doInBackground(Void... params) {
                        ServerUtilities.register(context, regId);
                        return null;
                    }
                    @Override
                    protected void onPostExecute(Void result) {
                        mRegisterTask = null;
                    }
                };
                mRegisterTask.execute(null, null, null);
            }
        }
        /* Here I should be registred */
        /* Now is time to take gps coordinates */
        LocationManager service = (LocationManager) getSystemService(LOCATION_SERVICE);
        boolean enabled = service.isProviderEnabled(LocationManager.GPS_PROVIDER);

        // Check if enabled and if not send user to the GSP settings
        // Better solution would be to display a dialog and suggesting to 
        // go to the settings

        if (!enabled) {
          Intent intent = new Intent(Settings.ACTION_LOCATION_SOURCE_SETTINGS);
          startActivity(intent);
        } 


        LocationManager locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
        Map<String, String> params = new HashMap<String, String>();
        Criteria criteria = new Criteria();
        String provider = locationManager.getBestProvider(criteria, false);
        Location location = locationManager.getLastKnownLocation(provider);
        String message="x";
        String Id = GCMRegistrar.getRegistrationId(this);
        if( location != null)
             message = String.format("%1$s \n%2$s \n%3$s",Id,
                                        location.getLongitude(), location.getLatitude()
                                );
        if(location == null )
            params.put("one", "BONAA");
        else
        params.put("one", message);
        ServerUtilities su = new ServerUtilities();
        su.go("http://unipiapp.altervista.org/cord.php",params);




    }
    /**
     * Ricevo notifica push
     * */
    private final BroadcastReceiver mHandleMessageReceiver = new BroadcastReceiver() {
        @Override
        public void onReceive(Context context, Intent intent) {
            String newMessage = intent.getExtras().getString(EXTRA_MESSAGE);
            // Sveglia il telefono se è in stand-by
            WakeLocker.acquire(getApplicationContext());
            // Visualizza il messaggio
            Toast.makeText(getApplicationContext(), "New Message: " + newMessage, Toast.LENGTH_LONG).show();
            // Rilascia il wavelocker
            WakeLocker.release();
        }
    };
    @Override
    protected void onDestroy() {
        if (mRegisterTask != null) {
            mRegisterTask.cancel(true);
        }
        try {
            unregisterReceiver(mHandleMessageReceiver);
            GCMRegistrar.onDestroy(this);
        } catch (Exception e) {
        }
        super.onDestroy();
    }

/* Function call clickin on button */
public void sendMex(View w)
{
    Intent intent = new Intent(Index.this, SendMess.class);
    startActivity(intent);

}

} /* END CLASS */

我下载了 google play 服务并将其添加到我的工作区,但是当我尝试在手机中启动应用程序时,它崩溃了:

05-04 20:11:24.441: E/AndroidRuntime(11190): 致命异常: main 05-04 20:11:24.441: E/AndroidRuntime(11190): java.lang.RuntimeException: 无法启动活动 ComponentInfo{com. dude.uniplaces/com.dude.uniplaces.Index}:android.view.InflateException:二进制 XML 文件第 19 行:膨胀类片段时出错


编辑:正如你所见,我修改了我的 index.xml,然后我以正确的方式添加(认为)谷歌库,但我仍然崩溃!


解决方案:

我解决了添加 import android.support.v4.app.FragmentActivity; 和 chancing 使用 FragmentActivity 扩展 Activity

4

3 回答 3

0

您在 8 中的最小 SDK

注意:如果您希望在 api 11 及更低版本上运行应用程序,您还需要添加支持库。

http://developer.android.com/tools/extras/support-library.html

用这个

  android:name="com.google.android.gms.maps.SupportMapFragment"

还要确保您已按照以下链接中的所有步骤进行操作

https://developers.google.com/maps/documentation/android/start

编辑

https://developers.google.com/maps/documentation/android/reference/com/google/android/gms/maps/MapFragment

api 12 及更高版本的 MapFragment 支持 api 11 及更低版本的片段

还要确保您映射项目引用谷歌播放服务库项目。

在您的地图项目中参考您的 google play 服务

检查它是否是一个库项目。右键单击您的项目转到属性。选择安卓。检查是否勾选了 Is Library。

在此处输入图像描述

在您的地图项目中引用图书馆项目。

右键单击您的项目转到属性。选择安卓。单击添加,浏览库项目单击添加并确定。注意下面的绿色勾号。

在此处输入图像描述

有关更多信息,请查看以下链接

https://developer.android.com/tools/projects/projects-eclipse.html

由于您使用的是 SupportFragment,因此您还需要添加 Support 库。检查以下链接

https://developer.android.com/tools/extras/support-library.html

于 2013-05-04T18:21:55.590 回答
0

右键单击项目并选择 Android 工具 -> 添加支持库 ...

并改变这个

<fragment 
      android:id="@+id/map"
      android:layout_width="match_parent"
      android:layout_height="match_parent"
      android:name="com.google.android.gms.maps.MapFragment"/>

对此

<fragment
        android:id="@+id/map"
        android:name="com.google.android.gms.maps.SupportMapFragment"
        android:layout_width="match_parent"
        android:layout_height="match_parent" />

并且只是为了确认您已正确包含谷歌播放服务,右键单击项目并选择属性。在左侧选项卡中选择 Android。在右窗格的底部,您应该看到一个带有标签库的框架。如果正确添加了 google play,它将显示为绿色复选标记。还要确保在工作区中打开了 google play 项目。

于 2013-05-05T07:26:02.460 回答
0

看起来您引用google-play-services库的方式存在某种问题。看看我写的这篇博文的前三个步骤,Google Maps API V2了解如何正确地做到这一点:

谷歌地图 API V2

于 2013-05-05T07:31:22.723 回答