我在Tabs中使用Google Map v2。
以下是活动的布局文件。
<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:background="@android:color/white" >
<RelativeLayout
android:id="@+id/registrationLayout"
android:layout_width="match_parent"
android:layout_height="50dp"
android:background="#333333" >
<ImageView
android:id="@+id/activity_logo"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerVertical="true"
android:layout_marginLeft="10dp"
android:contentDescription="@string/content_description"
android:src="@drawable/logo" />
</RelativeLayout>
<RelativeLayout
android:id="@+id/registrationLayout2"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_below="@id/registrationLayout" >
<fragment
android:id="@+id/map"
android:name="com.google.android.gms.maps.MapFragment"
android:layout_width="match_parent"
android:layout_height="match_parent" />
</RelativeLayout>
将项目转移到另一台PC时出现此错误。我已经在我的工作空间中为GoogleMap V2和Facebook SDK 3.5.2导入了GooglePlayService。
这是java代码
public class NearMe extends FragmentActivity {
GpsProvider gpsProvider;
private GoogleMap googleMap;
double lat;
double lng;
JsonParser jparser;
String latString, longString, titleString, dis, distanceString,
stationIdString, colorNameString;
ArrayList<HashMap<String, String>> dockitstationsArrayList,
stationidArrayList;
BitmapDescriptor bitmapMarker;
NetworkHelper nh;
AlertDialogManager alertDialogManager = new AlertDialogManager();
// flag for Internet connection status
Boolean isInternetPresent = false;
HashMap<String, String> markershashmap = null;
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
// Setting View for Screen
setContentView(R.layout.near_me);
dockitstationsArrayList = new ArrayList<HashMap<String, String>>();
markershashmap = new HashMap<String, String>();
try {
// Loading map
initilizeMap();
} catch (Exception e) {
e.printStackTrace();
}
googleMap.setOnInfoWindowClickListener(new OnInfoWindowClickListener() {
@Override
public void onInfoWindowClick(Marker marker) {
try {
String distance[] = marker.getSnippet().split(" - ");
String distanceString = distance[0];
String idString = marker.getId();
String stationid = markershashmap.get(idString);
Message mesg = new Message();
Bundle b = new Bundle();
b.putString("stationId", stationid);
b.putString("distance", distanceString);
mesg.setData(b);
handler.sendMessage(mesg);
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
@SuppressLint("HandlerLeak")
private Handler handler = new Handler() {
@Override
public void handleMessage(Message msg) {
Bundle b = msg.getData();
Bundle b1 = new Bundle();
b1.putString("stationId", b.getString("stationId"));
b1.putString("distance", b.getString("distance"));
Intent dockitdetailsIntent = new Intent(getParent(),
DockItStationDetails.class);
dockitdetailsIntent.putExtras(b1);
TabGroupActivity parentActivity = (TabGroupActivity) getParent();
parentActivity.startChildActivity("Dock It Details",
dockitdetailsIntent);
}
};
@Override
protected void onResume() {
super.onResume();
initilizeMap();
nh = new NetworkHelper(getApplicationContext());
isInternetPresent = nh.isConnectingToInternet();
if (!isInternetPresent) {
// Internet Connection is not present
alertDialogManager.showInfoAlertDialog(NearMe.this.getParent(),
"Please connect to working internet connection");
// stop executing code by return
return;
}
gpsProvider = new GpsProvider(this.getParent());
// check if GPS enabled
if (gpsProvider.canGetLocation()) {
double latitude = gpsProvider.getLatitude();
double longitude = gpsProvider.getLongitude();
// \n is for new line
/*
* Toast.makeText( getApplicationContext(),
* "Your Location is - \nLat: " + latitude + "\nLong: " + longitude,
* Toast.LENGTH_LONG).show();
*/
new LoadNearByDockITStations().execute(String.valueOf(latitude),
String.valueOf(longitude));
} else {
// can't get location
// GPS is not enabled
// Ask user to enable GPS in settings
AlertDialog.Builder alertDialog = new AlertDialog.Builder(
this.getParent());
// Setting Dialog Message
alertDialog
.setMessage("Your GPS seems to be disabled, do you want to enable it?");
// On pressing Settings button
alertDialog.setPositiveButton("Settings",
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
dialog.cancel();
Intent intent = new Intent(
Settings.ACTION_LOCATION_SOURCE_SETTINGS);
startActivity(intent);
}
});
// on pressing cancel button
alertDialog.setNegativeButton("Cancel",
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
dialog.cancel();
finish();
}
});
// Setting Gravity to Center
AlertDialog dialog = alertDialog.show();
dialog.setCanceledOnTouchOutside(false);
TextView messageText = (TextView) dialog
.findViewById(android.R.id.message);
messageText.setGravity(Gravity.CENTER);
// Showing Alert Message
dialog.show();
}
}
private class LoadNearByDockITStations extends
AsyncTask<String, String, String> {
String res, status;
JSONArray resultJsonArray;
HashMap<String, String> map;
@Override
protected void onPreExecute() {
// TODO Auto-generated method stub
super.onPreExecute();
}
@Override
protected String doInBackground(String... arg0) {
String lat = arg0[0];
String lng = arg0[1];
jparser = new JsonParser();
JSONObject jsonObject = jparser
.makeHttpRequest(Utility.GETDOCKITLIST + "?latitude=" + lat
+ "&longitude=" + lng + "&klm=20");
try {
// /Getting array of Events.......
if (jsonObject.getJSONArray(Utility.STATIONS) != null) {
res = jsonObject.getString(Utility.SUCCESS);
if (Integer.parseInt(res) == 1) {
resultJsonArray = jsonObject
.getJSONArray(Utility.STATIONS);
// looping through All Contacts
for (int i = 0; i < resultJsonArray.length(); i++) {
JSONObject c = resultJsonArray.getJSONObject(i);
// Storing each json item in variable
latString = c.getString(Utility.LAT);
longString = c.getString(Utility.LONG);
titleString = c.getString(Utility.TITLE);
distanceString = c.getString(Utility.DISTANCE);
stationIdString = c.getString(Utility.STATION_ID);
colorNameString = c.getString(Utility.COLOR_NAME);
// creating new HashMap
map = new HashMap<String, String>();
// adding each child node to HashMap key =>
// value
map.put(Utility.LAT, latString);
map.put(Utility.LONG, longString);
map.put(Utility.TITLE, titleString);
map.put(Utility.DISTANCE, distanceString);
// map.put(Utility.DISTANCE, distanceString);
map.put(Utility.STATION_ID, stationIdString);
map.put(Utility.COLOR_NAME, colorNameString);
dockitstationsArrayList.add(map);
}
}
}
} catch (JSONException e) {
e.printStackTrace();
}
return status;
}
@Override
protected void onPostExecute(String result) {
super.onPostExecute(result);
if (dockitstationsArrayList.size() == 0) {
Toast.makeText(getApplicationContext(),
"No Dock It Stations Found", Toast.LENGTH_LONG).show();
} else {
/*
* Toast.makeText(getApplicationContext(),
* "Dock It Stations Found", Toast.LENGTH_LONG).show();
*/
googleMap.clear();
addMarkersToMap();
}
}
}
private void addMarkersToMap() {
ArrayList<Marker> mMarkers = new ArrayList<Marker>();
LatLng ll = null;
String distance;
for (int i = 0; i < dockitstationsArrayList.size(); i++) {
ll = new LatLng(Double.parseDouble(dockitstationsArrayList.get(i)
.get(Utility.LAT)),
Double.parseDouble(dockitstationsArrayList.get(i).get(
Utility.LONG)));
distance = dockitstationsArrayList.get(i).get(Utility.DISTANCE);
stationIdString = dockitstationsArrayList.get(i).get(
Utility.STATION_ID);
colorNameString = dockitstationsArrayList.get(i).get(
Utility.COLOR_NAME);
if (colorNameString.equals("Cyan"))
bitmapMarker = BitmapDescriptorFactory
.fromResource(R.drawable.cyan_marker);
else if (colorNameString.equals("Red"))
bitmapMarker = BitmapDescriptorFactory
.fromResource(R.drawable.red_marker);
else if (colorNameString.equals("Yellow"))
bitmapMarker = BitmapDescriptorFactory
.fromResource(R.drawable.yellow_marker);
else if (colorNameString.equals("Blue"))
bitmapMarker = BitmapDescriptorFactory
.fromResource(R.drawable.blue_marker);
else if (colorNameString.equals("Green"))
bitmapMarker = BitmapDescriptorFactory
.fromResource(R.drawable.green_marker);
else if (colorNameString.equals("Purple"))
bitmapMarker = BitmapDescriptorFactory
.fromResource(R.drawable.purple_marker);
MarkerOptions mOption = new MarkerOptions();
mOption.position(ll);
mOption.title(dockitstationsArrayList.get(i).get(Utility.TITLE));
mOption.anchor(0.0f, 1.0f);
mOption.snippet(Utility.convertDistance(distance) + "m - Here");
mOption.icon(bitmapMarker);
Marker marker = googleMap.addMarker(mOption);
mMarkers.add(marker);
String markerkey = marker.getId();
markershashmap.put(markerkey,
dockitstationsArrayList.get(i).get(Utility.STATION_ID));
}
// stationidArrayList.add(markers);
final LatLngBounds.Builder bld = new LatLngBounds.Builder();
for (int i = 0; i < dockitstationsArrayList.size(); i++) {
LatLng ll2 = new LatLng(Double.parseDouble(dockitstationsArrayList
.get(i).get(Utility.LAT)),
Double.parseDouble(dockitstationsArrayList.get(i).get(
Utility.LONG)));
bld.include(ll2);
}
googleMap.animateCamera(CameraUpdateFactory.newLatLngBounds(
bld.build(), 30));
// googleMap.animateCamera(CameraUpdateFactory.zoomIn());
googleMap.setOnCameraChangeListener(new OnCameraChangeListener() {
@Override
public void onCameraChange(CameraPosition arg0) {
// Move camera.
googleMap.animateCamera(CameraUpdateFactory.newLatLngBounds(
bld.build(), 50));
// Remove listener to prevent position reset on camera move.
googleMap.setOnCameraChangeListener(null);
}
});
}
@TargetApi(Build.VERSION_CODES.HONEYCOMB)
@SuppressLint("NewApi")
private void initilizeMap() {
if (googleMap == null) {
googleMap = ((MapFragment) getFragmentManager().findFragmentById(
R.id.map)).getMap();
googleMap.getUiSettings().setZoomGesturesEnabled(true);
googleMap.getUiSettings().setCompassEnabled(true);
googleMap.getUiSettings().setRotateGesturesEnabled(true);
googleMap.getUiSettings().setMyLocationButtonEnabled(true);
// check if map is created successfully or not
if (googleMap == null) {
Toast.makeText(getApplicationContext(),
"Sorry! unable to create maps", Toast.LENGTH_SHORT)
.show();
}
}
}
}
这是清单文件
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.dockit"
android:versionCode="2"
android:versionName="1.0" >
<uses-permission android:name="android.permission.INTERNET" >
</uses-permission>
<permission
android:name="com.dockit.permission.MAPS_RECEIVE"
android:protectionLevel="signature" />
<uses-permission android:name="com.dockit.permission.MAPS_RECEIVE" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<uses-permission android:name="com.google.android.providers.gsf.permission.READ_GSERVICES" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<!-- Required to show current location -->
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
<!-- Network State Permissions -->
<uses-permission android:name="android.permission.ACCESS_MOCK_LOCATION" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<uses-permission android:name="android.permission.ACCESS_WIFI_STATE" />
<uses-permission android:name="android.permission.CHANGE_WIFI_STATE" />
<uses-permission android:name="android.permission.CHANGE_NETWORK_STATE" />
<!-- Access Location -->
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-feature
android:glEsVersion="0x00020000"
android:required="true" />
<uses-sdk
android:minSdkVersion="14"
android:targetSdkVersion="18" />
<application
android:allowBackup="true"
android:icon="@drawable/app_icon"
android:label="@string/app_name"
android:theme="@style/AppTheme"
android:windowSoftInputMode="adjustPan|adjustResize|stateHidden" >
<activity
android:name="com.dockit.SplashScreen"
android:configChanges="orientation|keyboardHidden"
android:label="@string/app_name"
android:screenOrientation="portrait"
android:theme="@android:style/Theme.NoTitleBar"
android:windowSoftInputMode="adjustPan|adjustResize|stateHidden" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity
android:name="com.dockit.AboutUs"
android:configChanges="orientation|keyboardHidden"
android:screenOrientation="portrait"
android:windowSoftInputMode="adjustPan|adjustResize|stateHidden" >
</activity>
<activity
android:name="com.dockit.ListViewDockItStations"
android:configChanges="orientation|keyboardHidden"
android:screenOrientation="portrait"
android:windowSoftInputMode="adjustPan|adjustResize|stateHidden" >
</activity>
<activity
android:name="com.dockit.More"
android:configChanges="orientation|keyboardHidden"
android:screenOrientation="portrait"
android:windowSoftInputMode="adjustPan|adjustResize|stateHidden|stateHidden" >
<intent-filter>
<action android:name="android.intent.action.VIEW" />
<category android:name="android.intent.category.DEFAULT" />
<category android:name="android.intent.category.BROWSABLE" />
<data
android:host="twitter"
android:scheme="app" />
</intent-filter>
</activity>
<activity
android:name="com.dockit.NearByDockItTabs"
android:configChanges="orientation|keyboardHidden"
android:screenOrientation="portrait"
android:windowSoftInputMode="adjustPan|adjustResize|stateHidden" >
</activity>
<activity
android:name="com.dockit.NearMeold"
android:configChanges="orientation|keyboardHidden"
android:screenOrientation="portrait"
android:windowSoftInputMode="adjustPan|adjustResize|stateHidden" >
</activity>
<activity
android:name="com.dockit.Registration"
android:configChanges="orientation|keyboardHidden"
android:screenOrientation="portrait"
android:windowSoftInputMode="adjustPan|adjustResize|stateHidden" >
</activity>
<activity
android:name="com.dockit.tabcontroller.TabGroupActivity"
android:configChanges="orientation|keyboardHidden"
android:screenOrientation="portrait"
android:windowSoftInputMode="adjustPan|adjustResize|stateHidden" >
</activity>
<activity
android:name="com.dockit.tabcontroller.TabGroup1"
android:configChanges="orientation|keyboardHidden"
android:screenOrientation="portrait"
android:windowSoftInputMode="adjustPan|adjustResize|stateHidden" >
</activity>
<activity
android:name="com.dockit.NearMe"
android:configChanges="orientation|keyboardHidden"
android:screenOrientation="portrait"
android:windowSoftInputMode="adjustPan|adjustResize|stateHidden" >
</activity>
<activity
android:name="com.dockit.tabcontroller.TabGroup2"
android:configChanges="orientation|keyboardHidden"
android:screenOrientation="portrait"
android:windowSoftInputMode="adjustPan|adjustResize|stateHidden" >
</activity>
<activity
android:name="com.dockit.tabcontroller.TabGroup3"
android:configChanges="orientation|keyboardHidden"
android:screenOrientation="portrait"
android:windowSoftInputMode="adjustPan|adjustResize|stateHidden" >
</activity>
<activity
android:name="com.dockit.DockItStationDetails"
android:configChanges="orientation|keyboardHidden"
android:screenOrientation="portrait"
android:windowSoftInputMode="adjustPan|adjustResize|stateHidden" >
</activity>
<meta-data
android:name="com.google.android.maps.v2.API_KEY"
android:value="AIzaSyAlvDShdumWacdKdHldu3TCAUPyUXId30k" />
<meta-data
android:name="com.facebook.sdk.ApplicationId"
android:value="@string/app_id" />
<activity
android:name="com.facebook.LoginActivity"
android:label="@string/app_name" >
</activity>
<activity
android:name="com.dockit.RateApplication"
android:label="@string/title_activity_rate_application" >
</activity>
</application>
</manifest>
我将片段大写但我得到了这个警告“无效标签片段应该是片段”