我正在关注此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 */
我下载谷歌播放服务并将其添加到我的工作区,但当我尝试在手机中启动应用程序时,它崩溃了:
05-04 20:11:24.441:E / AndroidRuntime(11190):致命异常:主要 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,然后我以正确的方式添加(思考)google libs,但我仍然会崩溃!
解决方案:
我解决了添加import android.support.v4.app.FragmentActivity; 和chancing使用FragmentActivity扩展Activity
答案 0 :(得分:0)
你的8分钟
注意:如果您希望在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
修改
用于api 12及更低版本的api 12及更高支持片段的MapFragment
还要确保映射项目参考google play services library project。
在地图项目中引用您的Google Play服务
检查它是否是一个库项目。右键单击您的项目转到属性。选择android。检查是否勾选了库。
在地图项目中引用库项目。
右键单击项目转到属性。选择android。单击添加,浏览库项目单击添加,然后单击确定。注意下面的绿色勾号。
有关更多信息,请查看以下链接
https://developer.android.com/tools/projects/projects-eclipse.html
您还需要添加支持库,因为您使用的是SupportFragment。请查看以下链接
https://developer.android.com/tools/extras/support-library.html
答案 1 :(得分:0)
右键单击该项目并选择Android Tools - &gt;添加支持库...
并更改此
<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" />
并确认您已正确包含Google Play服务,右键单击项目并选择“属性”。在左侧选项卡中选择Android。在右侧窗格的底部,您应该看到带有标签库的框架。如果正确添加Google Play,则会显示为绿色复选标记。另外,请确保在工作区中打开Google Play项目。
答案 2 :(得分:0)
看起来您引用google-play-services
库的方式存在某种问题。看看我在Google Maps API V2
上写的这篇博客文章的前三个步骤,以了解如何正确地做到这一点: