我正在关注这个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